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

135 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-09-09 10:49:44 -07:00
(function (globalScope) {
function serverSync(connectionManager) {
2015-09-09 20:22:52 -07:00
var self = this;
2015-09-21 18:05:33 -07:00
self.sync = function (server, options) {
2015-09-09 10:49:44 -07:00
if (!server.AccessToken && !server.ExchangeToken) {
2015-12-16 14:27:19 -07:00
console.log('Skipping sync to server ' + server.Id + ' because there is no saved authentication information.');
return new Promise(function (resolve, reject) {
resolve();
});
2015-09-09 10:49:44 -07:00
}
var connectionOptions = {
updateDateLastAccessed: false,
enableWebSocket: false,
reportCapabilities: false
};
2015-12-16 14:27:19 -07:00
return connectionManager.connectToServer(server, connectionOptions).then(function (result) {
2015-09-09 10:49:44 -07:00
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
2015-12-16 14:27:19 -07:00
return performSync(server, options);
2015-09-09 10:49:44 -07:00
} else {
2015-12-16 14:27:19 -07:00
console.log('Unable to connect to server id: ' + server.Id);
return Promise.reject();
2015-09-09 10:49:44 -07:00
}
2015-12-16 14:27:19 -07:00
}, function (err) {
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
console.log('Unable to connect to server id: ' + server.Id);
throw err;
2015-09-09 10:49:44 -07:00
});
};
2015-12-16 14:27:19 -07:00
function performSync(server, options) {
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
console.log("Creating ContentUploader to server: " + server.Id);
2015-09-21 18:05:33 -07:00
options = options || {};
2015-09-26 07:51:26 -07:00
var uploadPhotos = options.uploadPhotos !== false;
if (options.cameraUploadServers && options.cameraUploadServers.indexOf(server.Id) == -1) {
uploadPhotos = false;
}
if (!uploadPhotos) {
2015-12-16 14:27:19 -07:00
return syncOfflineUsers(server, options);
2015-09-21 18:05:33 -07:00
}
2015-12-16 14:27:19 -07:00
return new Promise(function (resolve, reject) {
require(['contentuploader'], function () {
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
new MediaBrowser.ContentUploader(connectionManager).uploadImages(server).then(function () {
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
console.log("ContentUploaded succeeded to server: " + server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
syncOfflineUsers(server, options).then(resolve, reject);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
}, function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
console.log("ContentUploaded failed to server: " + server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
syncOfflineUsers(server, options).then(resolve, reject);
});
2015-09-09 20:22:52 -07:00
});
});
}
2015-12-16 14:27:19 -07:00
function syncOfflineUsers(server, options) {
2015-09-09 20:22:52 -07:00
2015-10-01 09:28:24 -07:00
if (options.syncOfflineUsers === false) {
2015-12-16 14:27:19 -07:00
return syncMedia(server, options);
2015-10-01 09:28:24 -07:00
}
2015-12-16 14:27:19 -07:00
return new Promise(function (resolve, reject) {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
require(['offlineusersync'], function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
var apiClient = connectionManager.getApiClient(server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
new MediaBrowser.OfflineUserSync().sync(apiClient, server).then(function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
console.log("OfflineUserSync succeeded to server: " + server.Id);
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
syncMedia(server, options).then(resolve, reject);
2015-09-09 10:49:44 -07:00
2015-12-16 14:27:19 -07:00
}, function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
console.log("OfflineUserSync failed to server: " + server.Id);
reject();
});
2015-09-09 20:22:52 -07:00
});
});
}
2015-12-16 14:27:19 -07:00
function syncMedia(server, options) {
return new Promise(function (resolve, reject) {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
require(['mediasync'], function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
var apiClient = connectionManager.getApiClient(server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
new MediaBrowser.MediaSync().sync(apiClient, server, options).then(function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
console.log("MediaSync succeeded to server: " + server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
resolve();
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
}, function () {
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
console.log("MediaSync failed to server: " + server.Id);
2015-09-09 20:22:52 -07:00
2015-12-16 14:27:19 -07:00
reject();
});
2015-09-09 10:49:44 -07:00
});
});
}
}
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
globalScope.MediaBrowser.ServerSync = serverSync;
})(this);