ohho
ohho

Reputation: 51941

How to get NSURL from a Dropbox file?

I'd like to use QLPreviewController to preview a file stored in Dropbox. QLPreviewController takes a NSURL, however a Dropbox DBFile returns only a NSFileHandle * or the file content in NSData * or NSString *.

What's the proper way to preview a Dropbox DBFile in QLPreviewController?

Upvotes: 1

Views: 645

Answers (2)

auco
auco

Reputation: 9579

I'm using the dropbox sync API.

Here's a snippet that copies a dropbox file to a temp dir, thus "converting" a DBFileInfo item to a NSURL.

This method:

  • assumes you have a self.file NSArray containing DBFileInfo items (see: [[DBFilesystem sharedFilesystem] listFolder:[DBPath root] error:&err]; )
  • opens and reads the info item as NSData
  • writes to a tempURL with the same filename
  • and returns the tempURL

The snippet is not perfect, but can be used as it is. However, some user info like a HUD with a progress spinner should be used (it may take a while to download the file).

- (NSURL*)urlOfDropboxItemAtIndex:(NSUInteger)index {
    if(index < self.files.count) {
        //[self presentProcessingFileHUD];
        __block NSURL *fileURL;

        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            NSError *err;
            DBFileInfo *fileInfo = self.files[index];
            NSString *tempDir = NSTemporaryDirectory();
            NSURL *tempURL = [NSURL fileURLWithPath:[tempDir stringByAppendingPathComponent:@"dropbox-tmp"]];
            // create tempURL
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if(![fileManager fileExistsAtPath:tempURL.path]) {
                [fileManager createDirectoryAtURL:tempURL withIntermediateDirectories:YES attributes:nil error:&err];
                if(err) {
                    NSString *errMsg = [NSString stringWithFormat:@"Error creating temp directory at %@", tempURL.path];
                    NSLog(@"%@",errMsg);
                    [self presentErrorHUDWithMessage:errMsg];
                }
            }
            fileURL = [tempURL URLByAppendingPathComponent:fileInfo.path.name];
            // read dropbox file
            DBFile *file = [[DBFilesystem sharedFilesystem] openFile:fileInfo.path error:&err];
            if(file && !err) {
                NSData *fileData = [file readData:&err];
                if(err) { NSLog(@"Error reading data from file %@ (%@)", fileURL.path, err); }
                [fileData writeToURL:fileURL atomically:YES];
                [file close];
                //[self dismissHUD];

            } else {
                NSString *errStr = [NSString stringWithFormat:@"Error accessing Dropbox file %@ (%@)", fileURL.path, err];
                NSLog(@"%@", errStr);
                //[self presentErrorHUDWithMessage:errStr];
                fileURL = nil;

            }
        });
        return fileURL;

    } else {
        return nil;
    }
}

Upvotes: 1

Wain
Wain

Reputation: 119031

QLPreviewController may take an NSURL, but it needs to be a file URL. So, the file contents needs to be downloaded and saved to disk (using any of the methods of sourcing the file data that you want).

Upvotes: 0

Related Questions