Support configurable file/folder naming pattern for uploaded files

This commit is contained in:
adityachandelgit
2025-04-24 21:50:00 -06:00
committed by Aditya Chandel
parent c1d8e84742
commit 767fa835be
18 changed files with 411 additions and 81 deletions
@@ -1,12 +1,9 @@
import {MetadataRefreshOptions} from '../../metadata/model/request/metadata-refresh-options.model';
export interface CoverSettings {
resolution: string;
}
export interface AppSettings {
autoBookSearch: boolean;
similarBookRecommendation: boolean;
metadataRefreshOptions: MetadataRefreshOptions;
coverSettings?: CoverSettings
coverResolution: string;
uploadPattern: string;
}
@@ -0,0 +1,71 @@
<div class="flex flex-col space-y-6 p-4 m-4 custom-border">
<div>
<h2 class="mb-3 text-lg">Enter Custom File Naming Pattern:</h2>
<div class="flex gap-4 items-center">
<input
type="text"
[(ngModel)]="uploadPattern"
class="p-inputtext w-[500px]"
placeholder="e.g., {title} - {authors}"
(input)="onPatternChange(uploadPattern)"
/>
<p-button label="Save" icon="pi pi-check" (onClick)="savePattern()" [disabled]="!!errorMessage"></p-button>
</div>
</div>
<div>
<h3 class="mb-2">Preview:</h3>
<p class="text-lg text-green-500">{{ generatePreview() }}</p>
</div>
<div class="text-gray-400">
<h3 class="mb-1 font-bold">Example Patterns:</h3>
<div class="overflow-x-auto">
<table class=" table-auto">
<colgroup>
<col style="width: auto;"/>
<col style="width: 20px;"/>
<col/>
</colgroup>
<tbody>
<tr>
<td class="pr-2 break-words text-left">&#123;title&#125; - &#123;authors&#125;/filename.pdf</td>
<td class="text-gray-300 text-center">→</td>
<td class="pl-2 break-words text-left">/The Book Thief - Markus Zusak/filename.pdf</td>
</tr>
<tr>
<td class="pr-2 break-words text-left">&#123;title&#125;/&#123;authors&#125;/filename.pdf</td>
<td class="text-gray-300 text-center">→</td>
<td class="pl-2 break-words text-left">/The Book Thief/Markus Zusak/filename.pdf</td>
</tr>
<tr>
<td class="pr-2 break-words text-left">&#123;title&#125; (&#123;authors&#125;)/filename.pdf</td>
<td class="text-gray-300 text-center">→</td>
<td class="pl-2 break-words text-left">/The Book Thief (Markus Zusak)/filename.pdf</td>
</tr>
<tr>
<td class="pr-2 break-words text-left">&#123;title&#125;/filename.pdf</td>
<td class="text-gray-300 text-center">→</td>
<td class="pl-2 break-words text-left">/The Book Thief/filename.pdf</td>
</tr>
<tr>
<td class="pr-2 break-words text-left">EMPTY</td>
<td class="text-gray-300 text-center">→</td>
<td class="pl-2 break-words text-left">/filename.pdf</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="text text-gray-400">
<strong>Note:</strong>
<ul class="list-disc pl-6">
<li>Only the placeholders &#123;title&#125; and &#123;authors&#125; are supported. These values will be extracted from the metadata of the uploaded PDF or EPUB file.</li>
<li>Multiple authors will be joined using commas.</li>
<li>If <code>&#123;bookname&#125;</code> is used but missing, the path defaults to <code>/filename.pdf</code>.</li>
<li>If <code>&#123;authors&#125;</code> is used but missing, it falls back to <code>&#123;bookname&#125;/filename.pdf</code> if the title exists, otherwise <code>/filename.pdf</code>.
</li>
</ul>
</div>
</div>
@@ -0,0 +1,4 @@
.custom-border {
border: 1px solid var(--border-color);
border-radius: var(--card-border);
}
@@ -0,0 +1,71 @@
import {Component, inject, OnInit} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {Button} from 'primeng/button';
import {MessageService} from 'primeng/api';
import {AppSettingsService} from '../core/service/app-settings.service';
import {Observable} from 'rxjs';
import {AppSettings} from '../core/model/app-settings.model';
import {filter, take} from 'rxjs/operators';
@Component({
selector: 'app-file-upload-pattern',
templateUrl: './file-upload-pattern.component.html',
standalone: true,
imports: [FormsModule, Button],
styleUrls: ['./file-upload-pattern.component.scss']
})
export class FileUploadPatternComponent implements OnInit {
title: string = 'The Book Thief';
authors: string = 'Markus Zusak';
uploadPattern: string = '';
errorMessage: string = '';
private appSettingsService = inject(AppSettingsService);
private messageService = inject(MessageService);
appSettings$: Observable<AppSettings | null> = this.appSettingsService.appSettings$;
ngOnInit(): void {
this.appSettings$.pipe(
filter(settings => settings != null),
take(1)
).subscribe(settings => {
this.uploadPattern = settings?.uploadPattern || '';
});
}
generatePreview(): string {
let path = this.uploadPattern
.replace('{title}', this.title)
.replace('{authors}', this.authors)
.replace(/\/+$/, '')
.trim();
return path ? `/${path}/filename.pdf` : '/filename.pdf';
}
validatePattern(pattern: string): void {
const isValid = /^[\w\s\-{}\/().]*$/.test(pattern);
this.errorMessage = isValid ? '' : 'Pattern contains invalid characters.';
}
onPatternChange(pattern: string): void {
this.uploadPattern = pattern;
this.validatePattern(pattern);
}
savePattern(): void {
this.saveSetting('upload_file_pattern', this.uploadPattern);
}
private saveSetting(key: string, value: unknown): void {
this.appSettingsService.saveSettings([{key, newValue: value}]).subscribe({
next: () => this.showMessage('success', 'Settings Saved', 'The settings were successfully saved!'),
error: () => this.showMessage('error', 'Error', 'There was an error saving the settings.')
});
}
private showMessage(severity: 'success' | 'error', summary: string, detail: string): void {
this.messageService.add({severity, summary, detail});
}
}
@@ -71,6 +71,18 @@
<p-divider></p-divider>
<p class="text-xl pb-2 pt-8">
Uploaded Files Pattern
<i class="pi pi-info-circle text-sky-600"
pTooltip="Define a naming pattern for future uploads using available metadata. Existing files will remain unchanged."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<app-file-upload-pattern></app-file-upload-pattern>
<p-divider></p-divider>
<p class="text-xl pb-4 pt-8">
Quick Book Match Preferences
<i class="pi pi-info-circle text-sky-600"
@@ -16,6 +16,7 @@ import {AppSettings} from '../../core/model/app-settings.model';
import {MetadataRefreshOptions} from '../../metadata/model/request/metadata-refresh-options.model';
import {MetadataAdvancedFetchOptionsComponent} from '../../metadata/metadata-options-dialog/metadata-advanced-fetch-options/metadata-advanced-fetch-options.component';
import {filter, take} from 'rxjs/operators';
import {FileUploadPatternComponent} from '../../file-upload-pattern/file-upload-pattern.component';
@Component({
selector: 'app-global-preferences',
@@ -28,7 +29,8 @@ import {filter, take} from 'rxjs/operators';
Tooltip,
ToggleSwitch,
FormsModule,
MetadataAdvancedFetchOptionsComponent
MetadataAdvancedFetchOptionsComponent,
FileUploadPatternComponent
],
templateUrl: './global-preferences.component.html',
styleUrl: './global-preferences.component.scss'
@@ -58,8 +60,8 @@ export class GlobalPreferencesComponent implements OnInit {
filter(settings => settings != null),
take(1)
).subscribe(settings => {
if (settings.coverSettings) {
this.selectedResolution = settings.coverSettings.resolution;
if (settings?.coverResolution) {
this.selectedResolution = settings.coverResolution;
}
if (settings.metadataRefreshOptions) {
this.currentMetadataOptions = settings.metadataRefreshOptions;
+5
View File
@@ -1,5 +1,6 @@
@use "sass:meta";
@import "../node_modules/primeicons/primeicons.css";
@include meta.load-css("assets/layout/styles/global.scss");
@tailwind base;
@@ -10,3 +11,7 @@ html {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
.p-toast {
top: 4rem !important;
}