Implement file moving based on library naming rules, falling back to app defaults
This commit is contained in:
committed by
Aditya Chandel
parent
3be321dda4
commit
dd7453fae1
+46
-70
@@ -2,81 +2,57 @@
|
||||
|
||||
<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">
|
||||
Customize Your File Organization
|
||||
File Organization Preview
|
||||
</p>
|
||||
<p class="text-sm text-gray-300 leading-relaxed">
|
||||
Define a naming pattern to reorganize selected book files on your system. Use dynamic placeholders like
|
||||
<code class="bg-gray-800 px-1 py-0.5 rounded text-[var(--primary-color)] font-mono text-xs">{{ '{title}' }}</code>
|
||||
or
|
||||
<code class="bg-gray-800 px-1 py-0.5 rounded text-[var(--primary-color)] font-mono text-xs">{{ '{authors}' }}</code>
|
||||
to automatically insert metadata. <br>
|
||||
A preview below shows how files will be renamed and moved.
|
||||
Once you're satisfied, click <span class="font-semibold text-green-400">Move Files</span> to proceed.
|
||||
<span class="text-yellow-500 font-medium flex items-center gap-2">
|
||||
<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.
|
||||
</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>
|
||||
</p>
|
||||
<p class="text-yellow-500 font-medium flex items-center gap-2">
|
||||
<i class="pi pi-exclamation-triangle text-yellow-400"></i>
|
||||
Files will be physically moved on your system when you apply changes.
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="mb-2 px-1 text-sm text-muted">
|
||||
<p-button
|
||||
size="small"
|
||||
text
|
||||
severity="info"
|
||||
(click)="placeholdersVisible = !placeholdersVisible">
|
||||
{{ placeholdersVisible ? 'Hide' : 'Show' }} available placeholders
|
||||
</p-button>
|
||||
|
||||
@if (placeholdersVisible) {
|
||||
<div class="mx-4 my-2 flex flex-wrap gap-4">
|
||||
<ul class="list-disc pl-4 min-w-[250px]">
|
||||
<li><code>{{ '{title}' }}</code> - Book title</li>
|
||||
<li><code>{{ '{authors}' }}</code> - Author(s)</li>
|
||||
<li><code>{{ '{year}' }}</code> - Full year (e.g. 2025)</li>
|
||||
<li><code>{{ '{series}' }}</code> - Series name</li>
|
||||
<li><code>{{ '{seriesIndex}' }}</code> - Series index (e.g. 01)</li>
|
||||
<li><code>{{ '{language}' }}</code> - Language (e.g. en)</li>
|
||||
<li><code>{{ '{publisher}' }}</code> - Publisher</li>
|
||||
<li><code>{{ '{isbn}' }}</code> - ISBN</li>
|
||||
<li><code>{{ '{currentFilename}' }}</code> - Original file name (with extension)</li>
|
||||
</ul>
|
||||
|
||||
<p-divider layout="vertical"></p-divider>
|
||||
|
||||
<div class="text-sm p-3 min-w-[250px]">
|
||||
<p class="mb-1 font-medium">Tip: Optional placeholders</p>
|
||||
<p>Wrap parts of your pattern in angle brackets <code>{{ '<...>' }}</code> to make them optional.</p>
|
||||
<p>If any placeholder inside has no value, the whole block is removed.</p>
|
||||
<p class="mt-1 font-semibold">Example:</p>
|
||||
<p><code>{{ '<{seriesIndex} - >{title}' }}</code></p>
|
||||
<p class="pl-4 mt-1">• <code>{{ '01 - Dune' }}</code> (if <code>{{ '{seriesIndex}' }}</code> is present)</p>
|
||||
<p class="pl-4">• <code>{{ 'Dune' }}</code> (if <code>{{ '{seriesIndex}' }}</code> is missing)</p>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
This will actually move and rename files on your computer, make sure you're happy with the preview first!
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-4 mb-4 px-2 items-center w-full">
|
||||
<input
|
||||
type="text"
|
||||
pInputText
|
||||
[(ngModel)]="movePattern"
|
||||
(ngModelChange)="applyPattern()"
|
||||
[placeholder]="movePattern.trim() ? '' : getDefaultPattern()"
|
||||
class="flex-1"
|
||||
/>
|
||||
<div class="flex-1">
|
||||
<div class="flex items-center justify-between cursor-pointer" (click)="togglePatternsCollapsed()">
|
||||
<p-button
|
||||
outlined
|
||||
[icon]="patternsCollapsed ? 'pi pi-chevron-right' : 'pi pi-chevron-down'"
|
||||
iconPos="right"
|
||||
size="small"
|
||||
severity="info"
|
||||
[label]="patternsCollapsed ? 'Show Naming Rules' : 'Hide Naming Rules'">
|
||||
</p-button>
|
||||
</div>
|
||||
|
||||
<p-button
|
||||
icon="pi pi-save"
|
||||
label="Make Default"
|
||||
severity="info"
|
||||
outlined
|
||||
(click)="setDefaultPattern()"
|
||||
></p-button>
|
||||
@if (!patternsCollapsed) {
|
||||
<div class="space-y-2 mt-2">
|
||||
@for (pattern of libraryPatterns; track pattern.libraryId) {
|
||||
<div class="p-3 border border-zinc-700 rounded-lg bg-zinc-800">
|
||||
<div class="flex justify-between items-start">
|
||||
<div class="flex-1">
|
||||
<p class="text-xs text-gray-400 mb-1">Library: {{ pattern.libraryName }}</p>
|
||||
<code class="text-[var(--primary-color)] font-mono text-sm">{{ pattern.pattern || 'No pattern available' }}</code>
|
||||
<p class="text-xs text-gray-400 mt-1">Source: {{ pattern.source }}</p>
|
||||
</div>
|
||||
<span class="text-xs bg-blue-600 text-white px-2 py-1 rounded ml-2">{{ pattern.bookCount }} book{{ pattern.bookCount > 1 ? 's' : '' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex-grow overflow-y-auto mt-4">
|
||||
<div class="flex-grow overflow-y-auto">
|
||||
<p-table
|
||||
[value]="filePreviews"
|
||||
[responsiveLayout]="'scroll'"
|
||||
@@ -84,7 +60,7 @@
|
||||
>
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th style="width: 0.5rem; text-align: center;">ID</th>
|
||||
<th>ID</th>
|
||||
<th>Current Path</th>
|
||||
<th></th>
|
||||
<th>New Path</th>
|
||||
@@ -95,13 +71,13 @@
|
||||
<td class="text-center">{{ preview.bookId }}</td>
|
||||
<td class="w-auto">
|
||||
<p>
|
||||
<span class="text-gray-400 select-none">{{ preview.libraryPathPrefix }}/</span><span>{{ preview.relativeOriginalPath }}</span>
|
||||
<span class="text-gray-400 select-none">{{ preview.libraryPathPrefix }}/</span><span class="text-gray-200">{{ preview.relativeOriginalPath }}</span>
|
||||
</p>
|
||||
</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>{{ preview.relativeNewPath }}</span>
|
||||
<span class="text-gray-400 select-none">{{ preview.libraryPathPrefix }}/</span><span class="text-gray-200">{{ preview.relativeNewPath }}</span>
|
||||
</p>
|
||||
</td>
|
||||
<td class="w-auto text-center">
|
||||
|
||||
+99
-69
@@ -1,28 +1,27 @@
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {Component, inject, OnDestroy} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Button} from 'primeng/button';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
import {filter, take, takeUntil} from 'rxjs/operators';
|
||||
import {Subject} from 'rxjs';
|
||||
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
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 {AppSettingKey} from '../../../core/model/app-settings.model';
|
||||
|
||||
@Component({
|
||||
selector: 'app-file-mover-component',
|
||||
standalone: true,
|
||||
imports: [Button, FormsModule, TableModule, InputText, Divider],
|
||||
imports: [Button, FormsModule, TableModule, Divider],
|
||||
templateUrl: './file-mover-component.html',
|
||||
styleUrl: './file-mover-component.scss'
|
||||
})
|
||||
export class FileMoverComponent {
|
||||
export class FileMoverComponent implements OnDestroy {
|
||||
private config = inject(DynamicDialogConfig);
|
||||
private ref = inject(DynamicDialogRef);
|
||||
private bookService = inject(BookService);
|
||||
@@ -30,10 +29,18 @@ export class FileMoverComponent {
|
||||
private fileOperationsService = inject(FileOperationsService);
|
||||
private messageService = inject(MessageService);
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
movePattern = '';
|
||||
placeholdersVisible = false;
|
||||
libraryPatterns: {
|
||||
libraryId: number | null;
|
||||
libraryName: string;
|
||||
pattern: string;
|
||||
source: string;
|
||||
bookCount: number;
|
||||
}[] = [];
|
||||
defaultMovePattern = '';
|
||||
loading = false;
|
||||
patternsCollapsed = false;
|
||||
|
||||
bookIds: Set<number> = new Set();
|
||||
books: Book[] = [];
|
||||
@@ -42,12 +49,69 @@ export class FileMoverComponent {
|
||||
constructor() {
|
||||
this.bookIds = new Set(this.config.data?.bookIds ?? []);
|
||||
this.books = this.bookService.getBooksByIdsFromState([...this.bookIds]);
|
||||
this.loadDefaultPattern();
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
|
||||
private loadDefaultPattern(): void {
|
||||
this.appSettingsService.appSettings$.pipe(
|
||||
filter(settings => settings != null),
|
||||
take(1)
|
||||
take(1),
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe(settings => {
|
||||
this.movePattern = settings?.movePattern;
|
||||
this.defaultMovePattern = settings?.uploadPattern || '';
|
||||
this.loadLibraryPatterns();
|
||||
});
|
||||
}
|
||||
|
||||
private loadLibraryPatterns(): void {
|
||||
this.libraryService.libraryState$.pipe(
|
||||
filter(state => state.loaded && state.libraries != null),
|
||||
take(1),
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe(state => {
|
||||
const booksByLibrary = new Map<number | null, Book[]>();
|
||||
this.books.forEach(book => {
|
||||
const libraryId =
|
||||
book.libraryId ??
|
||||
book.libraryPath?.id ??
|
||||
(book as any).library?.id ??
|
||||
null;
|
||||
if (!booksByLibrary.has(libraryId)) {
|
||||
booksByLibrary.set(libraryId, []);
|
||||
}
|
||||
booksByLibrary.get(libraryId)!.push(book);
|
||||
});
|
||||
|
||||
this.libraryPatterns = Array.from(booksByLibrary.entries()).map(([libraryId, books]) => {
|
||||
let libraryName = 'Unknown Library';
|
||||
let pattern = this.defaultMovePattern;
|
||||
let source = 'App Default';
|
||||
|
||||
if (libraryId) {
|
||||
const library = state.libraries?.find(lib => lib.id === libraryId);
|
||||
if (library) {
|
||||
libraryName = library.name;
|
||||
if (library.fileNamingPattern) {
|
||||
pattern = library.fileNamingPattern;
|
||||
source = 'Library Setting';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
libraryId,
|
||||
libraryName,
|
||||
pattern,
|
||||
source,
|
||||
bookCount: books.length
|
||||
};
|
||||
});
|
||||
|
||||
this.applyPattern();
|
||||
});
|
||||
}
|
||||
@@ -66,6 +130,14 @@ export class FileMoverComponent {
|
||||
: '';
|
||||
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 values: Record<string, string> = {
|
||||
authors: this.sanitize(meta.authors?.join(', ') || 'Unknown Author'),
|
||||
title: this.sanitize(meta.title || 'Untitled'),
|
||||
@@ -80,11 +152,10 @@ export class FileMoverComponent {
|
||||
|
||||
let newPath: string;
|
||||
|
||||
if (!this.movePattern?.trim()) {
|
||||
// If pattern empty, just use current filename as the new path
|
||||
newPath = `${fileSubPath}${fileName}`; // preserve subpath + filename
|
||||
if (!pattern?.trim()) {
|
||||
newPath = `${fileSubPath}${fileName}`;
|
||||
} else {
|
||||
newPath = this.movePattern.replace(/<([^<>]+)>/g, (_, block) => {
|
||||
newPath = pattern.replace(/<([^<>]+)>/g, (_, block) => {
|
||||
const placeholders = [...block.matchAll(/{(.*?)}/g)].map(m => m[1]);
|
||||
const allHaveValues = placeholders.every(key => values[key]?.trim());
|
||||
return allHaveValues
|
||||
@@ -94,7 +165,6 @@ export class FileMoverComponent {
|
||||
|
||||
newPath = newPath.replace(/{(.*?)}/g, (_, key) => values[key] ?? '');
|
||||
|
||||
// Append extension if not already present (most patterns won't include extension)
|
||||
if (!newPath.endsWith(extension)) {
|
||||
newPath += extension;
|
||||
}
|
||||
@@ -111,6 +181,7 @@ export class FileMoverComponent {
|
||||
newPath: fullNewPath,
|
||||
relativeNewPath
|
||||
};
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@@ -136,35 +207,23 @@ export class FileMoverComponent {
|
||||
return this.sanitize(seriesNumber.toString());
|
||||
}
|
||||
|
||||
getDefaultPattern(): string {
|
||||
return 'e.g. {authors}/<{series}/><{seriesIndex}. >{title} - {authors}< ({year})>';
|
||||
}
|
||||
|
||||
saveChanges(): void {
|
||||
if (!this.movePattern?.trim()) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Missing Pattern',
|
||||
detail: 'Move pattern cannot be empty. Please enter a valid pattern.',
|
||||
life: 3000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = true;
|
||||
|
||||
const request: FileMoveRequest = {
|
||||
bookIds: [...this.bookIds],
|
||||
pattern: this.movePattern
|
||||
bookIds: [...this.bookIds]
|
||||
};
|
||||
|
||||
this.fileOperationsService.moveFiles(request).subscribe({
|
||||
this.fileOperationsService.moveFiles(request).pipe(
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe({
|
||||
next: () => {
|
||||
this.loading = false;
|
||||
this.filePreviews.forEach(p => (p.isMoved = true));
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Files Moved',
|
||||
detail: `${this.filePreviews.length} file(s) successfully relocated.`,
|
||||
summary: 'Files Organized!',
|
||||
detail: `Successfully organized ${this.filePreviews.length} file${this.filePreviews.length === 1 ? '' : 's'}.`,
|
||||
life: 3000
|
||||
});
|
||||
},
|
||||
@@ -172,48 +231,19 @@ export class FileMoverComponent {
|
||||
this.loading = false;
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Move Failed',
|
||||
detail: 'An error occurred while moving the files.',
|
||||
summary: 'Oops! Something went wrong',
|
||||
detail: 'We had trouble organizing your files. Please try again.',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setDefaultPattern(): void {
|
||||
const trimmedPattern = this.movePattern?.trim();
|
||||
|
||||
if (!trimmedPattern) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Cannot Save',
|
||||
detail: 'Move pattern cannot be empty.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = [
|
||||
{
|
||||
key: AppSettingKey.MOVE_FILE_PATTERN,
|
||||
newValue: trimmedPattern
|
||||
}
|
||||
];
|
||||
|
||||
this.appSettingsService.saveSettings(payload).subscribe({
|
||||
next: () => this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Saved',
|
||||
detail: 'Move pattern saved as default.'
|
||||
}),
|
||||
error: () => this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to save default move pattern.'
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.ref.close();
|
||||
}
|
||||
|
||||
togglePatternsCollapsed(): void {
|
||||
this.patternsCollapsed = !this.patternsCollapsed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import {API_CONFIG} from '../../config/api-config';
|
||||
|
||||
export interface FileMoveRequest {
|
||||
bookIds: number[];
|
||||
pattern: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
|
||||
Reference in New Issue
Block a user