Improve Anki storeMediaFile (#2084)

* Use the return value of storeMediaFile

* Update documentation
This commit is contained in:
toasted-nutbread 2022-03-13 21:17:41 -04:00 committed by GitHub
parent 1590f5218c
commit f98f5f9395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View File

@ -1806,7 +1806,7 @@ class Backend {
if (extension === null) { extension = '.mp3'; } if (extension === null) { extension = '.mp3'; }
let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', extension, timestamp, definitionDetails); let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', extension, timestamp, definitionDetails);
fileName = fileName.replace(/\]/g, ''); fileName = fileName.replace(/\]/g, '');
await ankiConnect.storeMediaFile(fileName, data); fileName = await ankiConnect.storeMediaFile(fileName, data);
return fileName; return fileName;
} }
@ -1821,8 +1821,8 @@ class Backend {
throw new Error('Unknown media type for screenshot image'); throw new Error('Unknown media type for screenshot image');
} }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails); let fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data); fileName = await ankiConnect.storeMediaFile(fileName, data);
return fileName; return fileName;
} }
@ -1839,8 +1839,8 @@ class Backend {
throw new Error('Unknown media type for clipboard image'); throw new Error('Unknown media type for clipboard image');
} }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails); let fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data); fileName = await ankiConnect.storeMediaFile(fileName, data);
return fileName; return fileName;
} }
@ -1877,7 +1877,7 @@ class Backend {
const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType); const extension = MediaUtil.getFileExtensionFromImageMediaType(mediaType);
fileName = this._generateAnkiNoteMediaFileName(`yomichan_dictionary_media_${i + 1}`, extension, timestamp, definitionDetails); fileName = this._generateAnkiNoteMediaFileName(`yomichan_dictionary_media_${i + 1}`, extension, timestamp, definitionDetails);
try { try {
await ankiConnect.storeMediaFile(fileName, content); fileName = await ankiConnect.storeMediaFile(fileName, content);
} catch (e) { } catch (e) {
errors.push(e); errors.push(e);
fileName = null; fileName = null;

View File

@ -105,12 +105,19 @@ class AnkiConnect {
return await this.guiBrowse(`nid:${noteId}`); 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) { if (!this._enabled) {
throw new Error('AnkiConnect not enabled'); throw new Error('AnkiConnect not enabled');
} }
await this._checkVersion(); await this._checkVersion();
return await this._invoke('storeMediaFile', {filename: fileName, data: dataBase64}); return await this._invoke('storeMediaFile', {filename: fileName, data: content});
} }
async findNoteIds(notes) { async findNoteIds(notes) {