From 8c1430e5d6c28815817ef5f18562f850124b68e1 Mon Sep 17 00:00:00 2001 From: benweet Date: Thu, 2 May 2013 01:03:34 +0100 Subject: [PATCH] Add sharing support --- js/sharing.js | 85 +++++++++++++++++++++++++++++++++ js/storage.js | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 js/sharing.js create mode 100644 js/storage.js diff --git a/js/sharing.js b/js/sharing.js new file mode 100644 index 00000000..a2461c4e --- /dev/null +++ b/js/sharing.js @@ -0,0 +1,85 @@ +define(["jquery", "core", "async-runner", "gdrive-provider", "underscore"], function($, core, asyncRunner) { + var sharing = {}; + + // Create a map with providerId: providerObject + var providerMap = _.chain(arguments) + .map(function(argument) { + return argument && argument.providerId && [argument.providerId, argument]; + }).compact().object().value(); + + sharing.getLink = function(attributes) { + var provider = providerMap[attributes.provider]; + if(provider === undefined) { + return undefined; + } + var url = [BASE_URL, 'viewer.html?provider=', attributes.provider]; + _.each(provider.sharingAttributes, function(attributeName) { + url.push('&'); + url.push(attributeName); + url.push('='); + url.push(attributes[attributeName]); + }); + return url.join(""); + }; + + sharing.createLink = function(attributes, callback) { + if(attributes.sharingLink !== undefined) { + callback(); + return; + } + var provider = providerMap[attributes.provider]; + if(provider === undefined) { + callback(); + return; + } + var task = asyncRunner.createTask(); + var shortUrl = undefined; + task.onRun(function() { + if(core.isOffline === true) { + task.chain(); + return; + } + var url = [MAIN_URL, 'viewer.html?provider=', attributes.provider]; + _.each(provider.sharingAttributes, function(attributeName) { + url.push('&'); + url.push(attributeName); + url.push('='); + url.push(attributes[attributeName]); + }); + $.getJSON( + "https://api-ssl.bitly.com/v3/shorten", + { + "access_token": BITLY_ACCESS_TOKEN, + "longUrl": url.join("") + }, + function(response) + { + if(response.data) { + shortUrl = response.data.url; + } + console.log(shortUrl); + task.chain(); + } + ); + }); + task.onSuccess(function() { + callback(shortUrl); + }); + asyncRunner.addTask(task); + }; + + core.onReady(function() { + var fileIndexList = _.compact(localStorage["file.list"].split(";")); + _.each(fileIndexList, function(fileIndex) { + var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";")); + _.each(publishIndexList, function(publishIndex) { + var publishAttributes = JSON.parse(localStorage[publishIndex]); + sharing.createLink(publishAttributes, function(shortUrl) { + publishAttributes.sharingLink = shortUrl; + }); + }); + }); + }); + + return sharing; +}); \ No newline at end of file diff --git a/js/storage.js b/js/storage.js new file mode 100644 index 00000000..bb2f779f --- /dev/null +++ b/js/storage.js @@ -0,0 +1,129 @@ +// Setup an empty localStorage or upgrade an existing one +function setupLocalStorage() { + + // Create the file system if not exist + if (localStorage["file.list"] === undefined) { + localStorage["file.list"] = ";"; + } + var fileIndexList = _.compact(localStorage["file.list"].split(";")); + + // localStorage versioning + var version = localStorage["version"]; + + // Upgrade from v0 to v1 + if(version === undefined) { + + // Not used anymore + localStorage.removeItem("sync.queue"); + localStorage.removeItem("sync.current"); + localStorage.removeItem("file.counter"); + + _.each(fileIndexList, function(fileIndex) { + localStorage[fileIndex + ".publish"] = ";"; + var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";")); + _.each(syncIndexList, function(syncIndex) { + localStorage[syncIndex + ".contentCRC"] = "0"; + // We store title CRC only for Google Drive synchronization + if(localStorage[syncIndex + ".etag"] !== undefined) { + localStorage[syncIndex + ".titleCRC"] = "0"; + } + }); + }); + version = "v1"; + } + + // Upgrade from v1 to v2 + if(version == "v1") { + var gdriveLastChangeId = localStorage["sync.gdrive.lastChangeId"]; + if(gdriveLastChangeId) { + localStorage["gdrive.lastChangeId"] = gdriveLastChangeId; + localStorage.removeItem("sync.gdrive.lastChangeId"); + } + var dropboxLastChangeId = localStorage["sync.dropbox.lastChangeId"]; + if(dropboxLastChangeId) { + localStorage["dropbox.lastChangeId"] = dropboxLastChangeId; + localStorage.removeItem("sync.dropbox.lastChangeId"); + } + + var SYNC_PROVIDER_GDRIVE = "sync." + PROVIDER_GDRIVE + "."; + var SYNC_PROVIDER_DROPBOX = "sync." + PROVIDER_DROPBOX + "."; + _.each(fileIndexList, function(fileIndex) { + var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";")); + _.each(syncIndexList, function(syncIndex) { + var syncAttributes = {}; + if (syncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) { + syncAttributes.provider = PROVIDER_GDRIVE; + syncAttributes.id = syncIndex.substring(SYNC_PROVIDER_GDRIVE.length); + syncAttributes.etag = localStorage[syncIndex + ".etag"]; + syncAttributes.contentCRC = localStorage[syncIndex + ".contentCRC"]; + syncAttributes.titleCRC = localStorage[syncIndex + ".titleCRC"]; + } + else if (syncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) { + syncAttributes.provider = PROVIDER_DROPBOX; + syncAttributes.path = decodeURIComponent(syncIndex.substring(SYNC_PROVIDER_DROPBOX.length)); + syncAttributes.version = localStorage[syncIndex + ".version"]; + syncAttributes.contentCRC = localStorage[syncIndex + ".contentCRC"]; + } + localStorage[syncIndex] = JSON.stringify(syncAttributes); + localStorage.removeItem(syncIndex + ".etag"); + localStorage.removeItem(syncIndex + ".version"); + localStorage.removeItem(syncIndex + ".contentCRC"); + localStorage.removeItem(syncIndex + ".titleCRC"); + }); + }); + version = "v2"; + } + + // Upgrade from v2 to v3 + if(version == "v2") { + _.each(fileIndexList, function(fileIndex) { + if(!_.has(localStorage, fileIndex + ".sync")) { + localStorage.removeItem(fileIndex + ".title"); + localStorage.removeItem(fileIndex + ".publish"); + localStorage.removeItem(fileIndex + ".content"); + localStorage["file.list"] = localStorage["file.list"].replace(";" + + fileIndex + ";", ";"); + } + }); + version = "v3"; + } + + // Upgrade from v3 to v4 + if(version == "v3") { + var currentFileIndex = localStorage["file.current"]; + if(currentFileIndex !== undefined && + localStorage["file.list"].indexOf(";" + currentFileIndex + ";") === -1) + { + localStorage.removeItem("file.current"); + } + version = "v4"; + } + + // Upgrade from v4 to v5 + if(version == "v4") { + // Recreate GitHub token + localStorage.removeItem("githubToken"); + version = "v5"; + } + + // Upgrade from v5 to v6 + if(version == "v5") { + _.each(fileIndexList, function(fileIndex) { + var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";")); + _.each(publishIndexList, function(publishIndex) { + var publishAttributes = JSON.parse(localStorage[publishIndex]); + if(publishAttributes.provider == "gdrive") { + // Change fileId to Id to be consistent with syncAttributes + publishAttributes.id = publishAttributes.fileId; + publishAttributes.fileId = undefined; + localStorage[publishIndex] = JSON.stringify(publishAttributes); + } + }); + }); + version = "v6"; + } + + localStorage["version"] = version; +} +// Setup the localStorage when starting +setupLocalStorage();