refactor(frontend): rename booklore-ui to frontend

- Frontend path:
  - rename the tracked Angular app tree from `booklore-ui/` to `frontend/`
  - preserve file contents during the move so this commit stays focused on the path change itself
- Frontend docs:
  - update the moved frontend README and contribution guide where they refer to the renamed folder
  - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
James Cox
2026-04-22 17:26:39 -04:00
committed by Zach Yale
parent c99aa87877
commit 874b013f75
1493 changed files with 3 additions and 3 deletions
@@ -0,0 +1,250 @@
<div class="file-mover-container">
<div class="panel-header">
<div class="header-icon-wrapper">
<i class="pi pi-folder-open header-icon"></i>
</div>
<div class="header-text">
<h2 class="panel-title">Move & Organize Files</h2>
<p class="panel-description">{{ books.length }} book{{ books.length !== 1 ? 's' : '' }} selected</p>
</div>
<p-button
icon="pi pi-times"
[rounded]="true"
[text]="true"
severity="secondary"
(click)="ref.close()"
class="close-button">
</p-button>
</div>
<div class="dialog-content">
<!-- Compact Info Banner -->
<section class="info-banner">
<div class="banner-main">
<div class="banner-icon">
<i class="pi pi-info-circle"></i>
</div>
<div class="banner-content">
<span class="banner-text">
Preview and organize <strong>{{ filePreviews.length }} file{{ filePreviews.length !== 1 ? 's' : '' }}</strong>.
Files will be renamed based on metadata and naming patterns.
</span>
<span class="banner-warning">
<i class="pi pi-exclamation-triangle"></i>
Requires good metadata quality.
</span>
</div>
<button type="button" class="banner-toggle" (click)="toggleInfoCollapsed()">
<span>{{ infoCollapsed ? 'Learn more' : 'Hide' }}</span>
<i [class]="infoCollapsed ? 'pi pi-chevron-down' : 'pi pi-chevron-up'"></i>
</button>
</div>
@if (!infoCollapsed) {
<div class="banner-details">
<div class="detail-item">
<i class="pi pi-check-circle"></i>
<span>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.</span>
</div>
<div class="detail-item">
<i class="pi pi-cog"></i>
<span>Want to change how files are named? Go to <strong>Settings → File Naming Pattern</strong>. Each library can have its own naming pattern!</span>
</div>
<div class="detail-item warning">
<i class="pi pi-exclamation-triangle"></i>
<span>This will actually move and rename files on your computer across different library folders. Make sure you're happy with the preview first!</span>
</div>
<div class="detail-item danger">
<i class="pi pi-shield"></i>
<span>This feature relies entirely on your books' metadata. If your metadata is missing or inconsistent, the resulting file names will be incorrect. Only recommended for well-curated libraries.</span>
</div>
</div>
}
</section>
<!-- Naming Patterns Section -->
<section class="patterns-section">
<button type="button" class="patterns-toggle" (click)="togglePatternsCollapsed()">
<div class="toggle-content">
<i class="pi pi-code"></i>
<span>Naming Rules</span>
<span class="patterns-count">{{ libraryPatterns.length }}</span>
</div>
<i [class]="patternsCollapsed ? 'pi pi-chevron-down' : 'pi pi-chevron-up'"></i>
</button>
@if (!patternsCollapsed) {
<div class="patterns-list">
@for (pattern of libraryPatterns; track pattern.libraryId) {
<div class="pattern-card">
<div class="pattern-header">
<span class="pattern-library">{{ pattern.libraryName }}</span>
<span class="pattern-badge">{{ pattern.bookCount }} book{{ pattern.bookCount > 1 ? 's' : '' }}</span>
</div>
<code class="pattern-code">{{ pattern.pattern || 'No pattern configured' }}</code>
<span class="pattern-source">{{ pattern.source }}</span>
</div>
}
</div>
}
</section>
<!-- Bulk Select Section -->
<section class="bulk-section">
<div class="bulk-header">
<i class="pi pi-list"></i>
<span>Bulk Assignment</span>
</div>
<div class="bulk-controls">
<label class="bulk-label">Set all files to:</label>
<p-select
[options]="availableLibraries"
[(ngModel)]="defaultTargetLibraryId"
optionLabel="name"
optionValue="id"
placeholder="Select library"
(onChange)="onDefaultLibraryChange()"
class="select-library"
size="small"
appendTo="body"
></p-select>
@if (defaultAvailableLibraryPaths.length > 0) {
<p-select
[options]="defaultAvailableLibraryPaths"
[(ngModel)]="defaultTargetLibraryPathId"
optionLabel="path"
optionValue="id"
placeholder="Select path"
(onChange)="onDefaultLibraryPathChange()"
class="select-path"
size="small"
appendTo="body"
></p-select>
}
</div>
</section>
<!-- Table Section -->
<section class="table-section">
<div class="table-header">
<i class="pi pi-table"></i>
<span>File Preview</span>
</div>
<div class="table-container">
<p-table
[value]="filePreviews"
[scrollable]="true"
scrollHeight="flex"
[virtualScroll]="true"
[virtualScrollItemSize]="46"
[rows]="50"
>
<ng-template pTemplate="header">
<tr>
<th style="width: 50px;"></th>
<th style="width: 60px;">ID</th>
<th>Current Path</th>
<th style="width: 160px;">Target Library</th>
<th style="width: 160px;">Library Path</th>
<th style="width: 40px;"></th>
<th>New Path</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-preview>
<tr [class.row-moved]="preview.isMoved">
<td class="status-cell">
@if (preview.isMoved) {
<i class="pi pi-check-circle status-success"></i>
} @else {
<i class="pi pi-clock status-pending"></i>
}
</td>
<td class="id-cell">{{ preview.bookId }}</td>
<td>
<div class="path-cell">
<span class="path-library">{{ preview.currentLibraryName }}/</span>
<span class="path-value">{{ preview.relativeOriginalPath }}</span>
</div>
</td>
<td>
<p-select
[options]="availableLibraries"
[(ngModel)]="preview.targetLibraryId"
optionLabel="name"
optionValue="id"
placeholder="Select"
(onChange)="onLibraryChange(preview)"
[disabled]="preview.isMoved"
class="select-full-width"
appendTo="body"
size="small"
></p-select>
</td>
<td>
@if (preview.availableLibraryPaths.length > 0) {
<p-select
[options]="preview.availableLibraryPaths"
[(ngModel)]="preview.targetLibraryPathId"
optionLabel="path"
optionValue="id"
placeholder="Select"
(onChange)="onLibraryPathChange(preview)"
[disabled]="preview.isMoved"
class="select-full-width"
appendTo="body"
size="small"
></p-select>
} @else {
<span class="no-paths">No paths</span>
}
</td>
<td class="arrow-cell">
<i class="pi pi-arrow-right"></i>
</td>
<td>
<div class="path-cell">
<span class="path-library">{{ preview.targetLibraryName }}/</span>
<span class="path-value">{{ preview.relativeNewPath }}</span>
</div>
</td>
</tr>
</ng-template>
</p-table>
</div>
</section>
</div>
<div class="dialog-footer">
@if (movedFileCount > 0) {
<div class="success-indicator">
<i class="pi pi-check-circle"></i>
<span>{{ movedFileCount }} file{{ movedFileCount > 1 ? 's' : '' }} successfully moved</span>
</div>
}
<div class="footer-actions">
@if (movedFileCount === 0) {
<p-button
label="Cancel"
icon="pi pi-times"
severity="secondary"
[outlined]="true"
(click)="cancel()"
></p-button>
<p-button
[label]="loading ? 'Moving...' : 'Move Files'"
[icon]="loading ? 'pi pi-spin pi-spinner' : 'pi pi-check'"
severity="success"
(click)="saveChanges()"
[disabled]="loading || filePreviews.length === 0"
></p-button>
} @else {
<p-button
label="Close"
icon="pi pi-check"
severity="primary"
(click)="cancel()"
></p-button>
}
</div>
</div>
</div>
@@ -0,0 +1,526 @@
@use '../../styles/panel-shared' as panel;
:host {
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
}
.file-mover-container {
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
}
.panel-header {
@include panel.panel-header;
flex-shrink: 0;
}
.dialog-content {
flex: 1;
display: flex;
flex-direction: column;
overflow-y: auto;
min-height: 0;
padding: 1.25rem;
gap: 1.25rem;
background: var(--card-background);
border: 1px solid var(--border-color);
border-top: none;
border-bottom: none;
&::-webkit-scrollbar {
width: 6px;
}
&::-webkit-scrollbar-track {
background: transparent;
}
&::-webkit-scrollbar-thumb {
background: var(--border-color);
border-radius: 3px;
&:hover {
background: var(--primary-color);
}
}
}
.dialog-footer {
@include panel.dialog-footer;
justify-content: space-between;
}
// Compact Info Banner
.info-banner {
border: 1px solid var(--border-color);
border-radius: 8px;
overflow: hidden;
}
.banner-main {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.875rem 1rem;
}
.banner-icon {
display: flex;
align-items: center;
justify-content: center;
width: 34px;
height: 34px;
background: var(--primary-color);
border-radius: 6px;
flex-shrink: 0;
i {
font-size: 1rem;
color: var(--primary-contrast-color);
}
}
.banner-content {
flex: 1;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.375rem 1rem;
min-width: 0;
}
.banner-text {
font-size: 0.875rem;
color: var(--text-secondary-color);
line-height: 1.4;
strong {
color: var(--text-color);
}
}
.banner-warning {
display: inline-flex;
align-items: center;
gap: 0.375rem;
font-size: 0.8rem;
font-weight: 500;
color: var(--p-orange-400);
i {
font-size: 0.8rem;
}
}
.banner-toggle {
display: flex;
align-items: center;
gap: 0.375rem;
padding: 0.4rem 0.75rem;
background: transparent;
border: 1px solid var(--p-blue-400);
border-radius: 5px;
color: var(--p-blue-400);
font-size: 0.8rem;
cursor: pointer;
transition: all 0.2s ease;
flex-shrink: 0;
&:hover {
background: var(--p-blue-400);
color: white;
}
i {
font-size: 0.7rem;
}
}
.banner-details {
display: flex;
flex-direction: column;
gap: 0.625rem;
padding: 0.875rem 1rem;
border-top: 1px solid var(--border-color);
}
.detail-item {
display: flex;
align-items: flex-start;
gap: 0.5rem;
font-size: 0.85rem;
line-height: 1.4;
color: var(--text-secondary-color);
i {
font-size: 0.85rem;
margin-top: 0.125rem;
color: var(--p-green-400);
flex-shrink: 0;
}
strong {
color: var(--text-color);
}
&.warning {
color: var(--p-yellow-300);
i {
color: var(--p-yellow-400);
}
}
&.danger {
color: var(--p-red-300);
i {
color: var(--p-red-400);
}
}
}
// Patterns Section
.patterns-section {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.patterns-toggle {
display: flex;
align-items: center;
gap: 0.5rem;
width: fit-content;
padding: 0.5rem 0.75rem;
background: transparent;
border: 1px solid var(--border-color);
border-radius: 6px;
cursor: pointer;
transition: all 0.2s ease;
&:hover {
border-color: var(--primary-color);
}
.toggle-content {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.85rem;
font-weight: 500;
color: var(--text-color);
i {
color: var(--primary-color);
}
}
.patterns-count {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 20px;
height: 20px;
padding: 0 0.375rem;
background: var(--primary-color);
color: var(--primary-contrast-color);
border-radius: 10px;
font-size: 0.7rem;
font-weight: 600;
}
> i {
font-size: 0.75rem;
color: var(--text-secondary-color);
}
}
.patterns-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.pattern-card {
display: flex;
flex-direction: column;
gap: 0.375rem;
padding: 0.75rem;
border: 1px solid var(--border-color);
border-radius: 6px;
}
.pattern-header {
display: flex;
align-items: center;
justify-content: space-between;
}
.pattern-library {
font-size: 0.8rem;
font-weight: 600;
color: var(--text-color);
}
.pattern-badge {
display: inline-flex;
align-items: center;
padding: 0.125rem 0.5rem;
background: var(--p-blue-500);
color: white;
border-radius: 10px;
font-size: 0.65rem;
font-weight: 600;
}
.pattern-code {
font-family: 'SF Mono', 'Fira Code', monospace;
font-size: 0.8rem;
color: var(--primary-color);
background: var(--ground-background);
padding: 0.375rem 0.5rem;
border-radius: 4px;
border: 1px solid var(--border-color);
}
.pattern-source {
font-size: 0.7rem;
color: var(--text-secondary-color);
}
// Bulk Section
.bulk-section {
background: var(--ground-background);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 1rem;
}
.bulk-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
font-size: 0.85rem;
font-weight: 500;
color: var(--text-color);
i {
color: var(--primary-color);
}
}
.bulk-controls {
display: flex;
align-items: center;
gap: 0.75rem;
flex-wrap: wrap;
}
.bulk-label {
font-size: 0.8rem;
color: var(--text-secondary-color);
}
// Table Section
.table-section {
background: var(--ground-background);
border: 1px solid var(--border-color);
border-radius: 8px;
overflow: hidden;
flex: 1 1 0;
display: flex;
flex-direction: column;
min-height: 0;
}
.table-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1rem;
background: var(--overlay-background);
border-bottom: 1px solid var(--border-color);
font-size: 0.85rem;
font-weight: 500;
color: var(--text-color);
i {
color: var(--primary-color);
}
}
.table-container {
flex: 1 1 0;
min-height: 0;
overflow: hidden;
::ng-deep .p-datatable {
height: 100%;
display: flex;
flex-direction: column;
.p-datatable-table-container {
flex: 1 1 0;
min-height: 0;
}
}
}
:host ::ng-deep {
.p-datatable-tbody > tr > td {
padding: 0.5rem 0.75rem !important;
}
.p-datatable-thead > tr > th:first-child,
.p-datatable-tbody > tr > td.status-cell {
padding-left: 1.5rem !important;
}
}
.id-cell {
font-family: monospace;
font-size: 0.75rem;
color: var(--text-secondary-color);
}
.path-cell {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 0.8rem;
}
.path-library {
color: var(--text-secondary-color);
}
.path-value {
color: var(--text-color);
}
.no-paths {
font-size: 0.75rem;
color: var(--p-red-400);
font-style: italic;
}
.arrow-cell {
text-align: center;
i {
font-size: 0.75rem;
color: var(--primary-color);
}
}
.status-cell {
text-align: center;
}
.status-success {
color: var(--p-green-500);
font-size: 1rem;
}
.status-pending {
color: var(--p-orange-400);
font-size: 1rem;
}
.row-moved {
background: rgba(34, 197, 94, 0.05) !important;
}
// Footer
.success-indicator {
display: flex;
align-items: center;
gap: 0.5rem;
color: var(--p-green-500);
font-weight: 600;
font-size: 0.9rem;
i {
font-size: 1.125rem;
animation: pulse 2s infinite;
}
}
.footer-actions {
display: flex;
align-items: center;
gap: 0.5rem;
}
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.6;
}
}
// Select widths
:host ::ng-deep {
.select-library {
min-width: 180px;
}
.select-path {
min-width: 220px;
}
.select-full-width {
width: 100%;
}
}
// Responsive
@media (max-width: 768px) {
.dialog-content {
padding: 1rem;
gap: 1rem;
}
.banner-main {
flex-wrap: wrap;
gap: 0.5rem;
}
.banner-content {
flex: 1 1 100%;
order: 1;
}
.banner-icon {
order: 0;
}
.banner-toggle {
order: 2;
margin-left: auto;
}
.bulk-controls {
flex-direction: column;
align-items: flex-start;
}
.dialog-footer {
flex-direction: column;
gap: 0.75rem;
.footer-actions {
width: 100%;
justify-content: flex-end;
}
}
}
@@ -0,0 +1,391 @@
import {Component, effect, inject, OnDestroy} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {Button} from 'primeng/button';
import {TableModule} from 'primeng/table';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {MessageService} from 'primeng/api';
import {takeUntil} from 'rxjs/operators';
import {Subject} from 'rxjs';
import {BookService} from '../../../features/book/service/book.service';
import {Book} from '../../../features/book/model/book.model';
import {FileMoveRequest, FileOperationsService} from '../../service/file-operations.service';
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';
import {replacePlaceholders} from '../../util/pattern-resolver';
interface FilePreview {
bookId: number;
originalPath: string;
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;
}
@Component({
selector: 'app-file-mover-component',
standalone: true,
imports: [Button, FormsModule, TableModule, Select],
templateUrl: './file-mover-component.html',
styleUrl: './file-mover-component.scss'
})
export class FileMoverComponent implements OnDestroy {
private config = inject(DynamicDialogConfig);
ref = inject(DynamicDialogRef);
private bookService = inject(BookService);
private libraryService = inject(LibraryService);
private fileOperationsService = inject(FileOperationsService);
private messageService = inject(MessageService);
private appSettingsService = inject(AppSettingsService);
private destroy$ = new Subject<void>();
private readonly appSettings = this.appSettingsService.appSettings;
private readonly syncLibraryPatternsEffect = effect(() => {
const settings = this.appSettings();
const libraries = this.libraryService.libraries();
if (!settings) {
return;
}
this.defaultMovePattern = settings.uploadPattern || '';
const booksByLibrary = new Map<number | null, Book[]>();
this.books.forEach(book => {
const libraryId =
book.libraryId ??
book.libraryPath?.id ??
(book as { library?: { id: number } }).library?.id ??
null;
if (!booksByLibrary.has(libraryId)) {
booksByLibrary.set(libraryId, []);
}
booksByLibrary.get(libraryId)!.push(book);
});
this.libraryPatterns = Array.from(booksByLibrary.entries()).map(([libraryId, libraryBooks]) => {
let libraryName = 'Unknown Library';
let pattern = this.defaultMovePattern;
let source = 'App Default';
if (libraryId) {
const library = libraries.find(currentLibrary => currentLibrary.id === libraryId);
if (library) {
libraryName = library.name;
if (library.fileNamingPattern) {
pattern = library.fileNamingPattern;
source = 'Library Setting';
}
}
}
return {
libraryId,
libraryName,
pattern,
source,
bookCount: libraryBooks.length
};
});
this.applyPattern();
});
libraryPatterns: {
libraryId: number | null;
libraryName: string;
pattern: string;
source: string;
bookCount: number;
}[] = [];
defaultMovePattern = '';
loading = false;
patternsCollapsed = true;
infoCollapsed = true;
bookIds = new Set<number>();
books: Book[] = [];
filePreviews: FilePreview[] = [];
defaultTargetLibraryId: number | null = null;
defaultTargetLibraryPathId: number | null = null;
defaultAvailableLibraryPaths: LibraryPath[] = [];
get availableLibraries(): { id: number | null; name: string }[] {
return this.libraryService.libraries().map(lib => ({id: lib.id ?? null, name: lib.name}));
}
constructor() {
this.bookIds = new Set(this.config.data?.bookIds ?? []);
this.books = this.bookService.getBooksByIds([...this.bookIds]);
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
applyPattern(): void {
this.filePreviews = this.books.map(book => {
const fileName = book.fileName ?? '';
const fileSubPath = book.fileSubPath ? `${book.fileSubPath.replace(/\/+$/g, '')}/` : '';
const relativeOriginalPath = `${fileSubPath}${fileName}`;
const currentLibraryId = book.libraryId ?? book.libraryPath?.id ?? (book as { library?: { id: number } }).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,
originalPath: this.getFullPath(currentLibraryId, relativeOriginalPath),
relativeOriginalPath,
currentLibraryId,
currentLibraryName,
currentLibraryPath,
targetLibraryId,
targetLibraryName,
targetLibraryPath,
targetLibraryPathId,
availableLibraryPaths,
newPath: '',
relativeNewPath: ''
};
this.updatePreviewPaths(preview, book);
return preview;
});
}
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) {
this.updatePreviewPaths(preview, book);
}
}
});
}
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);
}
}
private updatePreviewPaths(preview: FilePreview, book: Book): void {
const meta = book.metadata!;
const fileName = book.fileName ?? '';
const extension = fileName.match(/\.[^.]+$/)?.[0] ?? '';
const pattern = this.getPatternForLibrary(preview.targetLibraryId);
const values: Record<string, string> = {
authors: this.sanitize(meta.authors?.join(', ') || 'Unknown Author'),
title: this.sanitize(meta.title || 'Untitled'),
subtitle: this.sanitize(meta.subtitle || ''),
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 = replacePlaceholders(pattern, values);
if (!newPath.endsWith(extension)) {
newPath += extension;
}
}
preview.relativeNewPath = newPath;
preview.newPath = this.getFullPath(preview.targetLibraryId, newPath);
}
private getPatternForLibrary(libraryId: number | null): string {
if (libraryId === null) {
return this.defaultMovePattern;
}
const libraries = this.libraryService.libraries();
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.libraries();
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 {
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;
}
get movedFileCount(): number {
return this.filePreviews.filter(p => p.isMoved).length;
}
sanitize(input: string | undefined): string {
const sanitized = (input ?? '')
.replace(/[\\/:*?"<>|]/g, '')
.split('')
.filter(char => {
const code = char.charCodeAt(0);
return code >= 32 && code !== 127;
})
.join('');
return sanitized.replace(/\s+/g, ' ').trim();
}
formatYear(dateStr?: string): string {
if (!dateStr) return '';
const yearMatch = dateStr.match(/^(\d{4})/);
if (yearMatch) {
return yearMatch[1];
}
const date = new Date(dateStr);
return isNaN(date.getTime()) ? '' : date.getUTCFullYear().toString();
}
formatSeriesIndex(seriesNumber?: number): string {
if (seriesNumber == null) return '';
// Check if it's a whole number
if (Number.isInteger(seriesNumber)) {
// Format with leading zero for 1-9
return this.sanitize(seriesNumber.toString().padStart(2, '0'));
} else {
// For decimal numbers, format integer part with leading zero
const intPart = Math.floor(seriesNumber);
const decimalPart = (seriesNumber % 1).toFixed(10).substring(1).replace(/0+$/, '');
return this.sanitize(intPart.toString().padStart(2, '0') + decimalPart);
}
}
saveChanges(): void {
this.loading = true;
const request: FileMoveRequest = {
bookIds: [...this.bookIds],
moves: this.filePreviews.map(preview => ({
bookId: preview.bookId,
targetLibraryId: preview.targetLibraryId,
targetLibraryPathId: preview.targetLibraryPathId
}))
};
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 Organized!',
detail: `Successfully organized ${this.filePreviews.length} file${this.filePreviews.length === 1 ? '' : 's'}.`,
life: 3000
});
},
error: () => {
this.loading = false;
this.messageService.add({
severity: 'error',
summary: 'Oops! Something went wrong',
detail: 'We had trouble organizing your files. Please try again.',
life: 3000
});
}
});
}
cancel(): void {
this.ref.close();
}
togglePatternsCollapsed(): void {
this.patternsCollapsed = !this.patternsCollapsed;
}
toggleInfoCollapsed(): void {
this.infoCollapsed = !this.infoCollapsed;
}
}