Merge pull request #206 from Spydarlee/clean-file-names

Clean up generated files names to remove or replace invalid characters
This commit is contained in:
Moritz Jung 2025-09-03 15:33:39 +02:00 committed by GitHub
commit 535987e252
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -0,0 +1,16 @@
// Illegal characters in the form `[illegal_character, replacement][]`
export const ILLEGAL_FILENAME_CHARACTERS = [
['\/', '-'],
['\\', '-'],
['<', ''],
['>', ''],
[':', ' - '],
['"', "'"],
['|', ' - '],
['?', ''],
['*', ''],
['[', '('],
[']', ')'],
['^', ''],
['#', ''],
];

View file

@ -12,6 +12,7 @@ import { WikiModel } from '../models/WikiModel';
import type { MediaDbPluginSettings } from '../settings/Settings';
import { MediaType } from './MediaType';
import { replaceTags } from './Utils';
import { ILLEGAL_FILENAME_CHARACTERS } from './IllegalFilenameCharactersList';
export const MEDIA_TYPES: MediaType[] = [
MediaType.Movie,
@ -71,7 +72,14 @@ export class MediaTypeManager {
getFileName(mediaTypeModel: MediaTypeModel): string {
// Ignore undefined tags since some search APIs do not return all properties in the model and produce clean file names even if errors occur
return replaceTags(this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType())!, mediaTypeModel, true);
let fileName = replaceTags(this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType())!, mediaTypeModel, true);
return this.cleanFileName(fileName);
}
cleanFileName(fileName: string): string {
const cleanedFileName = ILLEGAL_FILENAME_CHARACTERS.reduce((str, char) => str.replaceAll(char[0], char[1]), fileName);
// Remove all duplicate whitespace in the file name
return cleanedFileName.replaceAll(/ +/g, ' ');
}
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {