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

507 lines
15 KiB
JavaScript
Raw Normal View History

2015-09-09 20:22:52 -07:00
(function (globalScope) {
function mediaSync() {
var self = this;
2015-09-14 12:47:03 -07:00
self.sync = function (apiClient, serverInfo) {
2015-09-09 20:22:52 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
reportOfflineActions(apiClient, serverInfo).done(function () {
2015-09-14 05:50:37 -07:00
// Do the first data sync
2015-09-14 12:47:03 -07:00
syncData(apiClient, serverInfo, false).done(function () {
2015-09-14 05:50:37 -07:00
// Download new content
2015-09-14 14:24:54 -07:00
getNewMedia(apiClient, serverInfo).done(function () {
2015-09-14 05:50:37 -07:00
// Do the second data sync
2015-09-14 12:47:03 -07:00
syncData(apiClient, serverInfo, false).done(function () {
2015-09-14 05:50:37 -07:00
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
2015-09-09 20:22:52 -07:00
return deferred.promise();
};
2015-09-14 05:50:37 -07:00
2015-09-14 12:47:03 -07:00
function reportOfflineActions(apiClient, serverInfo) {
Logger.log('Begin reportOfflineActions');
2015-09-14 05:50:37 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
require(['localassetmanager'], function () {
LocalAssetManager.getOfflineActions(serverInfo.Id).done(function (actions) {
if (!actions.length) {
deferred.resolve();
return;
}
apiClient.reportOfflineActions(actions).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
});
2015-09-14 05:50:37 -07:00
return deferred.promise();
}
2015-09-14 12:47:03 -07:00
function syncData(apiClient, serverInfo, syncUserItemAccess) {
Logger.log('Begin syncData');
2015-09-14 05:50:37 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
require(['localassetmanager'], function () {
LocalAssetManager.getServerItemIds(serverInfo.Id).done(function (localIds) {
var request = {
TargetId: apiClient.deviceId(),
LocalItemIds: localIds,
OfflineUserIds: (serverInfo.Users || []).map(function (u) { return u.Id; })
};
apiClient.syncData(request).done(function (result) {
afterSyncData(apiClient, serverInfo, syncUserItemAccess, result, deferred);
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
});
return deferred.promise();
}
function afterSyncData(apiClient, serverInfo, enableSyncUserItemAccess, syncDataResult, deferred) {
Logger.log('Begin afterSyncData');
removeLocalItems(syncDataResult, serverInfo.Id).done(function (result) {
if (enableSyncUserItemAccess) {
syncUserItemAccess(syncDataResult, serverInfo.Id).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}
else {
deferred.resolve();
}
}).fail(getOnFail(deferred));
2015-09-14 05:50:37 -07:00
deferred.resolve();
2015-09-14 12:47:03 -07:00
}
function removeLocalItems(syncDataResult, serverId) {
Logger.log('Begin removeLocalItems');
var deferred = DeferredBuilder.Deferred();
removeNextLocalItem(syncDataResult.ItemIdsToRemove, 0, serverId, deferred);
return deferred.promise();
}
function removeNextLocalItem(itemIdsToRemove, index, serverId, deferred) {
var length = itemIdsToRemove.length;
if (index >= length) {
deferred.resolve();
return;
}
removeLocalItem(itemIdsToRemove[index], serverId).done(function () {
removeNextLocalItem(itemIdsToRemove, index + 1, serverId, deferred);
}).fail(function () {
removeNextLocalItem(itemIdsToRemove, index + 1, serverId, deferred);
});
}
function removeLocalItem(itemId, serverId) {
Logger.log('Begin removeLocalItem');
var deferred = DeferredBuilder.Deferred();
require(['localassetmanager'], function () {
LocalAssetManager.removeLocalItem(itemId, serverId).done(function (localIds) {
deferred.resolve();
}).fail(getOnFail(deferred));
});
2015-09-14 05:50:37 -07:00
return deferred.promise();
}
2015-09-14 14:24:54 -07:00
function getNewMedia(apiClient, serverInfo) {
2015-09-14 05:50:37 -07:00
2015-09-14 12:47:03 -07:00
Logger.log('Begin getNewMedia');
2015-09-14 05:50:37 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
apiClient.getReadySyncItems(apiClient.deviceId()).done(function (jobItems) {
2015-09-14 14:24:54 -07:00
getNextNewItem(jobItems, 0, apiClient, serverInfo, deferred);
2015-09-14 12:47:03 -07:00
}).fail(getOnFail(deferred));
return deferred.promise();
}
2015-09-14 14:24:54 -07:00
function getNextNewItem(jobItems, index, apiClient, serverInfo, deferred) {
2015-09-14 12:47:03 -07:00
var length = jobItems.length;
if (index >= length) {
deferred.resolve();
return;
}
2015-09-14 14:24:54 -07:00
getNewItem(jobItems[index], apiClient, serverInfo).done(function () {
2015-09-14 12:47:03 -07:00
2015-09-14 14:24:54 -07:00
getNextNewItem(jobItems, index + 1, apiClient, serverInfo, deferred);
2015-09-14 12:47:03 -07:00
}).fail(function () {
2015-09-14 14:24:54 -07:00
getNextNewItem(jobItems, index + 1, apiClient, serverInfo, deferred);
2015-09-14 12:47:03 -07:00
});
}
2015-09-14 14:24:54 -07:00
function getNewItem(jobItem, apiClient, serverInfo) {
Logger.log('Begin getNewItem');
var deferred = DeferredBuilder.Deferred();
require(['localassetmanager'], function () {
var libraryItem = jobItem.Item;
2015-09-14 21:31:12 -07:00
LocalAssetManager.createLocalItem(libraryItem, serverInfo, jobItem.OriginalFileName).done(function (localItem) {
2015-09-14 14:24:54 -07:00
downloadMedia(apiClient, jobItem, localItem).done(function () {
getImages(apiClient, jobItem, localItem).done(function () {
getSubtitles(apiClient, jobItem, localItem).done(function () {
apiClient.reportSyncJobItemTransferred(jobItem.SyncJobItemId).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
});
return deferred.promise();
}
function downloadMedia(apiClient, jobItem, localItem) {
var deferred = DeferredBuilder.Deferred();
require(['localassetmanager'], function () {
var url = apiClient.getUrl("Sync/JobItems/" + jobItem.SyncJobItemId + "/File");
var localPath = localItem.LocalPath;
Logger.log('Downloading media. Url: ' + url + '. Local path: ' + localPath);
localAssetManager.downloadFile(url, localPath).done(function () {
localAssetManager.addOrUpdateLocalItem(localItem).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
});
return deferred.promise();
}
function getImages(apiClient, jobItem, localItem) {
var deferred = DeferredBuilder.Deferred();
getNextImage(0, apiClient, localItem, deferred);
return deferred.promise();
}
function getNextImage(index, apiClient, localItem, deferred) {
if (index >= 4) {
deferred.resolve();
return;
}
var libraryItem = item.Item;
var serverId = libraryItem.ServerId;
var itemId = null;
var imageTag = null;
var imageType = "Primary";
switch (index) {
case 0:
itemId = libraryItem.Id;
imageType = "Primary";
imageTag = (libraryItem.ImageTags || {})["Primary"];
break;
case 1:
itemId = libraryItem.SeriesId;
imageType = "Primary";
imageTag = libraryItem.SeriesPrimaryImageTag;
break;
case 2:
itemId = libraryItem.SeriesId;
imageType = "Thumb";
imageTag = libraryItem.SeriesPrimaryImageTag;
break;
case 3:
itemId = libraryItem.AlbumId;
imageType = "Primary";
imageTag = libraryItem.AlbumPrimaryImageTag;
break;
default:
break;
}
if (!itemId) {
deferred.resolve();
return;
}
if (!imageTag) {
getNextImage(index + 1, apiClient, localItem, deferred);
return;
}
downloadImage(apiClient, serverId, itemId, imageTag, imageType).done(function () {
getNextImage(index + 1, apiClient, localItem, deferred);
}).fail(getOnFail(deferred));
}
function downloadImage(apiClient, serverId, itemId, imageTag, imageType) {
2015-09-14 18:16:31 -07:00
Logger.log('Begin downloadImage');
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 18:16:31 -07:00
require(['localassetmanager'], function () {
localAssetManager.hasImage(serverId, itemId, imageTag).done(function (hasImage) {
if (hasImage) {
deferred.resolve();
return;
}
var imageUrl = apiClient.getImageUrl(itemId, {
Tag: imageTag,
ImageType: imageType
});
localAssetManager.downloadImage(imageUrl, serverId, itemId, imageTag).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
});
});
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
2015-09-14 14:24:54 -07:00
function getSubtitles(apiClient, jobItem, localItem) {
2015-09-14 18:16:31 -07:00
Logger.log('Begin getSubtitles');
2015-09-14 14:24:54 -07:00
var deferred = DeferredBuilder.Deferred();
require(['localassetmanager'], function () {
if (!jobItem.Item.MediaSources.length) {
logger.Error("Cannot download subtitles because video has no media source info.");
deferred.resolve();
return;
}
var files = jobItem.AdditionalFiles.filter(function (f) {
return f.Type == 'Subtitles';
});
var mediaSource = jobItem.Item.MediaSources[0];
getNextSubtitle(files, 0, apiClient, jobItem, localItem, mediaSource);
});
return deferred.promise();
}
function getNextSubtitle(files, index, apiClient, jobItem, localItem, mediaSource) {
var length = files.length;
if (index >= length) {
deferred.resolve();
return;
}
getItemSubtitle(file, apiClient, jobItem, localItem, mediaSource).done(function () {
getNextSubtitle(files, index + 1, apiClient, jobItem, localItem, mediaSource);
}).fail(function () {
getNextSubtitle(files, index + 1, apiClient, jobItem, localItem, mediaSource);
});
}
function getItemSubtitle(file, apiClient, jobItem, localItem, mediaSource) {
Logger.log('Begin getItemSubtitle');
var deferred = DeferredBuilder.Deferred();
var subtitleStream = mediaSource.MediaStreams.filter(function (m) {
return m.Type == 'Subtitle' && m.Index == file.Index;
})[0];
if (!subtitleStream) {
// We shouldn't get in here, but let's just be safe anyway
Logger.log("Cannot download subtitles because matching stream info wasn't found.");
deferred.reject();
return;
}
var url = apiClient.getUrl("Sync/JobItems/" + jobItem.SyncJobItemId + "/AdditionalFiles", {
Name: file.Name
});
require(['localassetmanager'], function () {
LocalAssetManager.downloadSubtitles(url, localItem, subtitleStream).done(function (subtitlePath) {
subtitleStream.Path = subtitlePath;
localAssetManager.addOrUpdateLocalItem(localItem).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
});
return deferred.promise();
}
2015-09-14 12:47:03 -07:00
function syncUserItemAccess(syncDataResult, serverId) {
Logger.log('Begin syncUserItemAccess');
var deferred = DeferredBuilder.Deferred();
var itemIds = [];
for (var id in syncDataResult.ItemUserAccess) {
itemIds.push(id);
}
syncNextUserAccessForItem(itemIds, 0, syncDataResult, serverId, deferred);
return deferred.promise();
}
function syncNextUserAccessForItem(itemIds, index, syncDataResult, serverId, deferred) {
var length = itemIds.length;
if (index >= length) {
deferred.resolve();
return;
}
syncUserAccessForItem(itemIds[index], syncDataResult).done(function () {
syncNextUserAccessForItem(itemIds, index + 1, syncDataResult, serverId, deferred);
}).fail(function () {
syncNextUserAccessForItem(itemIds, index + 1, syncDataResult, serverId, deferred);
});
}
function syncUserAccessForItem(itemId, syncDataResult) {
Logger.log('Begin syncUserAccessForItem');
var deferred = DeferredBuilder.Deferred();
require(['localassetmanager'], function () {
LocalAssetManager.getLocalItem(itemId, serverId).done(function (localItem) {
var userIdsWithAccess = syncDataResult.ItemUserAccess[itemId];
if (userIdsWithAccess.join(',') == localItem.UserIdsWithAccess.join(',')) {
// Hasn't changed, nothing to do
deferred.resolve();
}
else {
localItem.UserIdsWithAccess = userIdsWithAccess;
localAssetManager.addOrUpdateLocalItem(localItem).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}
}).fail(getOnFail(deferred));
});
2015-09-14 05:50:37 -07:00
return deferred.promise();
}
function getOnFail(deferred) {
return function () {
deferred.reject();
};
}
2015-09-09 20:22:52 -07:00
}
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
globalScope.MediaBrowser.MediaSync = mediaSync;
})(this);