jellyfin-web/dashboard-ui/bower_components/emby-apiclient/sync/contentuploader.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-08-06 19:56:32 -07:00
define(['localassetmanager'], function (localAssetManager) {
2016-10-01 23:13:04 -07:00
'use strict';
2015-09-09 10:49:44 -07:00
2016-08-06 19:56:32 -07:00
return function (connectionManager) {
2015-09-09 10:49:44 -07:00
2015-09-09 20:22:52 -07:00
var self = this;
2015-09-09 10:49:44 -07:00
self.uploadImages = function (server) {
2016-08-06 19:56:32 -07:00
return LocalAssetManager.getCameraPhotos().then(function (photos) {
2015-09-09 10:49:44 -07:00
2015-09-28 20:35:50 -07:00
if (!photos.length) {
2016-08-06 19:56:32 -07:00
return Promise.resolve();
2015-09-28 20:35:50 -07:00
}
2015-09-09 10:49:44 -07:00
2015-09-28 20:35:50 -07:00
var apiClient = connectionManager.getApiClient(server.Id);
2015-09-09 10:49:44 -07:00
2016-08-06 19:56:32 -07:00
return apiClient.getContentUploadHistory().then(function (uploadHistory) {
2015-09-10 11:28:22 -07:00
photos = getFilesToUpload(photos, uploadHistory);
2015-12-23 10:46:01 -07:00
console.log('Found ' + photos.length + ' files to upload');
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
return new Promise(function (resolve, reject) {
uploadNext(photos, 0, server, apiClient, resolve, reject);
});
2015-09-10 11:28:22 -07:00
2015-12-14 08:43:03 -07:00
}, function () {
2016-08-06 19:56:32 -07:00
return Promise.resolve();
2015-09-10 11:28:22 -07:00
});
2015-09-28 20:35:50 -07:00
2015-09-10 11:28:22 -07:00
});
2015-09-28 20:35:50 -07:00
};
2015-09-10 11:28:22 -07:00
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) {
2016-10-01 23:13:04 -07:00
return getUploadId(file) === u.Id;
2015-09-10 11:28:22 -07:00
2016-10-01 23:13:04 -07:00
}).length === 0;
2015-09-10 11:28:22 -07:00
});
}
function getUploadId(file) {
return CryptoJS.SHA1(file + "1").toString();
}
2016-08-06 19:56:32 -07:00
function uploadNext(files, index, server, apiClient, resolve, reject) {
2015-09-10 11:28:22 -07:00
var length = files.length;
if (index >= length) {
2015-09-09 10:49:44 -07:00
2016-08-06 19:56:32 -07:00
resolve();
2015-09-10 11:28:22 -07:00
return;
}
2015-12-14 08:43:03 -07:00
uploadFile(files[index], apiClient).then(function () {
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
uploadNext(files, index + 1, server, apiClient, resolve, reject);
2015-12-14 08:43:03 -07:00
}, function () {
2016-08-06 19:56:32 -07:00
uploadNext(files, index + 1, server, apiClient, resolve, reject);
2015-09-10 11:28:22 -07:00
});
}
function uploadFile(file, apiClient) {
2016-08-06 19:56:32 -07:00
return new Promise(function (resolve, reject) {
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
require(['fileupload', "cryptojs-sha1"], function (FileUpload) {
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
var name = 'camera image ' + new Date().getTime();
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
var url = apiClient.getUrl('Devices/CameraUploads', {
DeviceId: apiClient.deviceId(),
Name: name,
Album: 'Camera Roll',
Id: getUploadId(file),
api_key: apiClient.accessToken()
});
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
console.log('Uploading file to ' + url);
2015-09-10 11:28:22 -07:00
2016-08-06 19:56:32 -07:00
new FileUpload().upload(file, name, url).then(resolve, reject);
2015-09-10 11:28:22 -07:00
});
2015-09-09 10:49:44 -07:00
});
}
2016-08-06 19:56:32 -07:00
};
});