diff --git a/core/dropbox.js b/core/dropbox.js index bdd384e..4cd5499 100644 --- a/core/dropbox.js +++ b/core/dropbox.js @@ -17,11 +17,15 @@ const Dropbox = { /** * Initialize the Dropbox object by loading values from Zotero.ZeNotes.Prefs. */ - init() { + async init() { this.accessToken = Zotero.ZeNotes.Prefs.getb('dropbox-access-token', ''); this.refreshToken = Zotero.ZeNotes.Prefs.getb('dropbox-refresh-token', ''); this.clientId = Zotero.ZeNotes.Prefs.getb('dropbox-client-id', ''); this.clientSecret = Zotero.ZeNotes.Prefs.getb('dropbox-client-secret', ''); + if(this.accessToken=="") + { + await this.refresh(); + } }, /** @@ -80,7 +84,6 @@ const Dropbox = { }); if (response.status === 401) { - // console.log('Access token expired, refreshing...'); await this.refresh(); return this.upload(filePath, fileContent, callback); } diff --git a/core/nsync.js b/core/nsync.js index 6ab70ad..c60b7db 100644 --- a/core/nsync.js +++ b/core/nsync.js @@ -81,18 +81,19 @@ function update() { var activecollection = Zotero.getActiveZoteroPane().getSelectedCollection().name; var outfile = Zotero.File.pathToFile(Zotero.getTempDirectory().path+"\\zenotes-import.zip").path; var destfilename = Zotero.File.pathToFile(Zotero.getTempDirectory().path+"\\zenotes-import").path; - Dropbox.init(); - return Dropbox.download(activecollection+".zip", function(data, hash){ - Zotero.File.putContentsAsync(outfile, data).then(e=>{ - unzip(outfile, destfilename, - function(){Zotero_File_Interface.Progress.show("Unzipping file ...");}, - function(m){Zotero_File_Interface.Progress.show(m);}, - function(){ - Zotero_File_Interface.Progress.close(); - callback(destfilename+"\\zenotes-export.rdf", hash); - }, - ); - }) + Dropbox.init().then(token=> { + return Dropbox.download(activecollection+".zip", function(data, hash){ + Zotero.File.putContentsAsync(outfile, data).then(e=>{ + unzip(outfile, destfilename, + function(){Zotero_File_Interface.Progress.show("Unzipping file ...");}, + function(m){Zotero_File_Interface.Progress.show(m);}, + function(){ + Zotero_File_Interface.Progress.close(); + callback(destfilename+"\\zenotes-export.rdf", hash); + }, + ); + }) + }); }); } @@ -100,10 +101,10 @@ function update() { { var target = Zotero.ZeNotes.Prefs.get('dropbox-target-user', 'allusers'); var filename = Zotero.getActiveZoteroPane().getSelectedCollection().name; - Dropbox.init(); - - var fn = "/"+target+"/"+filename+".zip"; - Dropbox.upload(fn, contents, callback); + Dropbox.init().then(token=>{ + var fn = "/"+target+"/"+filename+".zip"; + Dropbox.upload(fn, contents, callback); + }) } delay = function(ms) @@ -212,7 +213,7 @@ function update() { NSync = { export() - { + { var zp = Zotero.getActiveZoteroPane(); var collection = zp.getSelectedCollection(); if(!collection) @@ -249,7 +250,6 @@ NSync = { async beforeimport(activecollection, hash) { - Zotero_File_Interface.Progress.show("Importing all items to Zotero ..."); Zotero.ZeNotes.DBPrefs.set("zenotes.nsync.collectionhash:"+activecollection, hash); }, @@ -269,50 +269,52 @@ NSync = { } var activecollection = collection.name; var username = Zotero.Prefs.get("extensions.zotero.sync.server.username", true); - Dropbox.init(); - Dropbox.list(username).then(async list=>{ - let dataexists = list.map(a=>{return a.name.replace(".zip", "")}).includes(activecollection); - if(dataexists) - { - let current_hash = await Zotero.ZeNotes.DBPrefs.get("zenotes.nsync.collectionhash:"+activecollection); - let file = list.filter(f => f.name === activecollection+".zip")[0]; - - if(file.content_hash!=current_hash) + Dropbox.init().then(token=>{ + Dropbox.list(username).then(async list=>{ + let dataexists = list.map(a=>{return a.name.replace(".zip", "")}).includes(activecollection); + if(dataexists) { - return download(async function(localfilename, hash){ - Zotero_File_Interface.Progress.show("Importing items to Zotero ..."); - return await Zotero_File_Interface.importFile({ - file: localfilename, - recreateStructure: false, - createNewCollection: false, - addToLibraryRoot: false, - linkFiles: false, - onBeforeImport: async function(){return NSync.beforeimport(activecollection, hash)}, - }).then(e=>{ - NSync.annotations(e).then(transfered=>{ - if(transfered) - { - Zotero_File_Interface.Progress.close(); - alert("All items imported!"); - } + let current_hash = await Zotero.ZeNotes.DBPrefs.get("zenotes.nsync.collectionhash:"+activecollection); + let file = list.filter(f => f.name === activecollection+".zip")[0]; + + if(file.content_hash!=current_hash) + { + return download(async function(localfilename, hash){ + Zotero_File_Interface.Progress.show("Importing items to Zotero ..."); + return await Zotero_File_Interface.importFile({ + file: localfilename, + recreateStructure: false, + createNewCollection: false, + addToLibraryRoot: false, + linkFiles: false, + onBeforeImport: async function(){return NSync.beforeimport(activecollection, hash)}, + }).then(e=>{ + NSync.annotations(e).then(transfered=>{ + if(transfered) + { + Zotero_File_Interface.Progress.close(); + alert("All items imported!"); + } + }) }) - }) - }); + }); + } + else + { + if(confirm("You have already downloaded this file! Do you want to force download?")) + { + Zotero.ZeNotes.DBPrefs.set("zenotes.nsync.collectionhash:"+activecollection, ""); + NSync.import(); + }; + } } else { - if(confirm("You have already downloaded this file! Do you want to force download?")) - { - Zotero.ZeNotes.DBPrefs.set("zenotes.nsync.collectionhash:"+activecollection, ""); - NSync.import(); - }; + alert("You do not have data on \""+activecollection+"\" on the server!"); } - } - else - { - alert("You do not have data on \""+activecollection+"\" on the server!"); - } - }) + }) + }); + }, async annotations(imported) diff --git a/install.rdf b/install.rdf index 016d6ab..9a5df36 100644 --- a/install.rdf +++ b/install.rdf @@ -5,7 +5,7 @@ zenotes@alefa.net ZeNotes - 0.8.9 + 0.9.0 true https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json https://github.com/frianasoa/zenotes diff --git a/manifest.json b/manifest.json index e63b3a1..e1995e1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Ze Notes", - "version": "0.8.9", + "version": "0.9.0", "description": "Advanced notes manager", "homepage_url": "https://github.com/frianasoa/zenotes", "author": "Fanantenana Rianasoa Andriariniaina", diff --git a/zenote-update.json b/zenote-update.json index 18ab73c..0b03774 100644 --- a/zenote-update.json +++ b/zenote-update.json @@ -3,9 +3,9 @@ "zenotes@alefa.net": { "updates": [ { - "version": "0.8.9", - "update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.8.9/zenotes-v0.8.9.xpi", - "update_hash": "sha256:b050c3bb68f553d52fbf037daa408912ffc3735ecf677b085762aa20dfcd477b", + "version": "0.9.0", + "update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.9.0/zenotes-v0.9.0.xpi", + "update_hash": "sha256:001441b3455fb9c59f67955ae71c00c7508fdfe118cbf370abbca082a67d6550", "applications": { "gecko": { "strict_min_version": "60.0" diff --git a/zenote-update.rdf b/zenote-update.rdf index bf8e69b..8c326a6 100644 --- a/zenote-update.rdf +++ b/zenote-update.rdf @@ -5,13 +5,13 @@ - 0.8.9 + 0.9.0 zotero@chnm.gmu.edu 5.0.0 6.* - https://github.com/frianasoa/Ze-Notes/releases/download/v0.8.9/zenotes-v0.8.9.xpi + https://github.com/frianasoa/Ze-Notes/releases/download/v0.9.0/zenotes-v0.9.0.xpi @@ -20,7 +20,7 @@ juris-m@juris-m.github.io 4.999 6.* - https://github.com/frianasoa/Ze-Notes/releases/download/v0.8.9/zenotes-v0.8.9.xpi + https://github.com/frianasoa/Ze-Notes/releases/download/v0.9.0/zenotes-v0.9.0.xpi