I'm trying to save a downloaded file so I can open it in another session. I'm saving the mp3 data to the documents directory, and I'm saving the url to the file in a local datastore. When I check using
if ([[NSFileManager defaultManager] fileExistsAtPath:musicObject[#"localFile"]]){
NSLog(#"applicationDocumentsDir exists");
}
else {
NSLog(#"File doesn't exist");
}
it returns "File doesn't exist", but I know it does because I've printed out the documents directory which gives me
"file:///private/var/mobile/Containers/Data/Application/94552DFC-022B-4962-9CB7-CCD87CB43E57/Documents/xDDsCbXAFhwEqGIzJfJRByEr1.mp3",
and I'm trying to access it with the same path but the first is private. How do I make the file not private (I have saved it earlier in the app)
file:///var/mobile/Containers/Data/Application/AE27BD8F-5EEB-48FC-A8D4-E228F99CECE3/Documents/xDDsCbXAFhwEqGIzJfJRByEr1.mp3
I suggest the following steps:
Take the last path component (that's just your mp3 filename)
Get the current Documents directory
Build a new path with the current Documents directory and the extracted mp3 filename
If I remember correctly, /var is a symlink to /private/var. So depending on how the path is built, one may end up with one or the other.
I inherited a project which was struck by the same problem, only with an extra randomly named directory in between. Eventually I removed the leading /private component, constructed an array of path components and checked, whether replacing non-existent elements with the current value leads to an existing file. You case should be easier to handle.
I don't remember when, but at some point the application directory (the hex numbers path component) began to change with almost each run in the simulator. Beginning with this behaviour such problems became much more visible. Although one should not save full paths, I suspect a lot of projects didn't care in the past. On one hand because things just worked, and on the other hand because a lot of people just don't know it.
Related
I'm starting to go loopy on this one, which normally means I'm missing something obvious.
I have the following code in my .NET MVC controller:
var provider = new CustomMultipartFileStreamProvider("prefix", GetRepositoryTempFolder());
await request.Content.ReadAsMultipartAsync(provider);
The CustomMultipartFileStreamProvider inherits from MultipartFileStreamProvider, overrides the GetLocalFileName method and returns an appropriate file name for a given ContentDisposition header. The HttpContent handled is normally a PDF file and a small JSON settings component.
Everything works wonderfully as far as getting the parts parsed, extracted and saved in the expected location. However, after parsing I end up with a temporary file left over in my C:\Windows\Temp folder. The file has a randomly generated name (e.g. zf0hk2h4.ks2). It has the same size and creation date as the PDF portion parsed and saved by ReadAsMultipartAsync.
I believe ReadAsMultipartAsync uses this temporary file during parsing and leaves it behind. Has anyone else experienced this? Any way I can prevent ReadAsMultipartAsync from leaving this temporary file behind and clogging up the Windows temp folder?
My apologies; I was missing something. The file was being moved after processing. This is an invalid question; there is nothing wrong with ReadAsMultipartAsync.
I have images in uitableview, they each have a string for they're path in documents directory.
Now my trouble is if somebody adds the same image they will have the same path.
I was thinking of making an if-statement that will run on all of my fetchedResultsController objects or better yet my entire documents directory and append a number or something to the pathString.
lets say user adds title.jpg to doc directory, then he adds the same image then I want a check to see if it already exists, if it already exists in doc directory then append title(1).jpg so it can save properly and so on.
any efficient way of doing that ?
Depending on the OS you're targeting you can set the image name using NSUUID. If you're targeting < iOS 6 you'll have to use CFUUIDRef. This will always ensure you have a unique filename for an image.
So, here's one of those too-simple-to-fail bugs that we all hate.
I have a .lua file that, among other things, tries to load an image via Love's newImageData function (and place it into a button):
back_button = buttonmanager.createButton("back", love.image.newImageData("../Images/BackButton.png"), width-200, height-105)
Love fails to load, throwing this error:
./frame.lua:5: Could not open file ../Images/BackButton.png. Does not exist.
I've gone through the stupid-mistakes process ("well, does it actually exist? Where is the folder?" etc) -- the file exists, is in the Images folder, which is one level up -- I can even say "ls ../Images/BackButton.png" from the directory this script sits in, and it outputs BackButton.png as I'd expect.
Is there some weird relative pathing issue I need to watch out for? I tried changing it to an absolute path and it gave me the same error.
The love.filesystem module restricts access to files in certain locations:
This module provides access to Files in two places, and two places only:
* The root folder of the .love-file. (Alternatively a directory).
* The root folder of the write directory.
Is the ../Images directory outside of your game's folder/archive?
I have a file that describes input data, which is split into several other files. In my descriptor file, I first give the path A that tells where all the other files are found.
The originator may set either a relative (to location of the descriptor file) or absolute path.
When my program is called, the user gives the name of the descriptor file. It may not be in the current working directory, so the filename B given may also contain directories.
For my program to always find the input files at the right places, I need to combine this information. If the path A given is absolute, I need to just that one. If it is relative, I need to concatenate it to the path B (i.e. directory portion of the filename).
I thought boost::filesystem::complete may do the job for me. Unfortunately, it seems it is not. I also did not understand how to test wether a path given is absolute or not.
Any ideas?
Actually I was quite misguided first but now found the solution myself. When "base" holds the path A, and filename holds B:
boost::filesystem::path basepath(base), filepath(filename);
if (!basepath.is_complete())
basepath = filepath.remove_leaf() /= basepath;
base = basepath.string();
It works with Linux at least (where it would be very easy to do without boost, but oh well..), still have to test with Windows.
I have this code,
showmessage('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID);
if removedir('C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID) then
showmessage('Removed')
else
showmessage('Failed');
The message shows C:\TEMP\0 and this directory does exist as the program created it earlier and used files inside it and then later deletes them. I can see the files and directories so I know they're there. The program successfully deletes the files but does not remove the directory.
If I hardcode the directory it works - this means that it accepts the string
C:\TEMP\0 but does not accept C:\TEMP\'+openfiles[openfilelist.ItemIndex].ID both equate to C:\TEMP\0. I cannot hardcode these directories, so what can I do? How do I convert from a string + string to whatever removedir() is expecting. I looked this up at Delphi basics and it's expecting a string.
I'm confused, since string + string = string. What is going on?
Make sure that neither your program nor any other program have the directory as their current working directory. When you recompile the program this may no longer be the case, so it may be a red herring that the hardcoded value works for you.
In addition to the other good answers, you should not be storing your temp folder in C:\TEMP. Use the value returned from GetTempFilename, instead. Unlike C:\TEMP, this location (which varies by operating system) will work on all operating systems, and all levels of user access control. This also eliminates the risk that the location you have hardcoded might also be hardcoded into another system.
If I understood correctly, openfiles[openfilelist.ItemIndex].ID is a string that contains number?
If so, did you check that it does not contain blanks? Something like this:
filename := 'C:\TEMP\' + trim(openfiles[openfilelist.ItemIndex].ID);
showmessage(filename);
if removedir(filename) then
showmessage('Removed')
else
showmessage('Failed');
What type of objects are openfiles and openfilelist?
Do they open folders at all, if so they may still be open when your trying to delete the folder.