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

646 lines
23 KiB
JavaScript
Raw Normal View History

2016-12-10 00:16:15 -07:00
define(['localassetmanager'], function (localassetmanager) {
2016-10-01 23:13:04 -07:00
'use strict';
2015-09-09 20:22:52 -07:00
2016-12-10 00:16:15 -07:00
function processDownloadStatus(apiClient, serverInfo, options) {
2015-09-09 20:22:52 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin processDownloadStatus');
2015-09-09 20:22:52 -07:00
2016-12-24 12:15:27 -07:00
return localassetmanager.resyncTransfers().then(function () {
2015-09-09 20:22:52 -07:00
2016-12-24 12:15:27 -07:00
return localassetmanager.getServerItems(serverInfo.Id).then(function (items) {
2015-09-14 05:50:37 -07:00
2016-12-24 12:15:27 -07:00
console.log('[mediasync] Begin processDownloadStatus getServerItems completed');
2015-09-14 05:50:37 -07:00
2016-12-24 12:15:27 -07:00
var progressItems = items.filter(function (item) {
return item.SyncStatus === 'transferring' || item.SyncStatus === 'queued';
});
2015-09-14 05:50:37 -07:00
2016-12-24 12:15:27 -07:00
var p = Promise.resolve();
var cnt = 0;
progressItems.forEach(function (item) {
p = p.then(function () {
return reportTransfer(apiClient, item);
});
cnt++;
2016-08-06 19:56:32 -07:00
});
2015-09-14 12:47:03 -07:00
2016-12-24 12:15:27 -07:00
return p.then(function () {
console.log('[mediasync] Exit processDownloadStatus. Items reported: ' + cnt.toString());
return Promise.resolve();
});
2015-09-14 12:47:03 -07:00
});
2016-12-10 00:16:15 -07:00
});
}
function reportTransfer(apiClient, item) {
return localassetmanager.getItemFileSize(item.LocalPath).then(function (size) {
// The background transfer service on Windows leaves the file empty (size = 0) until it
// has been downloaded completely
if (size > 0) {
return apiClient.reportSyncJobItemTransferred(item.SyncJobItemId).then(function () {
item.SyncStatus = 'synced';
return localassetmanager.addOrUpdateLocalItem(item);
}, function (error) {
2016-12-24 12:15:27 -07:00
console.error('[mediasync] Mediasync error on reportSyncJobItemTransferred', error);
2016-12-10 00:16:15 -07:00
item.SyncStatus = 'error';
return localassetmanager.addOrUpdateLocalItem(item);
});
} else {
2016-12-24 12:15:27 -07:00
return localassetmanager.isDownloadFileInQueue(item.LocalPath).then(function (result) {
2016-12-10 00:16:15 -07:00
if (result) {
// just wait for completion
return Promise.resolve();
}
2015-09-14 05:50:37 -07:00
2016-12-24 12:15:27 -07:00
console.log('[mediasync] reportTransfer: Size is 0 and download no longer in queue. Deleting item.');
2016-12-10 00:16:15 -07:00
return localassetmanager.removeLocalItem(item).then(function () {
console.log('[mediasync] reportTransfer: Item deleted.');
return Promise.resolve();
}, function (err2) {
2016-12-24 12:15:27 -07:00
console.log('[mediasync] reportTransfer: Failed to delete item.', err2);
2016-12-10 00:16:15 -07:00
return Promise.resolve();
});
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
}, function (error) {
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
console.error('[mediasync] reportTransfer: error on getItemFileSize. Deleting item.', error);
return localassetmanager.removeLocalItem(item).then(function () {
console.log('[mediasync] reportTransfer: Item deleted.');
return Promise.resolve();
}, function (err2) {
console.log('[mediasync] reportTransfer: Failed to delete item.', error);
return Promise.resolve();
});
});
}
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
function reportOfflineActions(apiClient, serverInfo) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin reportOfflineActions');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.getUserActions(serverInfo.Id).then(function (actions) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
if (!actions.length) {
console.log('[mediasync] Exit reportOfflineActions (no actions)');
return Promise.resolve();
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return apiClient.reportOfflineActions(actions).then(function () {
2016-08-06 19:56:32 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.deleteUserActions(actions).then(function () {
console.log('[mediasync] Exit reportOfflineActions (actions reported and deleted.)');
return Promise.resolve();
});
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
}, function (err) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// delete those actions even on failure, because if the error is caused by
// the action data itself, this could otherwise lead to a situation that
// never gets resolved
console.error('[mediasync] error on apiClient.reportOfflineActions: ' + err.toString());
return localassetmanager.deleteUserActions(actions);
});
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
function syncData(apiClient, serverInfo, syncUserItemAccess) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin syncData');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.getServerItems(serverInfo.Id).then(function (items) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
var completedItems = items.filter(function (item) {
return (item) && ((item.SyncStatus === 'synced') || (item.SyncStatus === 'error'));
});
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
var request = {
TargetId: apiClient.deviceId(),
LocalItemIds: completedItems.map(function (xitem) { return xitem.ItemId; }),
OfflineUserIds: (serverInfo.Users || []).map(function (u) { return u.Id; })
};
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return apiClient.syncData(request).then(function (result) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return afterSyncData(apiClient, serverInfo, syncUserItemAccess, result).then(function () {
2016-12-24 12:15:27 -07:00
console.log('[mediasync] Exit syncData');
2016-12-10 00:16:15 -07:00
return Promise.resolve();
2016-12-24 12:15:27 -07:00
}, function (err) {
console.error('[mediasync] Error in syncData: ' + err.toString());
2016-12-10 00:16:15 -07:00
return Promise.resolve();
});
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
});
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
function afterSyncData(apiClient, serverInfo, enableSyncUserItemAccess, syncDataResult) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin afterSyncData');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
var p = Promise.resolve();
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
if (syncDataResult.ItemIdsToRemove) {
syncDataResult.ItemIdsToRemove.forEach(function (itemId) {
p = p.then(function () {
return removeLocalItem(itemId, serverInfo.Id);
});
});
2015-09-14 12:47:03 -07:00
}
2016-12-10 00:16:15 -07:00
if (enableSyncUserItemAccess) {
p = p.then(function () {
return syncUserItemAccess(syncDataResult, serverInfo.Id);
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return p.then(function () {
console.log('[mediasync] Exit afterSyncData');
return Promise.resolve();
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
function removeLocalItem(itemId, serverId) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin removeLocalItem');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.getLocalItem(serverId, itemId).then(function (item) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
if (item) {
return localassetmanager.removeLocalItem(item);
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return Promise.resolve();
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
});
}
2015-09-14 12:47:03 -07:00
2017-01-20 10:52:37 -07:00
function getNewMedia(apiClient, serverInfo, options, downloadCount) {
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin getNewMedia');
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
return apiClient.getReadySyncItems(apiClient.deviceId()).then(function (jobItems) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
var p = Promise.resolve();
2015-09-14 05:50:37 -07:00
2017-01-20 10:52:37 -07:00
var maxDownloads = 10;
var currentCount = downloadCount;
2016-12-10 00:16:15 -07:00
jobItems.forEach(function (jobItem) {
2017-01-20 10:52:37 -07:00
if (currentCount++ <= maxDownloads) {
p = p.then(function () {
return getNewItem(jobItem, apiClient, serverInfo, options);
});
}
2016-12-10 00:16:15 -07:00
});
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return p.then(function () {
console.log('[mediasync] Exit getNewMedia');
return Promise.resolve();
});
});
}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
function getNewItem(jobItem, apiClient, serverInfo, options) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin getNewItem');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
var libraryItem = jobItem.Item;
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.getLocalItem(serverInfo.Id, libraryItem.Id).then(function (existingItem) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
if (existingItem) {
if (existingItem.SyncStatus === 'queued' || existingItem.SyncStatus === 'transferring' || existingItem.SyncStatus === 'synced') {
2016-12-24 12:15:27 -07:00
console.log('[mediasync] getNewItem: getLocalItem found existing item');
2016-12-10 00:16:15 -07:00
return Promise.resolve();
}
2015-09-14 12:47:03 -07:00
}
2016-12-24 12:15:27 -07:00
libraryItem.CanDelete = false;
libraryItem.CanDownload = false;
2017-01-22 20:54:51 -07:00
libraryItem.SupportsSync = false;
2016-12-24 12:15:27 -07:00
libraryItem.People = [];
2017-01-22 20:54:51 -07:00
libraryItem.UserData = [];
2016-12-24 12:15:27 -07:00
libraryItem.SpecialFeatureCount = null;
2015-09-22 09:06:27 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.createLocalItem(libraryItem, serverInfo, jobItem).then(function (localItem) {
2015-09-14 12:47:03 -07:00
2016-12-24 12:15:27 -07:00
console.log('[mediasync] getNewItem: createLocalItem completed');
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
localItem.SyncStatus = 'queued';
2015-09-14 14:24:54 -07:00
2017-01-22 20:54:51 -07:00
return downloadParentItems(apiClient, jobItem, localItem, serverInfo, options).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-22 20:54:51 -07:00
return downloadMedia(apiClient, jobItem, localItem, options).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-22 20:54:51 -07:00
return getImages(apiClient, jobItem, localItem).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-22 20:54:51 -07:00
return getSubtitles(apiClient, jobItem, localItem);
});
2016-12-10 00:16:15 -07:00
});
});
});
});
}
2015-09-14 14:24:54 -07:00
2017-01-22 20:54:51 -07:00
function downloadParentItems(apiClient, jobItem, localItem, serverInfo, options) {
var p = Promise.resolve();
var libraryItem = localItem.Item;
var itemType = (libraryItem.Type || '').toLowerCase();
var logoImageTag = (libraryItem.ImageTags || {}).Logo;
switch (itemType) {
case 'episode':
if (libraryItem.SeriesId && libraryItem.SeriesId) {
p = p.then(function () {
return downloadItem(apiClient, libraryItem, libraryItem.SeriesId, serverInfo).then(function (seriesItem) {
libraryItem.SeriesLogoImageTag = (seriesItem.Item.ImageTags || {}).Logo;
return Promise.resolve();
});
});
}
if (libraryItem.SeasonId && libraryItem.SeasonId) {
p = p.then(function () {
return downloadItem(apiClient, libraryItem, libraryItem.SeasonId, serverInfo).then(function (seasonItem) {
libraryItem.SeasonPrimaryImageTag = (seasonItem.Item.ImageTags || {}).Primary;
return Promise.resolve();
});
});
}
break;
case 'audio':
case 'photo':
if (libraryItem.AlbumId && libraryItem.AlbumId) {
p = p.then(function () {
return downloadItem(apiClient, libraryItem, libraryItem.AlbumId, serverInfo);
});
}
break;
case 'video':
case 'movie':
case 'musicvideo':
// no parent item download for now
break;
}
return p;
}
function downloadItem(apiClient, libraryItem, itemId, serverInfo) {
return apiClient.getItem(apiClient.getCurrentUserId(), itemId).then(function (downloadedItem) {
downloadedItem.CanDelete = false;
downloadedItem.CanDownload = false;
downloadedItem.SupportsSync = false;
downloadedItem.People = [];
downloadedItem.UserData = {};
downloadedItem.SpecialFeatureCount = null;
downloadedItem.BackdropImageTags = null;
return localassetmanager.createLocalItem(downloadedItem, serverInfo, null).then(function (localItem) {
return localassetmanager.addOrUpdateLocalItem(localItem).then(function () {
return Promise.resolve(localItem);
});
});
}, function (err) {
console.error('[mediasync] downloadItem failed: ' + err.toString());
return Promise.resolve(null);
});
}
2016-12-10 00:16:15 -07:00
function downloadMedia(apiClient, jobItem, localItem, options) {
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
var url = apiClient.getUrl('Sync/JobItems/' + jobItem.SyncJobItemId + '/File', {
2016-12-10 00:16:15 -07:00
api_key: apiClient.accessToken()
});
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
var localPath = localItem.LocalPath;
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Downloading media. Url: ' + url + '. Local path: ' + localPath);
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
options = options || {};
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
return localassetmanager.downloadFile(url, localItem).then(function (filename) {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
localItem.SyncStatus = 'transferring';
return localassetmanager.addOrUpdateLocalItem(localItem);
});
}
2016-08-06 19:56:32 -07:00
2016-12-10 00:16:15 -07:00
function getImages(apiClient, jobItem, localItem) {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin getImages');
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
var p = Promise.resolve();
var libraryItem = localItem.Item;
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
var serverId = libraryItem.ServerId;
// case 0
2016-12-26 13:48:13 -07:00
var mainImageTag = (libraryItem.ImageTags || {}).Primary;
2016-12-24 12:15:27 -07:00
if (libraryItem.Id && mainImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.Id, mainImageTag, 'Primary');
});
}
// case 0a
2016-12-26 13:48:13 -07:00
var logoImageTag = (libraryItem.ImageTags || {}).Logo;
2016-12-24 12:15:27 -07:00
if (libraryItem.Id && logoImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.Id, logoImageTag, 'Logo');
});
}
// case 0b
2016-12-26 13:48:13 -07:00
var artImageTag = (libraryItem.ImageTags || {}).Art;
2016-12-24 12:15:27 -07:00
if (libraryItem.Id && artImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.Id, artImageTag, 'Art');
});
}
// case 0c
2016-12-26 13:48:13 -07:00
var bannerImageTag = (libraryItem.ImageTags || {}).Banner;
2016-12-24 12:15:27 -07:00
if (libraryItem.Id && bannerImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.Id, bannerImageTag, 'Banner');
});
}
// case 0d
2016-12-26 13:48:13 -07:00
var thumbImageTag = (libraryItem.ImageTags || {}).Thumb;
2016-12-24 12:15:27 -07:00
if (libraryItem.Id && thumbImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.Id, thumbImageTag, 'Thumb');
});
}
// Backdrops
2016-12-26 13:48:13 -07:00
//if (libraryItem.Id && libraryItem.BackdropImageTags) {
// for (var i = 0; i < libraryItem.BackdropImageTags.length; i++) {
// var backdropImageTag = libraryItem.BackdropImageTags[i];
// // use self-invoking function to simulate block-level variable scope
// (function (index, tag) {
// p = p.then(function () {
// return downloadImage(localItem, apiClient, serverId, libraryItem.Id, tag, 'backdrop', index);
// });
// })(i, backdropImageTag);
// }
//}
2016-12-24 12:15:27 -07:00
// case 1/2:
if (libraryItem.SeriesId && libraryItem.SeriesPrimaryImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.SeriesId, libraryItem.SeriesPrimaryImageTag, 'Primary');
});
2017-01-22 20:54:51 -07:00
}
if (libraryItem.SeriesId && libraryItem.SeriesThumbImageTag) {
2016-12-24 12:15:27 -07:00
p = p.then(function () {
2017-01-22 20:54:51 -07:00
return downloadImage(localItem, apiClient, serverId, libraryItem.SeriesId, libraryItem.SeriesThumbImageTag, 'Thumb');
});
}
if (libraryItem.SeriesId && libraryItem.SeriesLogoImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.SeriesId, libraryItem.SeriesLogoImageTag, 'Logo');
});
}
if (libraryItem.SeasonId && libraryItem.SeasonPrimaryImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.SeasonId, libraryItem.SeasonPrimaryImageTag, 'Primary');
2016-12-24 12:15:27 -07:00
});
}
// case 3:
if (libraryItem.AlbumId && libraryItem.AlbumPrimaryImageTag) {
p = p.then(function () {
return downloadImage(localItem, apiClient, serverId, libraryItem.AlbumId, libraryItem.AlbumPrimaryImageTag, 'Primary');
});
}
return p.then(function () {
console.log('[mediasync] Finished getImages');
return localassetmanager.addOrUpdateLocalItem(localItem);
}, function (err) {
console.log('[mediasync] Error getImages: ' + err.toString());
2017-01-20 10:52:37 -07:00
return Promise.resolve();
2016-12-24 12:15:27 -07:00
});
2016-12-10 00:16:15 -07:00
}
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
function downloadImage(localItem, apiClient, serverId, itemId, imageTag, imageType, index) {
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
index = index || 0;
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
return localassetmanager.hasImage(serverId, itemId, imageType, index).then(function (hasImage) {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
if (hasImage) {
2016-12-24 12:15:27 -07:00
console.log('[mediasync] downloadImage - skip existing: ' + itemId + ' ' + imageType + '_' + index.toString());
2016-12-10 00:16:15 -07:00
return Promise.resolve();
2015-09-14 14:24:54 -07:00
}
2017-01-22 20:54:51 -07:00
var maxWidth = 400;
if (imageType === 'backdrop') {
maxWidth = null;
}
var imageUrl = apiClient.getScaledImageUrl(itemId, {
2016-12-10 00:16:15 -07:00
tag: imageTag,
type: imageType,
2017-01-22 20:54:51 -07:00
maxWidth: maxWidth,
2016-12-10 00:16:15 -07:00
api_key: apiClient.accessToken()
});
2015-09-14 18:16:31 -07:00
2016-12-24 12:15:27 -07:00
console.log('[mediasync] downloadImage ' + itemId + ' ' + imageType + '_' + index.toString());
2017-01-20 10:52:37 -07:00
return localassetmanager.downloadImage(localItem, imageUrl, serverId, itemId, imageType, index).then(function (result) {
return Promise.resolve();
}, function (err) {
console.log('[mediasync] Error downloadImage: ' + err.toString());
return Promise.resolve();
});
2016-12-24 12:15:27 -07:00
}, function (err) {
console.log('[mediasync] Error downloadImage: ' + err.toString());
2017-01-20 10:52:37 -07:00
return Promise.resolve();
2016-12-10 00:16:15 -07:00
});
}
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
function getSubtitles(apiClient, jobItem, localItem) {
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin getSubtitles');
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
if (!jobItem.Item.MediaSources.length) {
2016-12-24 12:15:27 -07:00
console.log('[mediasync] Cannot download subtitles because video has no media source info.');
2016-12-10 00:16:15 -07:00
return Promise.resolve();
}
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
var files = jobItem.AdditionalFiles.filter(function (f) {
return f.Type === 'Subtitles';
});
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
var mediaSource = jobItem.Item.MediaSources[0];
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
var p = Promise.resolve();
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
files.forEach(function (file) {
p = p.then(function () {
return getItemSubtitle(file, apiClient, jobItem, localItem, mediaSource);
2015-09-14 18:16:31 -07:00
});
2016-12-10 00:16:15 -07:00
});
2015-09-14 18:16:31 -07:00
2016-12-10 00:16:15 -07:00
return p.then(function () {
console.log('[mediasync] Exit getSubtitles');
return Promise.resolve();
});
}
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
function getItemSubtitle(file, apiClient, jobItem, localItem, mediaSource) {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
console.log('[mediasync] Begin getItemSubtitle');
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
var subtitleStream = mediaSource.MediaStreams.filter(function (m) {
return m.Type === 'Subtitle' && m.Index === file.Index;
})[0];
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
if (!subtitleStream) {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
// We shouldn't get in here, but let's just be safe anyway
2016-12-24 12:15:27 -07:00
console.log('[mediasync] Cannot download subtitles because matching stream info was not found.');
2016-12-10 00:16:15 -07:00
return Promise.resolve();
2015-09-14 14:24:54 -07:00
}
2016-12-24 12:15:27 -07:00
var url = apiClient.getUrl('Sync/JobItems/' + jobItem.SyncJobItemId + '/AdditionalFiles', {
2016-12-10 00:16:15 -07:00
Name: file.Name,
api_key: apiClient.accessToken()
});
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
var fileName = localassetmanager.getSubtitleSaveFileName(localItem, jobItem.OriginalFileName, subtitleStream.Language, subtitleStream.IsForced, subtitleStream.Codec);
return localassetmanager.downloadSubtitles(url, fileName).then(function (subtitlePath) {
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
if (localItem.AdditionalFiles) {
localItem.AdditionalFiles.forEach(function (item) {
if (item.Name === file.Name) {
item.Path = subtitlePath;
}
});
}
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
subtitleStream.Path = subtitlePath;
return localassetmanager.addOrUpdateLocalItem(localItem);
});
}
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
return function () {
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
var self = this;
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
self.sync = function (apiClient, serverInfo, options) {
2015-09-14 14:24:54 -07:00
2016-12-24 12:15:27 -07:00
console.log('[mediasync]************************************* Start sync');
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
return processDownloadStatus(apiClient, serverInfo, options).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-20 10:52:37 -07:00
return localassetmanager.getDownloadItemCount().then(function (downloadCount) {
2016-12-24 12:15:27 -07:00
2017-01-20 10:52:37 -07:00
if (options.syncCheckProgressOnly === true && downloadCount > 2) {
return Promise.resolve();
}
2015-09-14 14:24:54 -07:00
2017-01-20 10:52:37 -07:00
return reportOfflineActions(apiClient, serverInfo).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-20 10:52:37 -07:00
// Download new content
return getNewMedia(apiClient, serverInfo, options, downloadCount).then(function () {
2015-09-14 14:24:54 -07:00
2017-01-20 10:52:37 -07:00
// Do the second data sync
return syncData(apiClient, serverInfo, false).then(function () {
console.log('[mediasync]************************************* Exit sync');
return Promise.resolve();
});
2016-12-10 00:16:15 -07:00
});
2017-01-20 10:52:37 -07:00
//});
2016-12-10 00:16:15 -07:00
});
});
2017-01-20 10:52:37 -07:00
}, function (err) {
console.error(err.toString());
2015-09-14 14:24:54 -07:00
});
2016-12-10 00:16:15 -07:00
};
};
2015-09-14 14:24:54 -07:00
2016-12-10 00:16:15 -07:00
//function syncUserItemAccess(syncDataResult, serverId) {
// console.log('[mediasync] Begin syncUserItemAccess');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// var itemIds = [];
// for (var id in syncDataResult.ItemUserAccess) {
// itemIds.push(id);
// }
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// return syncNextUserAccessForItem(itemIds, 0, syncDataResult, serverId);
//}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
//function syncNextUserAccessForItem(itemIds, index, syncDataResult, serverId) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// var length = itemIds.length;
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// if (index >= length) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// return Promise.resolve
// return;
// }
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// syncUserAccessForItem(itemIds[index], syncDataResult).then(function () {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// syncNextUserAccessForItem(itemIds, index + 1, syncDataResult, serverId, deferred);
// }, function () {
// syncNextUserAccessForItem(itemIds, index + 1, syncDataResult, serverId, deferred);
// });
//}
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
//function syncUserAccessForItem(itemId, syncDataResult) {
// console.log('[mediasync] Begin syncUserAccessForItem');
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// localassetmanager.getUserIdsWithAccess(itemId, serverId).then(function (savedUserIdsWithAccess) {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// var userIdsWithAccess = syncDataResult.ItemUserAccess[itemId];
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// if (userIdsWithAccess.join(',') === savedUserIdsWithAccess.join(',')) {
// // Hasn't changed, nothing to do
// deferred.resolve();
// }
// else {
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// localassetmanager.saveUserIdsWithAccess(itemId, serverId, userIdsWithAccess).then(function () {
// deferred.resolve();
// }, getOnFail(deferred));
// }
2015-09-14 12:47:03 -07:00
2016-12-10 00:16:15 -07:00
// }, getOnFail(deferred));
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
// return deferred.promise();
//}
2015-09-14 05:50:37 -07:00
2016-12-10 00:16:15 -07:00
//}
2015-09-14 05:50:37 -07:00
2016-08-06 19:56:32 -07:00
});