Jeffrey Berthiaume Jeffrey Berthiaume - 7 months ago 118
Swift Question

Dropbox 2.0 Swift API: how to get Media Metadata

I'm using the SwiftyDropbox library for an iOS project, getting a list of folders recursively, and checking to see if files are photos or videos.

client.files.getMetadata(path: fileURL, includeMediaInfo: true).response { response, error in

if let file = response as? Files.FileMetadata {
if file.mediaInfo != nil {

// ??? how to get file.mediaInfo.metadata
// specifically, I need the mediaMetadata

}
}
}


I can see file.mediaInfo (which, if it exists, means that metadata exists, but the documentation doesn't show how to get the actual metadata itself (specifically, dimensions for photos or durations for video).

I can get this from the description of file.mediaInfo (and parse the String that is returned from that), but that's hacky and not future-safe. Is there another way to get this data?

This is the class I want to get data from (in Files.swift):

public class MediaMetadata: CustomStringConvertible {
/// Dimension of the photo/video.
public let dimensions : Files.Dimensions?
/// The GPS coordinate of the photo/video.
public let location : Files.GpsCoordinates?
/// The timestamp when the photo/video is taken.
public let timeTaken : NSDate?
public init(dimensions: Files.Dimensions? = nil, location: Files.GpsCoordinates? = nil, timeTaken: NSDate? = nil) {
self.dimensions = dimensions
self.location = location
self.timeTaken = timeTaken
}
public var description : String {
return "\(prepareJSONForSerialization(MediaMetadataSerializer().serialize(self)))"
}
}

Answer

Here's a sample:

Dropbox.authorizedClient!.files.getMetadata(path: "/test.jpg", includeMediaInfo: true).response { response, error in
    if let result = response as? Files.FileMetadata {
        print(result.name)

        if result.mediaInfo != nil {
            switch result.mediaInfo! as Files.MediaInfo {
            case .Pending:
                print("Media info is pending...")
            case .Metadata(let mediaMetadata):
                print(mediaMetadata.dimensions)
                print(mediaMetadata.location)
                print(mediaMetadata.timeTaken)
            }
        }
    } else {
        print(error!)
    }
}
Comments