Skip to content

Commit

Permalink
Stop editing file during sync, fixed promise await when downloading f…
Browse files Browse the repository at this point in the history
…ile from drive
  • Loading branch information
SebTota committed Feb 17, 2021
1 parent b2818f9 commit b016e44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
11 changes: 9 additions & 2 deletions lib/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,15 @@ $('#btn-cloud-sign-in').on('click', function(e) {
});
})

$('#btn-sync').on('click', function(e) {
$('#btn-sync').on('click', (e) => {
this.quill['emitter']['_events']['text-change'] = undefined; // Clear on change file save listener
quill.enable(false); // Disable editor
sync.instance.sync().then(() => {
console.log('done sync');
logger.info('Finished sync');
console.log('Finished sync');
// Reopen file if a user was editing a file when sync was initiated
if (currentFile['relativePath']) {
openFile(currentFile['relativePath']);
}
})
})
45 changes: 24 additions & 21 deletions modules/providers/google-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,29 +354,32 @@ module.exports = class GoogleAuth {

logger.info(`Sync file from drive: Syncing file to path: ${filePath}`);

this.drive.files
.get({ fileId, alt: "media"}, {responseType: 'stream'})
.then((res) => {
const dest = fs.createWriteStream(filePath);

const decoder = new TextDecoder("utf-8");
const reader = res.data.getReader()
reader.read().then(function processText({ done, value }) {
if (done) {
if (mtime !== undefined) {
console.log(`Setting time: ${mtime}`);
fs.utimesSync(filePath, mtime, mtime)
return new Promise((resolve) => {
this.drive.files
.get({fileId, alt: "media"}, {responseType: 'stream'})
.then((res) => {
const dest = fs.createWriteStream(filePath);

const decoder = new TextDecoder("utf-8");
const reader = res.data.getReader()
reader.read().then(function processText({done, value}) {
if (done) {
if (mtime !== undefined) {
console.log(`Setting time: ${mtime}`);
fs.utimesSync(filePath, mtime, mtime)
}
console.log("Stream complete");
folderStructure.buildFileMenu()
resolve();
return;
}
console.log("Stream complete");
folderStructure.buildFileMenu()
return;
}
dest.write(decoder.decode(value))
dest.write(decoder.decode(value))

// Read some more, and call this function again
return reader.read().then(processText);
});
})
// Read some more, and call this function again
return reader.read().then(processText);
});
})
});
}

/*
Expand Down

0 comments on commit b016e44

Please sign in to comment.