Add ability to move books across libraries (#1210)

This commit is contained in:
Aditya Chandel
2025-09-25 20:47:12 -06:00
committed by GitHub
parent b510f4cf3f
commit f82b22145c
17 changed files with 583 additions and 148 deletions
@@ -1,4 +1,4 @@
<div class="p-fluid px-4 py-2 rounded-xl flex flex-col h-[800px]">
<div class="p-fluid px-4 py-2 rounded-xl flex flex-col h-full">
<div class="px-4 mx-2 mb-4 py-3 rounded-lg border border-[var(--border-color)] transition-all">
<p class="text-[var(--primary-color)] font-semibold mb-1">
@@ -6,16 +6,16 @@
</p>
<div class="text-sm text-gray-300 space-y-3">
<p>
Your files will be automatically renamed and organized based on your library's naming rules.
Check the preview below to see how your files will look, then click <span class="font-semibold text-green-400">Move Files</span> to organize them.
Your files will be automatically renamed, organized, and can be moved between libraries based on your preferences.
Check the preview below to see how your files will look, select target libraries if needed, then click <span class="font-semibold text-green-400">Move Files</span> to organize them.
</p>
<p>
<i class="pi pi-lightbulb text-blue-400 mr-1"></i>
<span class="text-blue-300">Want to change how files are named? Go to <strong>Settings → File Naming Pattern</strong></span>
<span class="text-blue-300">Want to change how files are named? Go to <strong>Settings → File Naming Pattern</strong>. Each library can have its own naming pattern!</span>
</p>
<p class="text-yellow-500 font-medium flex items-center gap-2">
<i class="pi pi-exclamation-triangle text-yellow-400"></i>
This will actually move and rename files on your computer, make sure you're happy with the preview first!
This will actually move and rename files on your computer across different library folders. Make sure you're happy with the preview first!
</p>
</div>
</div>
@@ -52,6 +52,23 @@
</div>
</div>
<div class="flex gap-4 mb-4 px-2 items-center">
<div class="flex items-center gap-3">
<label class="text-sm font-medium text-gray-300">Set all files to:</label>
<p-select
[options]="availableLibraries"
[(ngModel)]="defaultTargetLibraryId"
optionLabel="name"
optionValue="id"
placeholder="Select default library"
(onChange)="onDefaultLibraryChange()"
class="min-w-[200px]"
size="small"
appendTo="body"
></p-select>
</div>
</div>
<div class="flex-grow overflow-y-auto">
<p-table
[value]="filePreviews"
@@ -62,6 +79,7 @@
<tr>
<th>ID</th>
<th>Current Path</th>
<th>Target Library</th>
<th></th>
<th>New Path</th>
</tr>
@@ -71,13 +89,27 @@
<td class="text-center">{{ preview.bookId }}</td>
<td class="w-auto">
<p>
<span class="text-gray-400 select-none">{{ preview.libraryPathPrefix }}/</span><span class="text-gray-200">{{ preview.relativeOriginalPath }}</span>
<span class="text-gray-400 select-none">{{ preview.currentLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeOriginalPath }}</span>
</p>
</td>
<td class="w-auto">
<p-select
[options]="availableLibraries"
[(ngModel)]="preview.targetLibraryId"
optionLabel="name"
optionValue="id"
placeholder="Select Library"
(onChange)="onLibraryChange(preview)"
[disabled]="preview.isMoved"
class="w-full"
appendTo="body"
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.libraryPathPrefix }}/</span><span class="text-gray-200">{{ preview.relativeNewPath }}</span>
<span class="text-gray-400 select-none">{{ preview.targetLibraryName }}/</span><span class="text-gray-200">{{ preview.relativeNewPath }}</span>
</p>
</td>
<td class="w-auto text-center">
@@ -13,11 +13,25 @@ import {Book} from '../../../book/model/book.model';
import {FileMoveRequest, FileOperationsService} from '../../service/file-operations-service';
import {LibraryService} from "../../../book/service/library.service";
import {AppSettingsService} from '../../../core/service/app-settings.service';
import {Select} from 'primeng/select';
interface FilePreview {
bookId: number;
originalPath: string;
relativeOriginalPath: string;
currentLibraryId: number | null;
currentLibraryName: string;
targetLibraryId: number | null;
targetLibraryName: string;
newPath: string;
relativeNewPath: string;
isMoved?: boolean;
}
@Component({
selector: 'app-file-mover-component',
standalone: true,
imports: [Button, FormsModule, TableModule, Divider],
imports: [Button, FormsModule, TableModule, Divider, Select],
templateUrl: './file-mover-component.html',
styleUrl: './file-mover-component.scss'
})
@@ -44,11 +58,14 @@ export class FileMoverComponent implements OnDestroy {
bookIds: Set<number> = new Set();
books: Book[] = [];
filePreviews: { originalPath: string; newPath: string; isMoved?: boolean }[] = [];
availableLibraries: { id: number | null; name: string }[] = [];
filePreviews: FilePreview[] = [];
defaultTargetLibraryId: number | null = null;
constructor() {
this.bookIds = new Set(this.config.data?.bookIds ?? []);
this.books = this.bookService.getBooksByIdsFromState([...this.bookIds]);
this.loadAvailableLibraries();
this.loadDefaultPattern();
}
@@ -116,6 +133,16 @@ export class FileMoverComponent implements OnDestroy {
});
}
private loadAvailableLibraries(): void {
this.libraryService.libraryState$.pipe(
filter(state => state.loaded && state.libraries != null),
take(1),
takeUntil(this.destroy$)
).subscribe(state => {
this.availableLibraries = state.libraries?.map(lib => ({id: lib.id ?? null, name: lib.name})) || [];
});
}
applyPattern(): void {
this.filePreviews = this.books.map(book => {
const meta = book.metadata!;
@@ -124,67 +151,107 @@ export class FileMoverComponent implements OnDestroy {
const fileSubPath = book.fileSubPath ? `${book.fileSubPath.replace(/\/+$/g, '')}/` : '';
const relativeOriginalPath = `${fileSubPath}${fileName}`;
const libraryPathPrefix =
book.libraryPath?.id != null
? this.libraryService.getLibraryPathById(book.libraryPath.id)?.replace(/\/+$/g, '') ?? ''
: '';
const originalPath = `${libraryPathPrefix}/${relativeOriginalPath}`.replace(/\/\/+/g, '/');
const bookLibraryId =
book.libraryId ??
book.libraryPath?.id ??
(book as any).library?.id ??
null;
const libraryPattern = this.libraryPatterns.find(p => p.libraryId === bookLibraryId);
const pattern = libraryPattern?.pattern || this.defaultMovePattern;
const currentLibraryId = book.libraryId ?? book.libraryPath?.id ?? (book as any).library?.id ?? null;
const currentLibraryName = this.getLibraryNameById(currentLibraryId);
const values: Record<string, string> = {
authors: this.sanitize(meta.authors?.join(', ') || 'Unknown Author'),
title: this.sanitize(meta.title || 'Untitled'),
year: this.formatYear(meta.publishedDate),
series: this.sanitize(meta.seriesName || ''),
seriesIndex: this.formatSeriesIndex(meta.seriesNumber ?? undefined),
language: this.sanitize(meta.language || ''),
publisher: this.sanitize(meta.publisher || ''),
isbn: this.sanitize(meta.isbn13 || meta.isbn10 || ''),
currentFilename: this.sanitize(fileName)
// Initially set target library to current library
const targetLibraryId = currentLibraryId;
const targetLibraryName = currentLibraryName;
const preview: FilePreview = {
bookId: book.id,
originalPath: this.getFullPath(currentLibraryId, relativeOriginalPath),
relativeOriginalPath,
currentLibraryId,
currentLibraryName,
targetLibraryId,
targetLibraryName,
newPath: '',
relativeNewPath: ''
};
let newPath: string;
this.updatePreviewPaths(preview, book);
return preview;
});
}
if (!pattern?.trim()) {
newPath = `${fileSubPath}${fileName}`;
} else {
newPath = pattern.replace(/<([^<>]+)>/g, (_, block) => {
const placeholders = [...block.matchAll(/{(.*?)}/g)].map(m => m[1]);
const allHaveValues = placeholders.every(key => values[key]?.trim());
return allHaveValues
? block.replace(/{(.*?)}/g, (_: string, key: string) => values[key] ?? '')
: '';
});
onDefaultLibraryChange(): void {
this.filePreviews.forEach(preview => {
if (!preview.isMoved) {
preview.targetLibraryId = this.defaultTargetLibraryId;
preview.targetLibraryName = this.getLibraryNameById(this.defaultTargetLibraryId);
newPath = newPath.replace(/{(.*?)}/g, (_, key) => values[key] ?? '');
if (!newPath.endsWith(extension)) {
newPath += extension;
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
this.updatePreviewPaths(preview, book);
}
}
const relativeNewPath = newPath;
const fullNewPath = `${libraryPathPrefix}/${relativeNewPath}`.replace(/\/\/+/g, '/');
return {
bookId: book.id,
originalPath,
relativeOriginalPath,
libraryPathPrefix,
newPath: fullNewPath,
relativeNewPath
};
});
}
onLibraryChange(preview: FilePreview): void {
preview.targetLibraryName = this.getLibraryNameById(preview.targetLibraryId);
const book = this.books.find(b => b.id === preview.bookId);
if (book) {
this.updatePreviewPaths(preview, book);
}
}
private updatePreviewPaths(preview: FilePreview, book: Book): void {
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 values: Record<string, string> = {
authors: this.sanitize(meta.authors?.join(', ') || 'Unknown Author'),
title: this.sanitize(meta.title || 'Untitled'),
year: this.formatYear(meta.publishedDate),
series: this.sanitize(meta.seriesName || ''),
seriesIndex: this.formatSeriesIndex(meta.seriesNumber ?? undefined),
language: this.sanitize(meta.language || ''),
publisher: this.sanitize(meta.publisher || ''),
isbn: this.sanitize(meta.isbn13 || meta.isbn10 || ''),
currentFilename: this.sanitize(fileName)
};
let newPath: string;
if (!pattern?.trim()) {
newPath = fileName;
} else {
newPath = pattern.replace(/<([^<>]+)>/g, (_, block) => {
const placeholders = [...block.matchAll(/{(.*?)}/g)].map(m => m[1]);
const allHaveValues = placeholders.every(key => values[key]?.trim());
return allHaveValues
? block.replace(/{(.*?)}/g, (_: string, key: string) => values[key] ?? '')
: '';
});
newPath = newPath.replace(/{(.*?)}/g, (_, key) => values[key] ?? '');
if (!newPath.endsWith(extension)) {
newPath += extension;
}
}
preview.relativeNewPath = newPath;
preview.newPath = this.getFullPath(preview.targetLibraryId, newPath);
}
private getLibraryNameById(libraryId: number | null): string {
if (libraryId === null) return 'Unknown Library';
return this.availableLibraries.find(lib => lib.id === libraryId)?.name || 'Unknown Library';
}
private getFullPath(libraryId: number | null, relativePath: string): string {
const libraryPath = libraryId ? this.libraryService.getLibraryPathById(libraryId)?.replace(/\/+$/g, '') : '';
return libraryPath ? `${libraryPath}/${relativePath}`.replace(/\/\/+/g, '/') : relativePath;
}
get movedFileCount(): number {
return this.filePreviews.filter(p => p.isMoved).length;
}
@@ -211,7 +278,11 @@ export class FileMoverComponent implements OnDestroy {
this.loading = true;
const request: FileMoveRequest = {
bookIds: [...this.bookIds]
bookIds: [...this.bookIds],
moves: this.filePreviews.map(preview => ({
bookId: preview.bookId,
targetLibraryId: preview.targetLibraryId
}))
};
this.fileOperationsService.moveFiles(request).pipe(
@@ -5,6 +5,10 @@ import {API_CONFIG} from '../../config/api-config';
export interface FileMoveRequest {
bookIds: number[];
moves: {
bookId: number;
targetLibraryId: number | null;
}[];
}
@Injectable({