Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Improve Anki storeMediaFile #2084

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ class Backend {
if (extension === null) { extension = '.mp3'; }
let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', extension, timestamp, definitionDetails);
fileName = fileName.replace(/\]/g, '');
await ankiConnect.storeMediaFile(fileName, data);
fileName = await ankiConnect.storeMediaFile(fileName, data);

return fileName;
}
Expand All @@ -1821,8 +1821,8 @@ class Backend {
throw new Error('Unknown media type for screenshot image');
}

const fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);
let fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails);
fileName = await ankiConnect.storeMediaFile(fileName, data);

return fileName;
}
Expand All @@ -1839,8 +1839,8 @@ class Backend {
throw new Error('Unknown media type for clipboard image');
}

const fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);
let fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails);
fileName = await ankiConnect.storeMediaFile(fileName, data);

return fileName;
}
Expand Down Expand Up @@ -1877,7 +1877,7 @@ class Backend {
const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType);
fileName = this._generateAnkiNoteMediaFileName(`yomichan_dictionary_media_${i + 1}`, extension, timestamp, definitionDetails);
try {
await ankiConnect.storeMediaFile(fileName, content);
fileName = await ankiConnect.storeMediaFile(fileName, content);
} catch (e) {
errors.push(e);
fileName = null;
Expand Down
11 changes: 9 additions & 2 deletions ext/js/comm/anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@ class AnkiConnect {
return await this.guiBrowse(`nid:${noteId}`);
}

async storeMediaFile(fileName, dataBase64) {
/**
* Stores a file with the specified base64-encoded content inside Anki's media folder.
* @param {string} fileName The name of the file.
* @param {string} content The base64-encoded content of the file.
* @returns {?string} The actual file name used to store the file, which may be different; or `null` if the file was not stored.
* @throws {Error} An error is thrown is this object is not enabled.
*/
async storeMediaFile(fileName, content) {
if (!this._enabled) {
throw new Error('AnkiConnect not enabled');
}
await this._checkVersion();
return await this._invoke('storeMediaFile', {filename: fileName, data: dataBase64});
return await this._invoke('storeMediaFile', {filename: fileName, data: content});
}

async findNoteIds(notes) {
Expand Down