querying documents directory objects ios - ios

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.

Related

How to replace an entire file using FileManager

Let's say I have an UIImage cached in my Cache folder:
/.../Cache/Image Cache/<firstImage.id>
Now I want this folder to only ever have 10 image cached at a time, so if a new one comes in I want to take a file and replace the entire file and not just the contents of it. I.e.
/.../Cache/Image Cache/<firstImage.id> becomes
/.../Cache/Image Cache/<secondImage.id>.
As far as I can tell, replaceItem(at:withItemAt:backupItemName:options: only replaces the contents of the file but the file name remains the same. And I'm not too sure what replaceItem(at:withItemAt:backupItemName:options:resultingItemURL:) does even though it might be what I'm looking for (I don't know what an AutoreleasingUnsafeMutablePointer<NSURL?>? was but it sounded dangerous so I decided to leave it alone, specially since it has the word "unsafe" in it).
Is there a straightforward way of doing using an in-built function or is manually deleting the old file and adding the new file the best way? Please let me know.
Thanks in advance!

Can't read saved file from documents directory

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.

Update File Name on Amazon S3 with IOS

I know there is no folder or directory concept on Amazon S3, but I want to update or delete a folder according to my app's users needs.
To Delete folder I guess I have to delete all the files one by one in a folder with:
- (S3DeleteObjectResponse *)deleteObjectWithKey:(NSString *)theKey withBucket:(NSString *)theBucket
http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AmazonS3Client.html#tasks
But there is no documentation to update a object name
Lets say my folder name is photos and there are two files in that folder image.png and image2.png that means I have
photos/
photos/image.png
photos/image2.png
I want to change folder name to photos1 that means I have to change image.png and image2.png to:
photos1/
photos1/image.png
photos1/image2.png
How can I accomplish this?
Is this the closest I can get? (copyObject)
Your thinking is correct. You can't rename a 'folder'. You must process each file individually by creating a copy with the desired name and deleting the original.
Also, be aware that according to the docs (http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectCOPY.html) the copy operation does not preserve the ACL, so if the moved files are public you need to explicitly mark the copies as public as well.
Related: How to rename a folder inside a bucket using the AmazonS3.net sdk

Check condition if image available in upload folder or not, in rails3?

I'm using carrier wave to upload images in rails3,
i want to just check the condition is image available in upload folder or not.
Because in my case suppose image name available in data base but by mistake image deleted from upload folder in that scenario when click on image link getting error .
I think, this can be done using exist?(file_name) method of Ruby File Class. I assume you can get the full path of the file from the database, and pass it to the class method as follows:
File.exist?('full/path/of/the/file')
This will return true if the file exists otherwise false. for reference you can read about File operations here
Carrierwave has a built-in method to do just that. It works whether your storage method is :file or :fog. Looks like this:
picture.data.file.exists?

filename..extension // Paperclip reprocess

Some kind of problem I can't resolveā€¦
In some app, a method called on :before_create was prefixing the file's extension with a double-dot (ex. /images/13402/medium/hey-1..jpg)
The problem is fixed for the new ones, but nothing occurs when I apply a reprocess! on the old ones; and I'd like to know if anyone could help about it
Reprocess / refresh only takes your original image and recreates the defined styles in your model class. So if your original image contains a file path with double dots in it, these are also applied to the generated styles. You have to clean up your original files and the stored file paths in your model records.
The only way I know would be to write a little script to modify this. Basically
foreach image
strip out double dots from original file name
rename file
store new file path in model record
end
And then rake paperclip:refresh

Resources