mirror of
https://github.com/litruv/AudioSort.git
synced 2026-07-24 18:56:01 +10:00
chore: update dependencies and add patch-package for better-sqlite3
feat: implement just-split category filter and enhance metadata parsing fix: improve type imports and variable handling in services
This commit is contained in:
@@ -12,6 +12,15 @@ import { SearchService } from './SearchService';
|
||||
import { WaveFile } from 'wavefile';
|
||||
import { OrganizationService } from './OrganizationService';
|
||||
|
||||
type MusicMetadataParser = (path: string, options?: { duration?: boolean }) => Promise<{
|
||||
format: { duration?: number | null; sampleRate?: number | null; bitsPerSample?: number | null };
|
||||
common: { comment?: unknown[]; genre?: unknown[]; subtitle?: unknown };
|
||||
}>;
|
||||
|
||||
type MusicMetadataNamespace = Partial<{ parseFile: MusicMetadataParser }> & {
|
||||
default?: Partial<{ parseFile: MusicMetadataParser }>;
|
||||
};
|
||||
|
||||
interface CsvCategoryRow {
|
||||
Category: string;
|
||||
SubCategory: string;
|
||||
@@ -32,6 +41,7 @@ export class LibraryService {
|
||||
private metadataSuggestionCache: { authors: Set<string> } | null = null;
|
||||
private readonly waveformPreviewCache = new Map<number, { modifiedAt: number; pointCount: number; samples: number[]; rms: number }>();
|
||||
private offlineAudioContextCtor: (new (channelCount: number, length: number, sampleRate: number) => any) | null = null;
|
||||
private musicMetadataParseFile: MusicMetadataParser | null = null;
|
||||
public constructor(
|
||||
private readonly database: DatabaseService,
|
||||
private readonly settings: SettingsService,
|
||||
@@ -1342,6 +1352,29 @@ export class LibraryService {
|
||||
return value.replace(/\\/g, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves and caches the music-metadata parseFile implementation.
|
||||
*/
|
||||
private async resolveMusicMetadataParser(): Promise<MusicMetadataParser> {
|
||||
if (this.musicMetadataParseFile) {
|
||||
return this.musicMetadataParseFile;
|
||||
}
|
||||
|
||||
const namespace = (await import('music-metadata')) as MusicMetadataNamespace;
|
||||
const candidate = typeof namespace.parseFile === 'function'
|
||||
? namespace.parseFile
|
||||
: namespace.default && typeof namespace.default.parseFile === 'function'
|
||||
? namespace.default.parseFile
|
||||
: null;
|
||||
|
||||
if (!candidate) {
|
||||
throw new Error('music-metadata parseFile not found');
|
||||
}
|
||||
|
||||
this.musicMetadataParseFile = candidate;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts metadata from the WAV container, falling back to defaults when parsing fails.
|
||||
*/
|
||||
@@ -1353,22 +1386,8 @@ export class LibraryService {
|
||||
categories: string[];
|
||||
}> {
|
||||
try {
|
||||
const mmImport = await import('music-metadata');
|
||||
const mm = (mmImport as { default?: unknown }).default ?? mmImport;
|
||||
|
||||
const loadMusicMetadata = typeof mm === 'object' && mm !== null && 'loadMusicMetadata' in mm
|
||||
? (mm as { loadMusicMetadata: () => Promise<{ parseFile: (path: string, opts?: { duration?: boolean }) => Promise<{
|
||||
format: { duration?: number | null; sampleRate?: number | null; bitsPerSample?: number | null };
|
||||
common: { comment?: unknown[]; genre?: unknown[]; subtitle?: unknown };
|
||||
}> }> }).loadMusicMetadata
|
||||
: null;
|
||||
|
||||
if (!loadMusicMetadata) {
|
||||
throw new Error('music-metadata loadMusicMetadata not found');
|
||||
}
|
||||
|
||||
const musicMetadata = await loadMusicMetadata();
|
||||
const metadata = await musicMetadata.parseFile(filePath, { duration: true });
|
||||
const parseFile = await this.resolveMusicMetadataParser();
|
||||
const metadata = await parseFile(filePath, { duration: true });
|
||||
const infoTags = this.tagService.readInfoTags(filePath);
|
||||
const splitInfoValues = (input: string | undefined): string[] =>
|
||||
input
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Fuse from 'fuse.js';
|
||||
import Fuse, { type FuseResult, type IFuseOptions } from 'fuse.js';
|
||||
import { AudioFileSummary } from '../../shared/models';
|
||||
import { DatabaseService } from './DatabaseService';
|
||||
import { TagService } from './TagService';
|
||||
@@ -76,7 +76,7 @@ export class SearchService {
|
||||
|
||||
return fuseInstance
|
||||
.search(remainingQuery)
|
||||
.map((result: Fuse.FuseResult<AudioFileSummary>) => result.item)
|
||||
.map((result: FuseResult<AudioFileSummary>) => result.item)
|
||||
.filter((file) => (filters.length === 0 ? true : this.matchesAdvancedFilters(file, filters)));
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ export class SearchService {
|
||||
/**
|
||||
* Provides the standard Fuse configuration used by the service.
|
||||
*/
|
||||
private createFuseOptions(): Fuse.IFuseOptions<AudioFileSummary> {
|
||||
private createFuseOptions(): IFuseOptions<AudioFileSummary> {
|
||||
return {
|
||||
includeScore: true,
|
||||
threshold: 0.35,
|
||||
@@ -114,7 +114,7 @@ export class SearchService {
|
||||
{ name: 'tags', weight: 0.2 },
|
||||
{ name: 'categories', weight: 0.1 }
|
||||
]
|
||||
} satisfies Fuse.IFuseOptions<AudioFileSummary>;
|
||||
} satisfies IFuseOptions<AudioFileSummary>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -163,8 +163,8 @@ export class TagService {
|
||||
.filter((entry) => entry.length > 0);
|
||||
|
||||
const tagText = tagValuesList.length > 0 ? tagValuesList.join('; ') : null;
|
||||
const categoryText = categoryValuesList.length > 0 ? categoryValuesList.join('; ') : null;
|
||||
const primaryCategory = categoryValuesList.at(0) ?? null;
|
||||
const categoryText = categoryValuesList.length > 0 ? categoryValuesList.join('; ') : null;
|
||||
const primaryCategory = categoryValuesList.length > 0 ? categoryValuesList[0] : null;
|
||||
const trimmedTitle = metadata.title?.toString().trim();
|
||||
const effectiveTitle = trimmedTitle && trimmedTitle.length > 0 ? trimmedTitle : null;
|
||||
const trimmedAuthor = metadata.author?.toString().trim();
|
||||
|
||||
Reference in New Issue
Block a user