[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> {
|
export default class JsonFileManager<T> {
|
||||||
jsonFile: string;
|
jsonFile: string;
|
||||||
json: T;
|
json: T;
|
||||||
|
private saving?: Promise<void>;
|
||||||
|
|
||||||
constructor(file: string) {
|
constructor(file: string) {
|
||||||
this.jsonFile = file;
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.writeFile(this.jsonFile, JSON.stringify(this.json, undefined, 2), (err) => {
|
fs.writeFile(this.jsonFile, json, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
// No real point in bringing electron-log into this otherwise electron-free file
|
// No real point in bringing electron-log into this otherwise electron-free file
|
||||||
// eslint-disable-next-line no-console
|
// 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 {
|
setJson(json: T): void {
|
||||||
this.json = json;
|
this.json = json;
|
||||||
this.writeToFile();
|
this.writeToFile();
|
||||||
|
@@ -174,14 +174,19 @@ export class DownloadsManager extends JsonFileManager<DownloadedItems> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const file of Object.values(this.downloads)) {
|
for (const file of Object.values(this.downloads)) {
|
||||||
if (file.bookmark) {
|
try {
|
||||||
this.bookmarks.set(this.getDownloadedFileId(file), {originalPath: file.location, bookmark: file.bookmark});
|
if (file.bookmark) {
|
||||||
|
this.bookmarks.set(this.getDownloadedFileId(file), {originalPath: file.location, bookmark: file.bookmark});
|
||||||
|
|
||||||
if (file.mimeType?.toLowerCase().startsWith('image/')) {
|
if (file.mimeType?.toLowerCase().startsWith('image/')) {
|
||||||
const func = app.startAccessingSecurityScopedResource(file.bookmark);
|
const func = app.startAccessingSecurityScopedResource(file.bookmark);
|
||||||
fs.copyFileSync(file.location, path.resolve(app.getPath('temp'), path.basename(file.location)));
|
fs.copyFileSync(file.location, path.resolve(app.getPath('temp'), path.basename(file.location)));
|
||||||
func();
|
func();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.warn('could not load bookmark', file.filename, e);
|
||||||
|
this.clearFile(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user