jellyfin-web/dashboard-ui/cordova/fileupload.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-09-10 11:28:22 -07:00
(function (globalScope) {
function fileUpload() {
var self = this;
2015-09-15 20:55:26 -07:00
self.upload = function (path, name, url) {
2015-09-10 11:28:22 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
resolveLocalFileSystemURL(path, function (fileEntry) {
2015-09-10 11:28:22 -07:00
2015-09-15 20:55:26 -07:00
fileEntry.file(function (file) {
2015-09-17 09:04:04 -07:00
var mimeType = (file.type || '');
if (mimeType.indexOf('image/') != 0) {
Logger.log('Skipping upload because file is not an image. path: ' + path + ' mimeType: ' + mimeType);
deferred.reject();
return;
}
2015-09-15 20:55:26 -07:00
Logger.log('mimeType for file ' + path + ' is ' + file);
var onSuccess = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
deferred.resolve();
}
var onFail = function (error) {
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
deferred.reject();
}
2015-09-10 11:28:22 -07:00
2015-09-15 20:55:26 -07:00
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = name;
options.mimeType = mimeType;
2015-09-10 11:28:22 -07:00
2015-09-15 20:55:26 -07:00
var params = {};
options.params = params;
2015-09-10 11:28:22 -07:00
2015-09-17 09:04:04 -07:00
new FileTransfer().upload(path, url, onSuccess, onFail, options);
2015-09-15 20:55:26 -07:00
}, function () {
Logger.log('File upload failed. fileEntry.file returned an error');
deferred.reject();
});
}, function () {
Logger.log('File upload failed. resolveLocalFileSystemURL returned an error');
deferred.reject();
});
2015-09-10 11:28:22 -07:00
return deferred.promise();
};
}
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
globalScope.MediaBrowser.FileUpload = fileUpload;
})(this);