Support configurable file/folder naming pattern for uploaded files
This commit is contained in:
committed by
Aditya Chandel
parent
c1d8e84742
commit
767fa835be
@@ -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">{title} - {authors}/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">{title}/{authors}/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">{title} ({authors})/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">{title}/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 {title} and {authors} 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>{bookname}</code> is used but missing, the path defaults to <code>/filename.pdf</code>.</li>
|
||||
<li>If <code>{authors}</code> is used but missing, it falls back to <code>{bookname}/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});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user