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:
commit
535987e252
2 changed files with 25 additions and 1 deletions
16
src/utils/IllegalFilenameCharactersList.ts
Normal file
16
src/utils/IllegalFilenameCharactersList.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Illegal characters in the form `[illegal_character, replacement][]`
|
||||||
|
export const ILLEGAL_FILENAME_CHARACTERS = [
|
||||||
|
['\/', '-'],
|
||||||
|
['\\', '-'],
|
||||||
|
['<', ''],
|
||||||
|
['>', ''],
|
||||||
|
[':', ' - '],
|
||||||
|
['"', "'"],
|
||||||
|
['|', ' - '],
|
||||||
|
['?', ''],
|
||||||
|
['*', ''],
|
||||||
|
['[', '('],
|
||||||
|
[']', ')'],
|
||||||
|
['^', ''],
|
||||||
|
['#', ''],
|
||||||
|
];
|
||||||
|
|
@ -12,6 +12,7 @@ import { WikiModel } from '../models/WikiModel';
|
||||||
import type { MediaDbPluginSettings } from '../settings/Settings';
|
import type { MediaDbPluginSettings } from '../settings/Settings';
|
||||||
import { MediaType } from './MediaType';
|
import { MediaType } from './MediaType';
|
||||||
import { replaceTags } from './Utils';
|
import { replaceTags } from './Utils';
|
||||||
|
import { ILLEGAL_FILENAME_CHARACTERS } from './IllegalFilenameCharactersList';
|
||||||
|
|
||||||
export const MEDIA_TYPES: MediaType[] = [
|
export const MEDIA_TYPES: MediaType[] = [
|
||||||
MediaType.Movie,
|
MediaType.Movie,
|
||||||
|
|
@ -71,7 +72,14 @@ export class MediaTypeManager {
|
||||||
|
|
||||||
getFileName(mediaTypeModel: MediaTypeModel): string {
|
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
|
// 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> {
|
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue