Improve robustness of book uploader error handling (#2130)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2026-01-03 16:47:59 -07:00
committed by GitHub
co-authored by acx10
parent d3a0e9ddc9
commit 3b3656c29b
10 changed files with 549 additions and 72 deletions
@@ -86,7 +86,6 @@
</div>
<p-fileupload
name="file"
[maxFileSize]="maxFileSizeBytes"
[customUpload]="true"
[multiple]="true"
accept=".pdf,.epub,.cbz,.cbr,.cb7,.fb2"
@@ -133,7 +132,7 @@
<div class="file-item">
<div class="file-info">
<p-badge
[value]="uploadFile.status === 'Pending' ? 'Ready' : uploadFile.status"
[value]="getFileStatusLabel(uploadFile)"
[severity]="getBadgeSeverity(uploadFile.status)"/>
<span class="file-name">{{ uploadFile.file.name }}</span>
<span class="file-size">{{ formatSize(uploadFile.file.size) }}</span>
@@ -155,11 +154,28 @@
<i class="pi pi-check status-icon success"></i>
}
@case ('Failed') {
<i
class="pi pi-exclamation-triangle status-icon error"
[pTooltip]="uploadFile.errorMessage || 'Upload failed'"
tooltipPosition="top">
</i>
@if (uploadFile.errorMessage?.includes('exceeds maximum size')) {
@if (hasUploadCompleted()) {
<i
class="pi pi-exclamation-triangle status-icon warning"
[pTooltip]="uploadFile.errorMessage || 'File too large'"
tooltipPosition="top">
</i>
} @else {
<p-button
icon="pi pi-times"
(click)="onRemoveTemplatingFile($event, uploadFile.file, removeFileCallback, i)"
severity="danger"
[text]="true"
[rounded]="true"/>
}
} @else {
<i
class="pi pi-exclamation-triangle status-icon error"
[pTooltip]="uploadFile.errorMessage || 'Upload failed'"
tooltipPosition="top">
</i>
}
}
}
</div>
@@ -172,13 +188,13 @@
<ng-template #empty>
<div class="empty-state">
<div class="empty-icon-wrapper">
<i class="pi pi-cloud-upload empty-icon"></i>
<i class="pi pi-cloud-upload empty-icon" [class.active]="isUploadZoneActive()"></i>
</div>
<h3 class="empty-title">Drag and Drop Files</h3>
<p class="empty-description">
Supported formats: <strong>.pdf</strong>, <strong>.epub</strong>, <strong>.cbz</strong>, <strong>.cbr</strong>, <strong>.cb7</strong>, <strong>.fb2</strong>
<br/>
Maximum file size: 100 MB per file
Maximum file size: {{ maxFileSizeDisplay }} per file
</p>
</div>
</ng-template>
@@ -30,7 +30,6 @@
background: var(--primary-color);
border-radius: 10px;
.header-icon {
font-size: 1.5rem;
color: var(--primary-contrast-color);
@@ -270,7 +269,7 @@
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 0.75rem;
padding: 0.25rem 0.75rem;
background: var(--card-background);
border: 1px solid var(--border-color);
border-radius: 8px;
@@ -319,7 +318,11 @@
}
&.success {
color: var(--green-500);
color: #22c55e;
}
&.warning {
color: #f97316;
}
&.error {
@@ -377,11 +380,16 @@
font-size: 2.5rem;
color: var(--text-secondary-color);
opacity: 0.6;
transition: color 0.3s ease;
&.active {
color: #22c55e;
}
}
}
.empty-title {
margin: 0 0 0.5rem 0;
margin: 0 0 1.5rem 0;
font-size: 1.125rem;
font-weight: 600;
color: var(--text-color);
@@ -45,6 +45,7 @@ interface UploadingFile {
export class BookUploaderComponent implements OnInit {
files: UploadingFile[] = [];
isUploading: boolean = false;
uploadCompleted: boolean = false;
_selectedLibrary: Library | null = null;
selectedPath: LibraryPath | null = null;
@@ -57,6 +58,7 @@ export class BookUploaderComponent implements OnInit {
readonly libraryState$: Observable<LibraryState> = this.libraryService.libraryState$;
appSettings$: Observable<AppSettings | null> = this.appSettingsService.appSettings$;
maxFileSizeBytes?: number;
maxFileSizeDisplay: string = '100 MB';
stateOptions = [
{label: 'Library', value: 'library'},
{label: 'Bookdrop', value: 'bookdrop'}
@@ -70,7 +72,9 @@ export class BookUploaderComponent implements OnInit {
take(1)
)
.subscribe(settings => {
this.maxFileSizeBytes = (settings?.maxFileUploadSizeInMb ?? 100) * 1024 * 1024;
const maxSizeMb = settings?.maxFileUploadSizeInMb ?? 100;
this.maxFileSizeBytes = maxSizeMb * 1024 * 1024;
this.maxFileSizeDisplay = `${maxSizeMb} MB`;
});
this.libraryState$.subscribe(state => {
@@ -89,7 +93,7 @@ export class BookUploaderComponent implements OnInit {
set selectedLibrary(library: Library | null) {
this._selectedLibrary = library;
if(library?.paths?.length === 1) {
if (library?.paths?.length === 1) {
this.selectedPath = library.paths[0];
}
}
@@ -115,7 +119,24 @@ export class BookUploaderComponent implements OnInit {
const newFiles = event.currentFiles;
for (const file of newFiles) {
const exists = this.files.some(f => f.file.name === file.name && f.file.size === file.size);
if (!exists) {
if (exists) {
continue;
}
if (this.maxFileSizeBytes && file.size > this.maxFileSizeBytes) {
const errorMsg = `File exceeds maximum size of ${this.formatSize(this.maxFileSizeBytes)}`;
this.files.unshift({
file,
status: 'Failed',
errorMessage: errorMsg
});
this.messageService.add({
severity: 'error',
summary: 'File Too Large',
detail: `${file.name} exceeds the maximum file size of ${this.formatSize(this.maxFileSizeBytes)}`,
life: 5000
});
} else {
this.files.unshift({file, status: 'Pending'});
}
}
@@ -144,6 +165,7 @@ export class BookUploaderComponent implements OnInit {
if (filesToUpload.length === 0) return;
this.isUploading = true;
this.uploadCompleted = false;
const destination = this.value;
const libraryId = this.selectedLibrary?.id?.toString();
const pathId = this.selectedPath?.id?.toString();
@@ -155,6 +177,7 @@ export class BookUploaderComponent implements OnInit {
const batch = files.slice(startIndex, startIndex + batchSize);
if (batch.length === 0) {
this.isUploading = false;
this.uploadCompleted = true;
if (destination === 'bookdrop') {
this.ref.close('uploaded_to_bookdrop');
}
@@ -167,7 +190,6 @@ export class BookUploaderComponent implements OnInit {
uploadFile.status = 'Uploading';
const formData = new FormData();
// Reconstruct the file to ensure strict type and name handling
const cleanFile = new File([uploadFile.file], uploadFile.file.name, {type: uploadFile.file.type});
formData.append('file', cleanFile, uploadFile.file.name);
@@ -216,6 +238,13 @@ export class BookUploaderComponent implements OnInit {
return this.isChooseDisabled() || !this.filesPresent() || !this.hasPendingFiles();
}
isUploadZoneActive(): boolean {
if (this.value === 'bookdrop') {
return true;
}
return !!(this.selectedLibrary && this.selectedPath);
}
formatSize(bytes: number): string {
const k = 1024;
const dm = 2;
@@ -239,6 +268,28 @@ export class BookUploaderComponent implements OnInit {
}
}
getFileStatusLabel(uploadFile: UploadingFile): string {
if (uploadFile.status === 'Failed' && uploadFile.errorMessage?.includes('exceeds maximum size')) {
return 'Too Large';
}
switch (uploadFile.status) {
case 'Pending':
return 'Ready';
case 'Uploading':
return 'Uploading';
case 'Uploaded':
return 'Uploaded';
case 'Failed':
return 'Failed';
default:
return uploadFile.status;
}
}
hasUploadCompleted(): boolean {
return this.uploadCompleted;
}
closeDialog(): void {
this.ref.close();
}