Allow book upload GUI to upload into Bookdrop

This commit is contained in:
aditya.chandel
2025-08-09 00:50:50 -06:00
committed by Aditya Chandel
parent 1b58b38cbe
commit fb5a70c7e7
10 changed files with 401 additions and 66 deletions
+4 -4
View File
@@ -28,7 +28,7 @@
"ngx-extended-pdf-viewer": "^23.3.1",
"ngx-infinite-scroll": "^19.0.0",
"primeicons": "^7.0.0",
"primeng": "19.1.2",
"primeng": "19.1.4",
"quill": "^2.0.3",
"rxjs": "^7.8.2",
"showdown": "^2.1.0",
@@ -14289,9 +14289,9 @@
"license": "MIT"
},
"node_modules/primeng": {
"version": "19.1.2",
"resolved": "https://registry.npmjs.org/primeng/-/primeng-19.1.2.tgz",
"integrity": "sha512-iu4lDrStBPCyYn+iSfofp/Hsv/dHVlnlsOhqFZlYTQB1o+MlVXa4PanLgHdoBr1thy3ozRkNDZHsICkUecWu0A==",
"version": "19.1.4",
"resolved": "https://registry.npmjs.org/primeng/-/primeng-19.1.4.tgz",
"integrity": "sha512-l5l8SHTxopxxyyXZx1BvbQ11P7ndLv2Qp8H5k2/+OCi65jTZn4xmtrBDGDs7k2K5UMHSqAnGjBgtpbckyqQETg==",
"license": "SEE LICENSE IN LICENSE.md",
"dependencies": {
"@primeuix/styled": "^0.3.2",
+1 -1
View File
@@ -32,7 +32,7 @@
"ngx-extended-pdf-viewer": "^23.3.1",
"ngx-infinite-scroll": "^19.0.0",
"primeicons": "^7.0.0",
"primeng": "19.1.2",
"primeng": "19.1.4",
"quill": "^2.0.3",
"rxjs": "^7.8.2",
"showdown": "^2.1.0",
@@ -1,26 +1,38 @@
@if ((libraryState$ | async)?.libraries; as libraries) {
<div
class="flex flex-col gap-10 p-4 items-center justify-center w-full max-w-[700px]">
<p-select
[options]="libraries"
optionLabel="name"
placeholder="Select Library"
[(ngModel)]="selectedLibrary"
class="w-full"></p-select>
<p-select
[options]="selectedLibrary?.paths"
optionLabel="path"
placeholder="Select Upload Location"
[(ngModel)]="selectedPath"
[disabled]="!selectedLibrary"
class="w-full"></p-select>
class="flex flex-col gap-8 p-4 items-center justify-center w-full max-w-[700px]">
<div class="flex flex-col items-center gap-2 w-full">
<label class="text-sm text-stone-400">Upload Destination</label>
<p-selectbutton
[options]="stateOptions"
[(ngModel)]="value"
[allowEmpty]="false"
optionLabel="label"
optionValue="value">
</p-selectbutton>
</div>
@if (value === 'library') {
<p-select
[options]="libraries"
optionLabel="name"
placeholder="Select Library"
[(ngModel)]="selectedLibrary"
class="w-full"></p-select>
<p-select
[options]="selectedLibrary?.paths"
optionLabel="path"
placeholder="Select Subpath"
[(ngModel)]="selectedPath"
[disabled]="!selectedLibrary"
class="w-full"></p-select>
}
<p-fileupload class="w-full" name="file"
[maxFileSize]="maxFileSizeBytes"
[customUpload]="true"
[multiple]="true" accept=".pdf,.epub,.cbz,.cbr,.cb7"
(onSelect)="onFilesSelect($event)"
(uploadHandler)="uploadFiles($event)"
[disabled]="!selectedLibrary || !selectedPath">
[disabled]="value === 'library' ? (!selectedLibrary || !selectedPath) : false">
<ng-template #header let-files let-chooseCallback="chooseCallback" let-clearCallback="clearCallback" let-uploadCallback="uploadCallback">
<div class="flex flex-col items-center justify-center flex-1 gap-4">
<div class="flex gap-4">
@@ -1,7 +1,7 @@
import {Component, inject, OnInit} from '@angular/core';
import {FileSelectEvent, FileUpload, FileUploadHandlerEvent} from 'primeng/fileupload';
import {Button} from 'primeng/button';
import { AsyncPipe } from '@angular/common';
import {AsyncPipe} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {MessageService} from 'primeng/api';
import {Select} from 'primeng/select';
@@ -17,6 +17,7 @@ import {Tooltip} from 'primeng/tooltip';
import {AppSettingsService} from '../../../core/service/app-settings.service';
import {filter, take} from 'rxjs/operators';
import {AppSettings} from '../../../core/model/app-settings.model';
import {SelectButton} from 'primeng/selectbutton';
interface UploadingFile {
file: File;
@@ -34,8 +35,9 @@ interface UploadingFile {
FormsModule,
Select,
Badge,
Tooltip
],
Tooltip,
SelectButton
],
templateUrl: './book-uploader.component.html',
styleUrl: './book-uploader.component.scss'
})
@@ -53,6 +55,11 @@ export class BookUploaderComponent implements OnInit {
readonly libraryState$: Observable<LibraryState> = this.libraryService.libraryState$;
appSettings$: Observable<AppSettings | null> = this.appSettingsService.appSettings$;
maxFileSizeBytes?: number;
stateOptions = [
{label: 'Library', value: 'library'},
{label: 'Bookdrop', value: 'bookdrop'}
];
value = 'library';
ngOnInit(): void {
this.appSettings$
@@ -101,7 +108,7 @@ export class BookUploaderComponent implements OnInit {
}
uploadFiles(event: FileUploadHandlerEvent): void {
if (!this.selectedLibrary || !this.selectedPath) {
if (this.value === 'library' && (!this.selectedLibrary || !this.selectedPath)) {
this.messageService.add({
severity: 'warn',
summary: 'Missing Data',
@@ -111,34 +118,52 @@ export class BookUploaderComponent implements OnInit {
return;
}
const libraryId = this.selectedLibrary.id!.toString();
const pathId = this.selectedPath.id!.toString();
const filesToUpload = this.files.filter(f => f.status === 'Pending');
if (filesToUpload.length === 0) return;
this.isUploading = true;
let pending = filesToUpload.length;
this.uploadBatch(filesToUpload, 0, 5);
}
for (const uploadFile of filesToUpload) {
private uploadBatch(files: UploadingFile[], startIndex: number, batchSize: number): void {
const batch = files.slice(startIndex, startIndex + batchSize);
if (batch.length === 0) {
this.isUploading = false;
return;
}
let pending = batch.length;
for (const uploadFile of batch) {
uploadFile.status = 'Uploading';
const formData = new FormData();
formData.append('file', uploadFile.file);
formData.append('libraryId', libraryId);
formData.append('pathId', pathId);
this.http.post<Book>(`${API_CONFIG.BASE_URL}/api/v1/files/upload`, formData).subscribe({
let uploadUrl: string;
if (this.value === 'library') {
const libraryId = this.selectedLibrary!.id!.toString();
const pathId = this.selectedPath!.id!.toString();
formData.append('libraryId', libraryId);
formData.append('pathId', pathId);
uploadUrl = `${API_CONFIG.BASE_URL}/api/v1/files/upload`;
} else {
uploadUrl = `${API_CONFIG.BASE_URL}/api/v1/files/upload/bookdrop`;
}
this.http.post<Book>(uploadUrl, formData).subscribe({
next: () => {
uploadFile.status = 'Uploaded';
if (--pending === 0) this.isUploading = false;
if (--pending === 0) {
this.uploadBatch(files, startIndex + batchSize, batchSize);
}
},
error: (err) => {
uploadFile.status = 'Failed';
uploadFile.errorMessage = err?.error?.message || 'Upload failed due to unknown error.';
console.error('Upload failed for', uploadFile.file.name, err);
if (--pending === 0) {
this.isUploading = false;
this.uploadBatch(files, startIndex + batchSize, batchSize);
}
}
});
@@ -146,11 +171,14 @@ export class BookUploaderComponent implements OnInit {
}
isChooseDisabled(): boolean {
if (this.value === 'bookdrop') {
return this.isUploading;
}
return !this.selectedLibrary || !this.selectedPath || this.isUploading;
}
isUploadDisabled(): boolean {
return this.isChooseDisabled() || !this.filesPresent() || !this.hasPendingFiles;
return this.isChooseDisabled() || !this.filesPresent() || !this.hasPendingFiles();
}
formatSize(bytes: number): string {
@@ -11,7 +11,7 @@
<code class="bg-gray-800 px-1 py-0.5 rounded text-[var(--primary-color)] font-mono text-xs">{{ '{authors}' }}</code>
to automatically insert metadata. <br>
A preview below shows how files will be renamed and moved.
Once you're satisfied, click <span class="font-semibold text-green-400">Apply</span> to proceed.
Once you're satisfied, click <span class="font-semibold text-green-400">Move Files</span> to proceed.
<span class="text-yellow-500 font-medium flex items-center gap-2">
<i class="pi pi-exclamation-triangle text-yellow-400"></i>
Files will be physically moved on your system when you apply changes.
@@ -57,7 +57,7 @@
}
</div>
<div class="flex gap-2 mb-4 px-2 items-center w-full">
<div class="flex gap-4 mb-4 px-2 items-center w-full">
<input
type="text"
pInputText
@@ -116,7 +116,7 @@
<p-divider></p-divider>
<div class="flex justify-end items-center gap-4 flex-shrink-0 px-4">
<div class="flex justify-end items-center gap-4 flex-shrink-0 px-4 pt-4">
@if (movedFileCount > 0) {
<span class="text-green-500 flex items-center font-semibold drop-shadow-sm select-none">
{{ movedFileCount }} file{{ movedFileCount > 1 ? 's' : '' }} successfully moved