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
@@ -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});
}
}