Skip to content

Commit

Permalink
change filename format for all media
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Mar 20, 2024
1 parent f7ae217 commit 69abb9f
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ export class Backend {

try {
if (screenshotDetails !== null) {
screenshotFileName = await this._injectAnkiNoteScreenshot(ankiConnect, timestamp, definitionDetails, screenshotDetails);
screenshotFileName = await this._injectAnkiNoteScreenshot(ankiConnect, timestamp, screenshotDetails);
}
} catch (e) {
errors.push(ExtensionError.serialize(e));
Expand Down Expand Up @@ -2122,7 +2122,7 @@ export class Backend {
let dictionaryMedia;
try {
let errors2;
({results: dictionaryMedia, errors: errors2} = await this._injectAnkiNoteDictionaryMedia(ankiConnect, timestamp, definitionDetails, dictionaryMediaDetails));
({results: dictionaryMedia, errors: errors2} = await this._injectAnkiNoteDictionaryMedia(ankiConnect, timestamp, dictionaryMediaDetails));
for (const error of errors2) {
errors.push(ExtensionError.serialize(error));
}
Expand Down Expand Up @@ -2173,19 +2173,18 @@ export class Backend {

let extension = contentType !== null ? getFileExtensionFromAudioMediaType(contentType) : null;
if (extension === null) { extension = '.mp3'; }
let fileName = this._generateAnkiNoteMediaFileName('yomitan_audio', extension, timestamp, definitionDetails);
let fileName = this._generateAnkiNoteMediaFileName('yomitan_audio', extension, timestamp);
fileName = fileName.replace(/\]/g, '');
return await ankiConnect.storeMediaFile(fileName, data);
}

/**
* @param {AnkiConnect} ankiConnect
* @param {number} timestamp
* @param {import('api').InjectAnkiNoteMediaDefinitionDetails} definitionDetails
* @param {import('api').InjectAnkiNoteMediaScreenshotDetails} details
* @returns {Promise<?string>}
*/
async _injectAnkiNoteScreenshot(ankiConnect, timestamp, definitionDetails, details) {
async _injectAnkiNoteScreenshot(ankiConnect, timestamp, details) {
const {tabId, frameId, format, quality} = details;
const dataUrl = await this._getScreenshot(tabId, frameId, format, quality);

Expand All @@ -2195,7 +2194,7 @@ export class Backend {
throw new Error('Unknown media type for screenshot image');
}

const fileName = this._generateAnkiNoteMediaFileName('yomitan_browser_screenshot', extension, timestamp, definitionDetails);
const fileName = this._generateAnkiNoteMediaFileName('yomitan_browser_screenshot', extension, timestamp);
return await ankiConnect.storeMediaFile(fileName, data);
}

Expand Down Expand Up @@ -2233,11 +2232,10 @@ export class Backend {
/**
* @param {AnkiConnect} ankiConnect
* @param {number} timestamp
* @param {import('api').InjectAnkiNoteMediaDefinitionDetails} definitionDetails
* @param {import('api').InjectAnkiNoteMediaDictionaryMediaDetails[]} dictionaryMediaDetails
* @returns {Promise<{results: import('api').InjectAnkiNoteDictionaryMediaResult[], errors: unknown[]}>}
*/
async _injectAnkiNoteDictionaryMedia(ankiConnect, timestamp, definitionDetails, dictionaryMediaDetails) {
async _injectAnkiNoteDictionaryMedia(ankiConnect, timestamp, dictionaryMediaDetails) {
const targets = [];
const detailsList = [];
const detailsMap = new Map();
Expand Down Expand Up @@ -2271,8 +2269,7 @@ export class Backend {
fileName = this._generateAnkiNoteMediaFileName(
`yomitan_dictionary_media_${i + 1}`,
extension !== null ? extension : '',
timestamp,
definitionDetails
timestamp
);
try {
fileName = await ankiConnect.storeMediaFile(fileName, content);
Expand Down Expand Up @@ -2353,30 +2350,11 @@ export class Backend {
* @param {string} prefix
* @param {string} extension
* @param {number} timestamp
* @param {import('api').InjectAnkiNoteMediaDefinitionDetails?} definitionDetails
* @returns {string}
*/
_generateAnkiNoteMediaFileName(prefix, extension, timestamp, definitionDetails = null) {
_generateAnkiNoteMediaFileName(prefix, extension, timestamp) {
let fileName = prefix;

if (definitionDetails) {
switch (definitionDetails.type) {
case 'kanji':
{
const {character} = definitionDetails;
if (character) { fileName += `_${character}`; }
}
break;
default:
{
const {reading, term} = definitionDetails;
if (reading) { fileName += `_${reading}`; }
if (term) { fileName += `_${term}`; }
}
break;
}
}

fileName += `_${this._ankNoteDateToString(new Date(timestamp))}`;
fileName += extension;

Expand Down Expand Up @@ -2405,7 +2383,8 @@ export class Backend {
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
const milliseconds = date.getUTCMilliseconds().toString().padStart(3, '0');
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}-${milliseconds}`;
}

/**
Expand Down

0 comments on commit 69abb9f

Please sign in to comment.