Skip to content

Commit

Permalink
Fixing Dropbox connection
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Oct 28, 2024
1 parent e3a362f commit 036f8c5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 66 deletions.
7 changes: 5 additions & 2 deletions core/dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},

/**
Expand Down Expand Up @@ -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);
}
Expand Down
114 changes: 58 additions & 56 deletions core/nsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,30 @@ 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);
},
);
})
});
});
}

upload = function(contents, callback)
{
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)
Expand Down Expand Up @@ -212,7 +213,7 @@ function update() {

NSync = {
export()
{
{
var zp = Zotero.getActiveZoteroPane();
var collection = zp.getSelectedCollection();
if(!collection)
Expand Down Expand Up @@ -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);
},
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>ZeNotes</em:name>
<em:version>0.8.9</em:version>
<em:version>0.9.0</em:version>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:updateURL>https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json</em:updateURL>
<em:homepageURL>https://github.com/frianasoa/zenotes</em:homepageURL>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"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"
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<ns1:version>0.8.9</ns1:version>
<ns1:version>0.9.0</ns1:version>
<ns1:targetApplication>
<rdf:Description>
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>5.0.0</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.8.9/zenotes-v0.8.9.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.9.0/zenotes-v0.9.0.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand All @@ -20,7 +20,7 @@
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>4.999</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.8.9/zenotes-v0.8.9.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.9.0/zenotes-v0.9.0.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand Down

0 comments on commit 036f8c5

Please sign in to comment.