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

134 lines
3.9 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
var deferred = DeferredBuilder.Deferred();
if (!server.AccessToken && !server.ExchangeToken) {
Logger.log('Skipping sync to server ' + server.Id + ' because there is no saved authentication information.');
deferred.resolve();
2015-09-10 11:28:22 -07:00
return deferred.promise();
2015-09-09 10:49:44 -07:00
}
var connectionOptions = {
updateDateLastAccessed: false,
enableWebSocket: false,
reportCapabilities: false
};
connectionManager.connectToServer(server, connectionOptions).done(function (result) {
if (result.State == MediaBrowser.ConnectionState.SignedIn) {
2015-09-21 18:05:33 -07:00
performSync(server, options, deferred);
2015-09-09 10:49:44 -07:00
} else {
Logger.log('Unable to connect to server id: ' + server.Id);
deferred.reject();
}
}).fail(function () {
Logger.log('Unable to connect to server id: ' + server.Id);
deferred.reject();
});
return deferred.promise();
};
2015-09-21 18:05:33 -07:00
function performSync(server, options, deferred) {
2015-09-09 10:49:44 -07:00
Logger.log("Creating ContentUploader to server: " + server.Id);
2015-09-21 18:05:33 -07:00
var nextAction = function () {
2015-09-22 09:06:27 -07:00
syncOfflineUsers(server, options, deferred);
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-09-21 18:05:33 -07:00
nextAction();
return;
}
2015-09-09 10:49:44 -07:00
require(['contentuploader'], function () {
new MediaBrowser.ContentUploader(connectionManager).uploadImages(server).done(function () {
2015-09-09 20:22:52 -07:00
Logger.log("ContentUploaded succeeded to server: " + server.Id);
2015-09-21 18:05:33 -07:00
nextAction();
2015-09-09 20:22:52 -07:00
}).fail(function () {
Logger.log("ContentUploaded failed to server: " + server.Id);
2015-09-21 18:05:33 -07:00
nextAction();
2015-09-09 20:22:52 -07:00
});
});
}
2015-09-22 09:06:27 -07:00
function syncOfflineUsers(server, options, deferred) {
2015-09-09 20:22:52 -07:00
2015-10-01 09:28:24 -07:00
if (options.syncOfflineUsers === false) {
syncMedia(server, options, deferred);
return;
}
2015-09-09 20:22:52 -07:00
require(['offlineusersync'], function () {
var apiClient = connectionManager.getApiClient(server.Id);
2015-09-10 11:28:22 -07:00
new MediaBrowser.OfflineUserSync().sync(apiClient, server).done(function () {
2015-09-09 20:22:52 -07:00
Logger.log("OfflineUserSync succeeded to server: " + server.Id);
2015-09-22 09:06:27 -07:00
syncMedia(server, options, deferred);
2015-09-09 10:49:44 -07:00
}).fail(function () {
2015-09-09 20:22:52 -07:00
Logger.log("OfflineUserSync failed to server: " + server.Id);
deferred.reject();
});
});
}
2015-09-22 09:06:27 -07:00
function syncMedia(server, options, deferred) {
2015-09-09 20:22:52 -07:00
require(['mediasync'], function () {
var apiClient = connectionManager.getApiClient(server.Id);
2015-09-22 09:06:27 -07:00
new MediaBrowser.MediaSync().sync(apiClient, server, options).done(function () {
2015-09-09 20:22:52 -07:00
Logger.log("MediaSync succeeded to server: " + server.Id);
2015-09-09 10:49:44 -07:00
deferred.resolve();
2015-09-09 20:22:52 -07:00
}).fail(function () {
Logger.log("MediaSync failed to server: " + server.Id);
deferred.reject();
2015-09-09 10:49:44 -07:00
});
});
}
}
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
globalScope.MediaBrowser.ServerSync = serverSync;
})(this);