Refactor file mover to ensure faster transfers and more reliable handling (#1406)

This commit is contained in:
Aditya Chandel
2025-10-21 21:10:46 -06:00
committed by GitHub
parent 1edc55e4f0
commit 9186ea0c67
28 changed files with 498 additions and 2596 deletions
@@ -66,33 +66,50 @@
size="small"
appendTo="body"
></p-select>
@if (defaultAvailableLibraryPaths.length > 0) {
<p-select
[options]="defaultAvailableLibraryPaths"
[(ngModel)]="defaultTargetLibraryPathId"
optionLabel="path"
optionValue="id"
placeholder="Select default path"
(onChange)="onDefaultLibraryPathChange()"
class="min-w-[250px]"
size="small"
appendTo="body"
></p-select>
}
</div>
</div>
<div class="flex-grow overflow-y-auto">
<div class="mb-4" style="height: 500px;">
<p-table
[value]="filePreviews"
[responsiveLayout]="'scroll'"
class="block min-w-full"
[scrollable]="true"
scrollHeight="500px"
[virtualScroll]="true"
[virtualScrollItemSize]="46"
[rows]="50"
>
<ng-template pTemplate="header">
<tr>
<th>ID</th>
<th style="max-width:50px;">ID</th>
<th>Current Path</th>
<th>Target Library</th>
<th style="max-width:150px;">Target Library</th>
<th style="max-width:150px;">Library Sub Path</th>
<th></th>
<th>New Path</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-preview let-i="rowIndex">
<ng-template pTemplate="body" let-preview>
<tr>
<td class="text-center">{{ preview.bookId }}</td>
<td class="w-auto">
<p>
<span class="text-gray-400 select-none">{{ preview.currentLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeOriginalPath }}</span>
</p>
<td>{{ preview.bookId }}</td>
<td>
<div class="overflow-hidden text-ellipsis">
<span class="text-gray-400">{{ preview.currentLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeOriginalPath }}</span>
</div>
</td>
<td class="w-auto">
<td style="max-width:150px;">
<p-select
[options]="availableLibraries"
[(ngModel)]="preview.targetLibraryId"
@@ -106,13 +123,31 @@
size="small"
></p-select>
</td>
<td class="w-auto text-[var(--primary-color)] text-center">→</td>
<td class="w-auto">
<p>
<span class="text-gray-400 select-none">{{ preview.targetLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeNewPath }}</span>
</p>
<td style="max-width:150px;">
@if (preview.availableLibraryPaths.length > 0) {
<p-select
[options]="preview.availableLibraryPaths"
[(ngModel)]="preview.targetLibraryPathId"
optionLabel="path"
optionValue="id"
placeholder="Select Path"
(onChange)="onLibraryPathChange(preview)"
[disabled]="preview.isMoved"
class="w-full"
appendTo="body"
size="small"
></p-select>
} @else {
<div class="text-xs text-red-400">No paths configured</div>
}
</td>
<td class="w-auto text-center">
<td class="text-[var(--primary-color)] text-center">→</td>
<td>
<div class="overflow-hidden text-ellipsis">
<span class="text-gray-400">{{ preview.targetLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeNewPath }}</span>
</div>
</td>
<td class="text-center">
@if (preview.isMoved) {
<i class="pi pi-check-circle text-green-600"></i>
}
@@ -14,6 +14,7 @@ import {FileMoveRequest, FileOperationsService} from '../../service/file-operati
import {LibraryService} from "../../../features/book/service/library.service";
import {AppSettingsService} from '../../service/app-settings.service';
import {Select} from 'primeng/select';
import {Library, LibraryPath} from '../../../features/book/model/library.model';
interface FilePreview {
bookId: number;
@@ -21,8 +22,12 @@ interface FilePreview {
relativeOriginalPath: string;
currentLibraryId: number | null;
currentLibraryName: string;
currentLibraryPath: string;
targetLibraryId: number | null;
targetLibraryName: string;
targetLibraryPath: string;
targetLibraryPathId: number | null;
availableLibraryPaths: LibraryPath[];
newPath: string;
relativeNewPath: string;
isMoved?: boolean;
@@ -54,13 +59,15 @@ export class FileMoverComponent implements OnDestroy {
}[] = [];
defaultMovePattern = '';
loading = false;
patternsCollapsed = false;
patternsCollapsed = true;
bookIds: Set<number> = new Set();
books: Book[] = [];
availableLibraries: { id: number | null; name: string }[] = [];
filePreviews: FilePreview[] = [];
defaultTargetLibraryId: number | null = null;
defaultTargetLibraryPathId: number | null = null;
defaultAvailableLibraryPaths: LibraryPath[] = [];
constructor() {
this.bookIds = new Set(this.config.data?.bookIds ?? []);
@@ -152,9 +159,13 @@ export class FileMoverComponent implements OnDestroy {
const currentLibraryId = book.libraryId ?? book.libraryPath?.id ?? (book as any).library?.id ?? null;
const currentLibraryName = this.getLibraryNameById(currentLibraryId);
const currentLibraryPath = this.getLibraryPathById(currentLibraryId);
const targetLibraryId = currentLibraryId;
const targetLibraryName = currentLibraryName;
const availableLibraryPaths = this.getLibraryPathsById(targetLibraryId);
const targetLibraryPathId = availableLibraryPaths.length > 0 ? availableLibraryPaths[0].id ?? null : null;
const targetLibraryPath = availableLibraryPaths.length > 0 ? availableLibraryPaths[0].path : '';
const preview: FilePreview = {
bookId: book.id,
@@ -162,8 +173,12 @@ export class FileMoverComponent implements OnDestroy {
relativeOriginalPath,
currentLibraryId,
currentLibraryName,
currentLibraryPath,
targetLibraryId,
targetLibraryName,
targetLibraryPath,
targetLibraryPathId,
availableLibraryPaths,
newPath: '',
relativeNewPath: ''
};
@@ -174,10 +189,30 @@ export class FileMoverComponent implements OnDestroy {
}
onDefaultLibraryChange(): void {
this.defaultAvailableLibraryPaths = this.getLibraryPathsById(this.defaultTargetLibraryId);
this.defaultTargetLibraryPathId = this.defaultAvailableLibraryPaths.length > 0 ? this.defaultAvailableLibraryPaths[0].id ?? null : null;
this.filePreviews.forEach(preview => {
if (!preview.isMoved) {
preview.targetLibraryId = this.defaultTargetLibraryId;
preview.targetLibraryName = this.getLibraryNameById(this.defaultTargetLibraryId);
preview.availableLibraryPaths = this.defaultAvailableLibraryPaths;
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
this.updatePreviewPaths(preview, book);
}
}
});
}
onDefaultLibraryPathChange(): void {
this.filePreviews.forEach(preview => {
if (!preview.isMoved && preview.targetLibraryId === this.defaultTargetLibraryId) {
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
@@ -189,6 +224,20 @@ export class FileMoverComponent implements OnDestroy {
onLibraryChange(preview: FilePreview): void {
preview.targetLibraryName = this.getLibraryNameById(preview.targetLibraryId);
preview.availableLibraryPaths = this.getLibraryPathsById(preview.targetLibraryId);
preview.targetLibraryPathId = preview.availableLibraryPaths.length > 0 ? preview.availableLibraryPaths[0].id ?? null : null;
preview.targetLibraryPath = preview.availableLibraryPaths.length > 0 ? preview.availableLibraryPaths[0].path : '';
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
this.updatePreviewPaths(preview, book);
}
}
onLibraryPathChange(preview: FilePreview): void {
const selectedPath = preview.availableLibraryPaths.find(p => p.id === preview.targetLibraryPathId);
preview.targetLibraryPath = selectedPath?.path || '';
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
this.updatePreviewPaths(preview, book);
@@ -199,9 +248,7 @@ export class FileMoverComponent implements OnDestroy {
const meta = book.metadata!;
const fileName = book.fileName ?? '';
const extension = fileName.match(/\.[^.]+$/)?.[0] ?? '';
const libraryPattern = this.libraryPatterns.find(p => p.libraryId === preview.targetLibraryId);
const pattern = libraryPattern?.pattern || this.defaultMovePattern;
const pattern = this.getPatternForLibrary(preview.targetLibraryId);
const values: Record<string, string> = {
authors: this.sanitize(meta.authors?.join(', ') || 'Unknown Author'),
@@ -239,13 +286,40 @@ export class FileMoverComponent implements OnDestroy {
preview.newPath = this.getFullPath(preview.targetLibraryId, newPath);
}
private getPatternForLibrary(libraryId: number | null): string {
if (libraryId === null) {
return this.defaultMovePattern;
}
const libraries = this.libraryService.getLibrariesFromState();
const library = libraries.find((lib: Library) => lib.id === libraryId);
return library?.fileNamingPattern || this.defaultMovePattern;
}
private getLibraryNameById(libraryId: number | null): string {
if (libraryId === null) return 'Unknown Library';
return this.availableLibraries.find(lib => lib.id === libraryId)?.name || 'Unknown Library';
}
private getLibraryPathsById(libraryId: number | null): LibraryPath[] {
if (libraryId === null) return [];
const libraries = this.libraryService.getLibrariesFromState();
const library = libraries.find((lib: Library) => lib.id === libraryId);
return library?.paths || [];
}
private getLibraryPathById(libraryId: number | null): string {
const paths = this.getLibraryPathsById(libraryId);
return paths.map((p: LibraryPath) => p.path).join(', ');
}
private getFullPath(libraryId: number | null, relativePath: string): string {
const libraryPath = libraryId ? this.libraryService.getLibraryPathById(libraryId)?.replace(/\/+$/g, '') : '';
if (!libraryId) return relativePath;
const paths = this.getLibraryPathsById(libraryId);
const libraryPath = paths.length > 0 ? paths[0].path.replace(/\/+$/g, '') : '';
return libraryPath ? `${libraryPath}/${relativePath}`.replace(/\/\/+/g, '/') : relativePath;
}
@@ -282,7 +356,8 @@ export class FileMoverComponent implements OnDestroy {
bookIds: [...this.bookIds],
moves: this.filePreviews.map(preview => ({
bookId: preview.bookId,
targetLibraryId: preview.targetLibraryId
targetLibraryId: preview.targetLibraryId,
targetLibraryPathId: preview.targetLibraryPathId
}))
};
@@ -318,4 +393,8 @@ export class FileMoverComponent implements OnDestroy {
togglePatternsCollapsed(): void {
this.patternsCollapsed = !this.patternsCollapsed;
}
trackByBookId(index: number, preview: FilePreview): number {
return preview.bookId;
}
}
@@ -8,6 +8,7 @@ export interface FileMoveRequest {
moves: {
bookId: number;
targetLibraryId: number | null;
targetLibraryPathId: number | null;
}[];
}