[MM-54279] Fix issues around working with saved downloads (#2812)
This commit is contained in:
@@ -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;
|
||||
@@ -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
|
||||
@@ -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();
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user