2015-09-09 10:49:44 -07:00
|
|
|
|
(function (globalScope) {
|
|
|
|
|
|
|
|
|
|
function contentUploader(connectionManager) {
|
|
|
|
|
|
2015-09-09 20:22:52 -07:00
|
|
|
|
var self = this;
|
|
|
|
|
|
2015-09-09 10:49:44 -07:00
|
|
|
|
self.uploadImages = function (server) {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
2015-09-09 20:22:52 -07:00
|
|
|
|
var apiClient = connectionManager.getApiClient(server.Id);
|
2015-09-09 10:49:44 -07:00
|
|
|
|
|
2015-09-26 07:51:26 -07:00
|
|
|
|
apiClient.getContentUploadHistory().done(function (result) {
|
2015-09-09 10:49:44 -07:00
|
|
|
|
|
2015-09-26 07:51:26 -07:00
|
|
|
|
uploadImagesWithHistory(server, result, apiClient, deferred);
|
2015-09-09 10:49:44 -07:00
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function uploadImagesWithHistory(server, uploadHistory, apiClient, deferred) {
|
|
|
|
|
|
2015-09-10 11:28:22 -07:00
|
|
|
|
require(['localassetmanager', "cryptojs-sha1"], function () {
|
|
|
|
|
|
|
|
|
|
LocalAssetManager.getCameraPhotos().done(function (photos) {
|
|
|
|
|
|
|
|
|
|
photos = getFilesToUpload(photos, uploadHistory);
|
|
|
|
|
|
|
|
|
|
Logger.log('Found ' + photos.length + ' files to upload');
|
|
|
|
|
|
|
|
|
|
uploadNext(photos, 0, server, apiClient, deferred);
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFilesToUpload(files, uploadHistory) {
|
|
|
|
|
|
|
|
|
|
return files.filter(function (file) {
|
|
|
|
|
|
|
|
|
|
// Seeing some null entries for some reason
|
|
|
|
|
if (!file) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uploadHistory.FilesUploaded.filter(function (u) {
|
|
|
|
|
|
|
|
|
|
return getUploadId(file) == u.Id;
|
|
|
|
|
|
|
|
|
|
}).length == 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getUploadId(file) {
|
|
|
|
|
return CryptoJS.SHA1(file + "1").toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadNext(files, index, server, apiClient, deferred) {
|
|
|
|
|
|
|
|
|
|
var length = files.length;
|
|
|
|
|
|
|
|
|
|
if (index >= length) {
|
2015-09-09 10:49:44 -07:00
|
|
|
|
|
|
|
|
|
deferred.resolve();
|
2015-09-10 11:28:22 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadFile(files[index], apiClient).done(function () {
|
|
|
|
|
|
|
|
|
|
uploadNext(files, index + 1, server, apiClient, deferred);
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
uploadNext(files, index + 1, server, apiClient, deferred);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadFile(file, apiClient) {
|
|
|
|
|
|
|
|
|
|
var deferred = DeferredBuilder.Deferred();
|
|
|
|
|
|
|
|
|
|
require(['fileupload', "cryptojs-sha1"], function () {
|
|
|
|
|
|
|
|
|
|
var name = 'camera image ' + new Date().getTime();
|
|
|
|
|
|
|
|
|
|
var url = apiClient.getUrl('Devices/CameraUploads', {
|
|
|
|
|
DeviceId: apiClient.deviceId(),
|
|
|
|
|
Name: name,
|
|
|
|
|
Album: 'Camera Roll',
|
|
|
|
|
Id: getUploadId(file),
|
|
|
|
|
api_key: apiClient.accessToken()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Logger.log('Uploading file to ' + url);
|
|
|
|
|
|
2015-09-15 20:55:26 -07:00
|
|
|
|
new MediaBrowser.FileUpload().upload(file, name, url).done(function () {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
Logger.log('File upload succeeded');
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
|
|
|
|
|
Logger.log('File upload failed');
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
2015-09-09 10:49:44 -07:00
|
|
|
|
});
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
2015-09-09 10:49:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!globalScope.MediaBrowser) {
|
|
|
|
|
globalScope.MediaBrowser = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalScope.MediaBrowser.ContentUploader = contentUploader;
|
|
|
|
|
|
|
|
|
|
})(this);
|