3

I wanna get thumbnails for dropbox files that I display on a view. I know that I've to use the loadThumbnail method, but I don't get exactly how to do it.

I wrote this :

for file in dropboxMetadata.contents {
        dbRestClient.loadThumbnail(file.path, ofSize: "s", intoPath: "https://api-content.dropbox.com/1/thumbnails/auto/")
}

but I get some errors like this :

error making request to /1/thumbnails/dropbox/star.jpg - (4) Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed.

Thanks for your help !

Jasper
  • 7,031
  • 3
  • 35
  • 43
Karz
  • 557
  • 1
  • 6
  • 22
  • show your "`- (void)restClient:(DBRestClient *)client loadedThumbnail:(NSString *)destPath metadata:(DBMetadata *)metadata`" implementation – Michael Dautermann Aug 17 '15 at 08:21
  • 1
    The `intoPath` needs to be a location that you can write to locally. – Anya Shenanigans Aug 17 '15 at 08:29
  • If I'm right, I have to put thumbnails on my own url ? @MichaelDautermann I haven't saw this method, so I can put images in my view in this method when thumbnails are ready ? – Karz Aug 17 '15 at 08:31
  • So I replaced the method by this : `client.loadThumbnail("https://api-content.dropbox.com/1/thumbnails/auto/", ofSize: "s", intoPath: "http://myUrl")` and I got this error : `error making request to /1/thumbnails/dropboxhttps://api-content.dropbox.com/1/thumbnails/auto - (400) Expected 'root' to be 'dropbox', 'sandbox', or 'auto', got u'dropboxhttps:'` – Karz Aug 17 '15 at 08:45
  • 1
    path is a file system path and not a URL. – Michael Dautermann Aug 17 '15 at 08:46

1 Answers1

0

Petesh has the right idea. intoPath is the destination directory for those thumbnails.

Try this:

    func createTempDirectory() -> String? {
    let tempDirectoryTemplate = NSTemporaryDirectory().stringByAppendingPathComponent("XXXXX")

    let fileManager = NSFileManager.defaultManager()

    var err: NSErrorPointer = nil

    // remove any previous temporary folder that's there, in case it's there
    fileManager.removeItemAtPath(tempDirectoryTemplate)

    if fileManager.createDirectoryAtPath(tempDirectoryTemplate, withIntermediateDirectories: true, attributes: nil, error: err) {
        return tempDirectoryTemplate
    } else {
        print("can't create temporary directory at \(tempDirectoryTemplate)")
        return nil
    }
}

The above code for which I found in this question

Then you could change your own code to do something like:

let temporaryDirectory = createTempDirectory()
for file in dropboxMetadata.contents {
        dbRestClient.loadThumbnail(file.path, ofSize: "s", intoPath: temporaryDirectory)
}

If this works, then you could change the "intoPath" parameter into any directory you think is more appropriate.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks for your answer, it seems to really work ! Now I wonder how to get thumbnails, are they called by their name in the url ? Something like : `var imageName = temporaryDirectory + "/star.jpg" ` ? – Karz Aug 17 '15 at 08:51
  • I suspect so, but you can list out the files in that folder and see what you can find. [Here is another useful question & answer that might help you](http://stackoverflow.com/questions/25285016/swift-iterate-through-files-in-a-folder-and-its-subfolders). – Michael Dautermann Aug 17 '15 at 08:53
  • Thanks, but when I print `temporaryDirectory`, it return nil. Is that right ? – Karz Aug 17 '15 at 08:58
  • how & where are you printing it? I added a couple lines to the temporary directory creation code up there. – Michael Dautermann Aug 17 '15 at 09:07
  • I declared `tempDirectoryTemplate` and did `stringByAppendingPathComponent` after and it works. It return that : `Optional("/Users/simpleandnew2/Library/Developer/CoreSimulator/Devices/2F8FD9AD-0EA5-4DE9-B2E3-553B374049EA/data/Containers/Data/Application/37567AC9-ECDC-46A4-8FFB-E0AAF1E0E4BF/tmp/")` – Karz Aug 17 '15 at 09:13
  • Looks like it's working well for the simulator. You also may want to change my code so instead of deleting the temp directory (and the files inside it) each time it runs, you simply check for the existence of that directory and if it doesn't exist, then go ahead and create it. – Michael Dautermann Aug 17 '15 at 09:15
  • Thanks, but the error in my question `error making request to /1/thumbnails/dropbox/star.jpg`is always displaying, and I don"t understand why – Karz Aug 17 '15 at 09:23
  • Okay! I give up for now. Perhaps somebody else can answer. – Michael Dautermann Aug 17 '15 at 09:24
  • Thanks Michael you helped me a lot – Karz Aug 17 '15 at 09:28