Skip to content

Commit

Permalink
[MM-54279] Fix issues around working with saved downloads (#2812)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinbinnie committed Sep 1, 2023
1 parent 9c3febd commit 8bf7a59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/common/JsonFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs from 'fs';
export default class JsonFileManager<T> {
jsonFile: string;
json: T;
private saving?: Promise<void>;

constructor(file: string) {
this.jsonFile = file;
Expand All @@ -16,9 +17,9 @@ export default class JsonFileManager<T> {
}
}

writeToFile(): Promise<void> {
write(json: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile(this.jsonFile, JSON.stringify(this.json, undefined, 2), (err) => {
fs.writeFile(this.jsonFile, json, (err) => {
if (err) {
// No real point in bringing electron-log into this otherwise electron-free file
// eslint-disable-next-line no-console
Expand All @@ -31,6 +32,16 @@ export default class JsonFileManager<T> {
});
}

writeToFile(): Promise<void> {
const json = JSON.stringify(this.json, undefined, 2);
if (this.saving) {
this.saving = this.saving.then(() => this.write(json));
} else {
this.saving = this.write(json);
}
return this.saving;
}

setJson(json: T): void {
this.json = json;
this.writeToFile();
Expand Down
17 changes: 11 additions & 6 deletions src/main/downloadsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,19 @@ export class DownloadsManager extends JsonFileManager<DownloadedItems> {
}

for (const file of Object.values(this.downloads)) {
if (file.bookmark) {
this.bookmarks.set(this.getDownloadedFileId(file), {originalPath: file.location, bookmark: file.bookmark});
try {
if (file.bookmark) {
this.bookmarks.set(this.getDownloadedFileId(file), {originalPath: file.location, bookmark: file.bookmark});

if (file.mimeType?.toLowerCase().startsWith('image/')) {
const func = app.startAccessingSecurityScopedResource(file.bookmark);
fs.copyFileSync(file.location, path.resolve(app.getPath('temp'), path.basename(file.location)));
func();
if (file.mimeType?.toLowerCase().startsWith('image/')) {
const func = app.startAccessingSecurityScopedResource(file.bookmark);
fs.copyFileSync(file.location, path.resolve(app.getPath('temp'), path.basename(file.location)));
func();
}
}
} catch (e) {
log.warn('could not load bookmark', file.filename, e);
this.clearFile(file);
}
}
}
Expand Down

0 comments on commit 8bf7a59

Please sign in to comment.