jellyfin-web/dashboard-ui/cordova/localassetmanager.js

749 lines
21 KiB
JavaScript
Raw Normal View History

2015-09-09 10:49:44 -07:00
(function () {
function getLocalMediaSource(serverId, itemId) {
2015-09-14 12:47:03 -07:00
2015-09-15 11:09:44 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 12:47:03 -07:00
// android
if (window.ApiClientBridge) {
var json = ApiClientBridge.getLocalMediaSource(serverId, itemId);
if (json) {
2015-09-15 11:09:44 -07:00
deferred.resolveWith(null, [JSON.parse(json)]);
2015-09-14 12:47:03 -07:00
}
2015-09-15 11:09:44 -07:00
else {
deferred.resolveWith(null, [null]);
}
return deferred.promise();
2015-09-14 12:47:03 -07:00
}
2015-09-14 21:31:12 -07:00
getLocalItem(itemId, serverId).done(function (localItem) {
2015-09-20 09:16:06 -07:00
if (localItem && localItem.Item.MediaSources.length) {
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
var mediaSource = localItem.Item.MediaSources[0];
2015-09-14 21:31:12 -07:00
fileExists(mediaSource.Path).done(function (exists) {
if (exists) {
deferred.resolveWith(null, [mediaSource]);
}
else {
deferred.resolveWith(null, [null]);
}
}).fail(getOnFail(deferred));
return;
}
deferred.resolveWith(null, [null]);
}).fail(getOnFail(deferred));
2015-09-10 11:28:22 -07:00
return deferred.promise();
}
function getCameraPhotos() {
var deferred = DeferredBuilder.Deferred();
if (window.CameraRoll) {
var photos = [];
CameraRoll.getPhotos(function (result) {
photos.push(result);
});
setTimeout(function () {
// clone the array in case the callback is still getting called
Logger.log('Found ' + photos.length + ' in camera roll');
deferred.resolveWith(null, [photos]);
}, 2000);
} else {
deferred.resolveWith(null, [[]]);
}
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
var offlineUserDatabase;
function getOfflineUserdb(callback) {
if (offlineUserDatabase) {
callback(offlineUserDatabase);
return;
}
// Create/open database
offlineUserDatabase = window.sqlitePlugin.openDatabase({ name: "offlineusers.db" });
offlineUserDatabase.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS users (id text primary key, data text)');
tx.executeSql('create index if not exists idx_users on users(id)');
callback(offlineUserDatabase);
});
}
2015-09-14 21:31:12 -07:00
function saveOfflineUser(user) {
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
getOfflineUserdb(function (db) {
db.transaction(function (tx) {
tx.executeSql("REPLACE INTO offlineusers (id, data) VALUES (?,?)", [user.Id, JSON.stringify(user)], function (tx, res) {
deferred.resolve();
}, function (e) {
deferred.reject();
});
});
});
return deferred.promise();
}
function deleteOfflineUser(id) {
var deferred = DeferredBuilder.Deferred();
getOfflineUserdb(function (db) {
db.transaction(function (tx) {
tx.executeSql("DELETE from offlineusers where id=?", [user.Id], function (tx, res) {
deferred.resolve();
}, function (e) {
deferred.reject();
});
});
});
2015-09-14 21:31:12 -07:00
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
var offlineActionsDatabase;
function getOfflineActionsDb(callback) {
if (offlineActionsDatabase) {
callback(offlineActionsDatabase);
return;
}
// Create/open database
offlineActionsDatabase = window.sqlitePlugin.openDatabase({ name: "offlineactions.db" });
offlineActionsDatabase.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS offlineactions (Id text primary key, ServerId text not null, Json text not null)');
tx.executeSql('create index if not exists idx_offlineactions on offlineactions(id)');
callback(offlineActionsDatabase);
});
}
2015-09-14 12:47:03 -07:00
function getOfflineActions(serverId) {
2015-09-15 20:55:26 -07:00
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
getOfflineActionsDb(function (db) {
db.transaction(function (tx) {
2015-09-20 09:16:06 -07:00
tx.executeSql("SELECT Json from offlineactions where ServerId=?", [serverId], function (tx, res) {
2015-09-15 20:55:26 -07:00
var actions = [];
for (var i = 0, length = res.rows.length; i < length; i++) {
2015-09-20 09:16:06 -07:00
actions.push(JSON.parse(res.rows.item(i).Json));
2015-09-15 20:55:26 -07:00
}
deferred.resolveWith(null, [actions]);
}, function (e) {
deferred.reject();
});
});
});
return deferred.promise();
}
function deleteOfflineActions(actions) {
var ids = actions.map(function (a) { return "'" + a.Id + "'"; }).join(',');
var deferred = DeferredBuilder.Deferred();
getOfflineActionsDb(function (db) {
db.transaction(function (tx) {
tx.executeSql("DELETE from offlineactions where Id in (" + ids + ")", [], function (tx, res) {
deferred.resolve();
}, function (e) {
deferred.reject();
});
});
});
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
var offlineItemsDatabase;
function getOfflineItemsDb(callback) {
if (offlineItemsDatabase) {
callback(offlineItemsDatabase);
return;
}
// Create/open database
offlineItemsDatabase = window.sqlitePlugin.openDatabase({ name: "offlineitems.db" });
offlineItemsDatabase.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Items ( Id text primary key, ItemId text not null, ItemType text not null, MediaType text, ServerId text not null, LocalPath text not null, UserIdsWithAccess text, AlbumId text, AlbumName text, SeriesId text, SeriesName text, Json text not null)');
tx.executeSql('create index if not exists idx_items on Items(Id)');
tx.executeSql('CREATE TABLE IF NOT EXISTS AlbumArtists ( Id text not null, Name text not null, ItemId text not null)');
tx.executeSql('create index if not exists idx_AlbumArtists on AlbumArtists(id)');
callback(offlineItemsDatabase);
});
}
2015-09-14 12:47:03 -07:00
function getServerItemIds(serverId) {
2015-09-15 20:55:26 -07:00
var deferred = DeferredBuilder.Deferred();
getOfflineItemsDb(function (db) {
db.transaction(function (tx) {
tx.executeSql("SELECT ItemId from Items where ServerId=?", [serverId], function (tx, res) {
var itemIds = [];
for (var i = 0, length = res.rows.length; i < length; i++) {
itemIds.push(res.rows.item(i).ItemId);
}
deferred.resolveWith(null, [itemIds]);
}, function (e) {
deferred.reject();
});
});
});
return deferred.promise();
}
function getLocalItem(itemId, serverId) {
var deferred = DeferredBuilder.Deferred();
getOfflineItemsDb(function (db) {
db.transaction(function (tx) {
tx.executeSql("SELECT Json from Items where itemId=? AND serverId=?", [itemId, serverId], function (tx, res) {
if (res.rows.length) {
var localItem = JSON.parse(res.rows.item(0).Json);
2015-09-20 09:16:06 -07:00
deferred.resolveWith(null, [localItem]);
2015-09-15 20:55:26 -07:00
}
else {
deferred.resolveWith(null, [null]);
}
}, function (e) {
deferred.reject();
});
});
});
return deferred.promise();
}
function addOrUpdateLocalItem(item) {
Logger.log('addOrUpdateLocalItem');
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
getOfflineItemsDb(function (db) {
db.transaction(function (tx) {
var libraryItem = item.Item || {};
var values = [item.Id, item.ItemId, libraryItem.Type, libraryItem.MediaType, item.ServerId, item.LocalPath, (item.UserIdsWithAccess || []).join(','), libraryItem.AlbumId, libraryItem.AlbumName, libraryItem.SeriesId, libraryItem.SeriesName, JSON.stringify(item)];
2015-09-15 20:55:26 -07:00
tx.executeSql("REPLACE INTO Items (Id, ItemId, ItemType, MediaType, ServerId, LocalPath, UserIdsWithAccess, AlbumId, AlbumName, SeriesId, SeriesName, Json) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", values);
deferred.resolve();
2015-09-15 20:55:26 -07:00
});
});
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
function removeLocalItem(itemId, serverId) {
2015-09-15 20:55:26 -07:00
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
getLocalItem(itemId, serverId).done(function (item) {
getOfflineItemsDb(function (db) {
db.transaction(function (tx) {
tx.executeSql("DELETE from Items where itemId=? AND serverId=?", [itemId, serverId]);
var files = item.AdditionalFiles || [];
files.push(item.LocalPath);
deleteFiles(files).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
});
});
}).fail(getOnFail(deferred));
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
function deleteFiles(files) {
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
deleteNextFile(files, 0, deferred);
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
function deleteNextFile(files, index, deferred) {
if (index >= files.length) {
deferred.resolve();
return;
}
deleteFile(file).done(function () {
deleteNextFile(files, index + 1, deferred);
}).fail(function () {
deleteNextFile(files, index + 1, deferred);
});
}
function deleteFile(path) {
2015-09-14 12:47:03 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-15 20:55:26 -07:00
Logger.log('Deleting ' + path);
2015-09-20 09:16:06 -07:00
resolveFile(path, function (fileEntry) {
2015-09-15 20:55:26 -07:00
fileEntry.remove(function () {
Logger.log('Deleted ' + path);
deferred.resolve();
}, function () {
Logger.log('Error deleting ' + path);
deferred.reject();
});
}, function () {
Logger.log('Skipping deletion because file does not exist: ' + path);
deferred.resolve();
});
2015-09-14 12:47:03 -07:00
return deferred.promise();
}
2015-09-20 09:16:06 -07:00
function resolveFile(path, success, fail) {
getFileSystem().done(function (fileSystem) {
fileSystem.root.getFile(path, { create: false }, success, fail);
});
}
2015-09-14 21:31:12 -07:00
function createLocalItem(libraryItem, serverInfo, originalFileName) {
var path = getDirectoryPath(libraryItem, serverInfo);
path.push(getLocalFileName(libraryItem, originalFileName));
2015-09-14 14:24:54 -07:00
var item = {};
var deferred = DeferredBuilder.Deferred();
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
var localPath = path.join('/');
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
item.LocalPath = localPath;
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
for (var i = 0, length = libraryItem.MediaSources.length; i < length; i++) {
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
var mediaSource = libraryItem.MediaSources[i];
mediaSource.Path = localPath;
mediaSource.Protocol = 'File';
}
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
item.ServerId = serverInfo.Id;
item.Item = libraryItem;
item.ItemId = libraryItem.Id;
item.Id = getLocalId(item.ServerId, item.ItemId);
deferred.resolveWith(null, [item]);
2015-09-14 21:31:12 -07:00
2015-09-14 14:24:54 -07:00
return deferred.promise();
}
2015-09-15 20:55:26 -07:00
function getDirectoryPath(item, server) {
2015-09-14 21:31:12 -07:00
var parts = [];
2015-09-15 20:55:26 -07:00
parts.push("sync");
2015-09-14 21:31:12 -07:00
parts.push(server.Name);
if (item.Type == "Episode") {
parts.push("TV");
parts.push(item.SeriesName);
if (item.SeasonName) {
parts.push(item.SeasonName);
}
}
else if (item.MediaType == 'Video') {
parts.push("Videos");
parts.push(item.Name);
}
else if (item.MediaType == 'Audio') {
parts.push("Music");
if (item.AlbumArtist) {
parts.push(item.AlbumArtist);
}
if (item.Album) {
parts.push(item.Album);
}
}
else if (item.MediaType == 'Photo') {
parts.push("Photos");
if (item.Album) {
parts.push(item.Album);
}
}
return parts.map(getValidFileName);
}
function getLocalFileName(libraryItem, originalFileName) {
var filename = originalFileName || libraryItem.Name;
2015-09-15 20:55:26 -07:00
return getValidFileName(filename);
2015-09-14 21:31:12 -07:00
}
function getValidFileName(filename) {
// TODO
return filename;
}
2015-09-14 14:24:54 -07:00
function downloadFile(url, localPath) {
var deferred = DeferredBuilder.Deferred();
2015-09-14 21:31:12 -07:00
Logger.log('downloading: ' + url + ' to ' + localPath);
2015-09-17 14:26:06 -07:00
getFileSystem().done(function (fileSystem) {
2015-09-21 08:43:10 -07:00
createDirectory(getParentDirectoryPath(localPath)).done(function () {
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
fileSystem.root.getFile(localPath, { create: true }, function (targetFile) {
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
var downloader = new BackgroundTransfer.BackgroundDownloader();
// Create a new download operation.
var download = downloader.createDownload(url, targetFile);
// Start the download and persist the promise to be able to cancel the download.
var downloadPromise = download.startAsync().then(function () {
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
// on success
var localUrl = localPath;
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
Logger.log('Downloaded local url: ' + localUrl);
deferred.resolveWith(null, [localUrl]);
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
}, function () {
// on error
Logger.log('download failed: ' + url + ' to ' + localPath);
deferred.reject();
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
}, function (value) {
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
// on progress
2015-09-22 09:06:27 -07:00
//Logger.log('download progress: ' + value);
2015-09-17 14:26:06 -07:00
2015-09-21 08:43:10 -07:00
});
2015-09-20 09:16:06 -07:00
});
2015-09-21 08:43:10 -07:00
}).fail(getOnFail(deferred));;
}).fail(getOnFail(deferred));
return deferred.promise();
}
function createDirectory(path) {
var deferred = DeferredBuilder.Deferred();
createDirectoryPart(path, 0, deferred);
return deferred.promise();
}
function createDirectoryPart(path, index, deferred) {
var parts = path.split('/');
if (index >= parts.length) {
deferred.resolve();
return;
}
parts.length = index + 1;
var pathToCreate = parts.join('/');
createDirectoryInternal(pathToCreate).done(function () {
createDirectoryPart(path, index + 1, deferred);
}).fail(getOnFail(deferred));
}
function createDirectoryInternal(path) {
Logger.log('creating directory: ' + path);
var deferred = DeferredBuilder.Deferred();
getFileSystem().done(function (fileSystem) {
fileSystem.root.getDirectory(path, { create: true, exclusive: false }, function (targetFile) {
Logger.log('createDirectory succeeded');
deferred.resolve();
}, function () {
Logger.log('createDirectory failed');
deferred.reject();
2015-09-17 14:26:06 -07:00
});
2015-09-21 08:43:10 -07:00
}).fail(getOnFail(deferred));
2015-09-14 21:31:12 -07:00
2015-09-14 14:24:54 -07:00
return deferred.promise();
}
2015-09-21 08:43:10 -07:00
function getParentDirectoryPath(path) {
return path.substring(0, path.lastIndexOf('/'));;
}
2015-09-14 21:31:12 -07:00
function downloadSubtitles(url, localItem, subtitleStream) {
var path = item.LocalPath;
var filename = getSubtitleSaveFileName(item, subtitleStream.Language, subtitleStream.IsForced) + "." + subtitleStream.Codec.toLowerCase();
var parentPath = getParentDirectoryPath(path);
path = combinePaths(parentPath, filename);
return downloadFile(url, path);
}
function getSubtitleSaveFileName(item, language, isForced) {
var path = item.LocalPath;
var name = getNameWithoutExtension(path);
if (language) {
name += "." + language.toLowerCase();
}
if (isForced) {
name += ".foreign";
}
return name;
}
function getNameWithoutExtension(path) {
var parts = path.split('/');
var name = parts[parts.length - 1];
var index = name.lastIndexOf('.');
if (index != -1) {
name = name.substring(0, index);
}
return name;
}
function getParentDirectoryPath(path) {
var parts = path.split('/');
parts.length--;
return parts.join('/');
}
function combinePaths(path1, path2) {
return path1 + path2;
}
function getLocalId(serverId, itemId) {
2015-09-17 14:26:06 -07:00
return serverId + '_' + itemId;
2015-09-14 14:24:54 -07:00
}
2015-09-14 18:16:31 -07:00
function hasImage(serverId, itemId, imageTag) {
2015-09-14 21:31:12 -07:00
2015-09-14 18:16:31 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 21:31:12 -07:00
getImageLocalPath(serverId, itemId, imageTag).done(function (localPath) {
fileExists(localPath).done(function (exists) {
deferred.resolveWith(null, [exists]);
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
2015-09-14 18:16:31 -07:00
return deferred.promise();
}
function downloadImage(url, serverId, itemId, imageTag) {
2015-09-14 21:31:12 -07:00
var deferred = DeferredBuilder.Deferred();
getImageLocalPath(serverId, itemId, imageTag).done(function (localPath) {
downloadFile(url, localPath).done(function () {
deferred.resolve();
}).fail(getOnFail(deferred));
}).fail(getOnFail(deferred));
return deferred.promise();
}
function getImageLocalPath(serverId, itemId, imageTag) {
var deferred = DeferredBuilder.Deferred();
2015-09-21 08:43:10 -07:00
var path = "images/" + serverId + "/" + itemId + "/" + imageTag;
2015-09-14 21:31:12 -07:00
2015-09-20 09:16:06 -07:00
deferred.resolveWith(null, [path]);
2015-09-14 21:31:12 -07:00
return deferred.promise();
}
function fileExists(path) {
var deferred = DeferredBuilder.Deferred();
2015-09-22 10:09:29 -07:00
if (window.NativeFileSystem) {
var exists = NativeFileSystem.fileExists(path);
Logger.log('fileExists: ' + exists + ' - path: ' + path);
deferred.resolveWith(null, [exists]);
return deferred.promise();
}
2015-09-20 09:16:06 -07:00
resolveFile(path, function (fileEntry) {
Logger.log('fileExists: true - path: ' + path);
2015-09-14 21:31:12 -07:00
deferred.resolveWith(null, [true]);
}, function () {
2015-09-20 09:16:06 -07:00
Logger.log('fileExists: false - path: ' + path);
2015-09-14 21:31:12 -07:00
deferred.resolveWith(null, [false]);
});
return deferred.promise();
}
var fileSystem;
function getFileSystem() {
2015-09-14 18:16:31 -07:00
var deferred = DeferredBuilder.Deferred();
2015-09-14 21:31:12 -07:00
if (fileSystem) {
deferred.resolveWith(null, [fileSystem]);
} else {
requestFileSystem(PERSISTENT, 0, function (fs) {
fileSystem = fs;
deferred.resolveWith(null, [fileSystem]);
});
}
2015-09-14 18:16:31 -07:00
return deferred.promise();
}
2015-09-14 21:31:12 -07:00
function getOnFail(deferred) {
return function () {
deferred.reject();
};
}
2015-09-21 08:43:10 -07:00
function translateFilePath(path) {
var deferred = DeferredBuilder.Deferred();
2015-09-22 10:09:29 -07:00
if ($.browser.android) {
deferred.resolveWith(null, ['file://' + ]);
return deferred.promise();
}
2015-09-21 08:43:10 -07:00
resolveFile(path, function (fileEntry) {
Logger.log('translateFilePath fileExists: true - path: ' + path);
Logger.log('translateFilePath resolving with: ' + fileEntry.toURL());
deferred.resolveWith(null, [fileEntry.toURL()]);
}, function () {
Logger.log('translateFilePath fileExists: false - path: ' + path);
deferred.resolveWith(null, [path]);
});
return deferred.promise();
}
2015-09-09 10:49:44 -07:00
window.LocalAssetManager = {
2015-09-10 11:28:22 -07:00
getLocalMediaSource: getLocalMediaSource,
saveOfflineUser: saveOfflineUser,
2015-09-15 20:55:26 -07:00
deleteOfflineUser: deleteOfflineUser,
2015-09-14 12:47:03 -07:00
getCameraPhotos: getCameraPhotos,
getOfflineActions: getOfflineActions,
2015-09-15 20:55:26 -07:00
deleteOfflineActions: deleteOfflineActions,
2015-09-14 12:47:03 -07:00
getServerItemIds: getServerItemIds,
removeLocalItem: removeLocalItem,
getLocalItem: getLocalItem,
2015-09-14 14:24:54 -07:00
addOrUpdateLocalItem: addOrUpdateLocalItem,
createLocalItem: createLocalItem,
downloadFile: downloadFile,
2015-09-14 18:16:31 -07:00
downloadSubtitles: downloadSubtitles,
hasImage: hasImage,
2015-09-20 09:16:06 -07:00
downloadImage: downloadImage,
2015-09-21 08:43:10 -07:00
fileExists: fileExists,
translateFilePath: translateFilePath
2015-09-09 10:49:44 -07:00
};
})();