Added MediaFileProxy.js
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/3fa5d138 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/3fa5d138 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/3fa5d138 Branch: refs/heads/master Commit: 3fa5d1389ba31a2cb04f2126f27ee047391b1117 Parents: a6e4d9e Author: mingfeng <mingfengwan...@gmail.com> Authored: Wed Aug 15 15:10:48 2012 +0800 Committer: mingfeng <mingfengwan...@gmail.com> Committed: Wed Aug 15 15:10:48 2012 +0800 ---------------------------------------------------------------------- lib/win8metro/exec.js | 1 + lib/win8metro/plugin/win8metro/MediaFileProxy.js | 82 +++++++++++++++++ 2 files changed, 83 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3fa5d138/lib/win8metro/exec.js ---------------------------------------------------------------------- diff --git a/lib/win8metro/exec.js b/lib/win8metro/exec.js index 54604c5..9545aa4 100644 --- a/lib/win8metro/exec.js +++ b/lib/win8metro/exec.js @@ -33,6 +33,7 @@ var CommandProxy = { "Device":require('cordova/plugin/win8metro/DeviceProxy'), "File":require('cordova/plugin/win8metro/FileProxy'), "Media":require('cordova/plugin/win8metro/MediaProxy'), + "MediaFile":require('cordova/plugin/win8metro/MediaFileProxy'), "NetworkStatus":require('cordova/plugin/win8metro/NetworkStatusProxy') }; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3fa5d138/lib/win8metro/plugin/win8metro/MediaFileProxy.js ---------------------------------------------------------------------- diff --git a/lib/win8metro/plugin/win8metro/MediaFileProxy.js b/lib/win8metro/plugin/win8metro/MediaFileProxy.js new file mode 100755 index 0000000..45c486b --- /dev/null +++ b/lib/win8metro/plugin/win8metro/MediaFileProxy.js @@ -0,0 +1,82 @@ +var utils = require('cordova/utils'), + File = require('cordova/plugin/File'), + CaptureError = require('cordova/plugin/CaptureError'); +/** + * Represents a single file. + * + * name {DOMString} name of the file, without path information + * fullPath {DOMString} the full path of the file, including the name + * type {DOMString} mime type + * lastModifiedDate {Date} last modified date + * size {Number} size of the file in bytes + */ +var MediaFile = function (name, fullPath, type, lastModifiedDate, size) { + MediaFile.__super__.constructor.apply(this, arguments); +}; + +utils.extend(MediaFile, File); + +/** + * Request capture format data for a specific file and type + * + * @param {Function} successCB + * @param {Function} errorCB + */ +MediaFile.prototype.getFormatData = function (successCallback, errorCallback) { + var contentType = this.type; + if (typeof this.fullPath === "undefined" || this.fullPath === null) { + errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); + } else { + Windows.Storage.StorageFile.getFileFromPathAsync(this.fullPath).then(function (storageFile) { + var mediaTypeFlag = String(contentType).split("/")[0].toLowerCase(); + if (mediaTypeFlag === "audio") { + storageFile.properties.getMusicPropertiesAsync().then(function (audioProperties) { + successCallback(new MediaFileData(null, audioProperties.bitrate, 0, 0, audioProperties.duration / 1000)); + }, function () { + errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); + }) + } + else if (mediaTypeFlag === "video") { + storageFile.properties.getVideoPropertiesAsync().then(function (videoProperties) { + successCallback(new MediaFileData(null, videoProperties.bitrate, videoProperties.height, videoProperties.width, videoProperties.duration / 1000)); + }, function () { + errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); + }) + } + else if (mediaTypeFlag === "image") { + storageFile.properties.getImagePropertiesAsync().then(function (imageProperties) { + successCallback(new MediaFileData(null, 0, imageProperties.height, imageProperties.width, 0)); + }, function () { + errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); + }) + } + else { errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)) } + }, function () { + errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT)); + }) + } +}; + +// TODO: can we axe this? +/** + * Casts a PluginResult message property (array of objects) to an array of MediaFile objects + * (used in Objective-C and Android) + * + * @param {PluginResult} pluginResult + */ +MediaFile.cast = function (pluginResult) { + var mediaFiles = []; + for (var i = 0; i < pluginResult.message.length; i++) { + var mediaFile = new MediaFile(); + mediaFile.name = pluginResult.message[i].name; + mediaFile.fullPath = pluginResult.message[i].fullPath; + mediaFile.type = pluginResult.message[i].type; + mediaFile.lastModifiedDate = pluginResult.message[i].lastModifiedDate; + mediaFile.size = pluginResult.message[i].size; + mediaFiles.push(mediaFile); + } + pluginResult.message = mediaFiles; + return pluginResult; +}; + +module.exports = MediaFile; \ No newline at end of file