Skip to content

Commit

Permalink
CB-5959: Android, iOS: Return size with Entry.getMetadata() method
Browse files Browse the repository at this point in the history
  • Loading branch information
clelland committed Feb 7, 2014
1 parent 3c0ba72 commit 9ac8e47
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
13 changes: 2 additions & 11 deletions src/android/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,14 @@ public void run() throws IOException, JSONException {
}
},callbackContext);
}
else if (action.equals("getMetadata")) {
final String fname=args.getString(0);
threadhelper( new FileOp( ){
public void run() throws FileNotFoundException, JSONException, MalformedURLException {
JSONObject obj = getFileMetadata(fname);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj.getLong("lastModifiedDate")));
}
}, callbackContext);
}
else if (action.equals("getFileMetadata")) {
else if (action.equals("getMetadata") || action.equals("getFileMetadata")) {
final String fname=args.getString(0);
threadhelper( new FileOp( ){
public void run() throws FileNotFoundException, JSONException, MalformedURLException {
JSONObject obj = getFileMetadata(fname);
callbackContext.success(obj);
}
},callbackContext);
}, callbackContext);
}
else if (action.equals("getParent")) {
final String fname=args.getString(0);
Expand Down
4 changes: 3 additions & 1 deletion src/ios/CDVLocalFilesystem.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ - (void)getMetadataForURL:(CDVFilesystemURL *)url callback:(void (^)(CDVPluginRe
NSDictionary* fileAttribs = [fileMgr attributesOfItemAtPath:[self filesystemPathForURL:url] error:&error];

if (fileAttribs) {
// Ensure that directories (and other non-regular files) report size of 0
unsigned long long size = ([fileAttribs fileType] == NSFileTypeRegular ? [fileAttribs fileSize] : 0);
NSDate* modDate = [fileAttribs fileModificationDate];
if (modDate) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:[modDate timeIntervalSince1970] * 1000];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"modificationTime": @([modDate timeIntervalSince1970] * 1000), @"size": @(size)}];
}
} else {
// didn't get fileAttribs
Expand Down
4 changes: 2 additions & 2 deletions www/Entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function Entry(isFile, isDirectory, name, fullPath, fileSystem) {
*/
Entry.prototype.getMetadata = function(successCallback, errorCallback) {
argscheck.checkArgs('FF', 'Entry.getMetadata', arguments);
var success = successCallback && function(lastModified) {
var metadata = new Metadata(lastModified);
var success = successCallback && function(entryMetadata) {
var metadata = new Metadata(entryMetadata);
successCallback(metadata);
};
var fail = errorCallback && function(code) {
Expand Down
13 changes: 11 additions & 2 deletions www/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@
*
* {Date} modificationTime (readonly)
*/
var Metadata = function(time) {
this.modificationTime = (typeof time != 'undefined'?new Date(time):null);
var Metadata = function(metadata) {
if (typeof metadata == "object") {
this.modificationTime = new Date(metadata.modificationTime);
this.size = +(metadata.size);
} else if (typeof metadata == "undefined") {
this.modificationTime = null;
this.size = null;
} else {
/* Backwards compatiblity with platforms that only return a timestamp */
this.modificationTime = new Date(metadata);
}
};

module.exports = Metadata;

0 comments on commit 9ac8e47

Please sign in to comment.