refactor(frontend): rename booklore-ui to frontend
- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
@if (libraries(); as libraries) {
|
||||
<ng-container *transloco="let t; prefix: 'shared.bookUploader'">
|
||||
<div class="book-uploader">
|
||||
<div class="panel-header">
|
||||
<div class="header-icon-wrapper">
|
||||
<i class="pi pi-cloud-upload header-icon"></i>
|
||||
</div>
|
||||
<div class="header-text">
|
||||
<h2 class="panel-title">{{ t('title') }}</h2>
|
||||
<p class="panel-description">
|
||||
{{ t('description') }}
|
||||
</p>
|
||||
</div>
|
||||
<p-button
|
||||
icon="pi pi-times"
|
||||
(click)="closeDialog()"
|
||||
severity="secondary"
|
||||
[text]="true"
|
||||
[rounded]="true"
|
||||
class="close-button"/>
|
||||
</div>
|
||||
|
||||
<div class="upload-container">
|
||||
<div class="destination-section">
|
||||
<div class="section-header">
|
||||
<span class="section-title">
|
||||
<i class="pi pi-map-marker"></i>
|
||||
{{ t('uploadDestination') }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="destination-selector">
|
||||
<p-selectbutton
|
||||
[options]="stateOptions"
|
||||
[(ngModel)]="value"
|
||||
[allowEmpty]="false"
|
||||
optionLabel="label"
|
||||
optionValue="value">
|
||||
</p-selectbutton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (value === 'library') {
|
||||
<div class="gradient-divider"></div>
|
||||
<div class="library-selection">
|
||||
<div class="section-header">
|
||||
<span class="section-title">
|
||||
<i class="pi pi-building"></i>
|
||||
{{ t('librarySelection') }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="selection-fields">
|
||||
<div class="field-group">
|
||||
<label for="library-select" class="field-label">{{ t('libraryLabel') }}</label>
|
||||
<p-select
|
||||
inputId="library-select"
|
||||
[options]="libraries"
|
||||
optionLabel="name"
|
||||
[placeholder]="t('selectLibrary')"
|
||||
[(ngModel)]="selectedLibrary"
|
||||
class="full-width">
|
||||
</p-select>
|
||||
</div>
|
||||
<div class="field-group">
|
||||
<label for="subpath-select" class="field-label">{{ t('subpathLabel') }}</label>
|
||||
<p-select
|
||||
inputId="subpath-select"
|
||||
[options]="selectedLibrary?.paths"
|
||||
optionLabel="path"
|
||||
[placeholder]="t('selectSubpath')"
|
||||
[(ngModel)]="selectedPath"
|
||||
[disabled]="!selectedLibrary"
|
||||
class="full-width">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="gradient-divider"></div>
|
||||
|
||||
<div class="file-upload-section">
|
||||
<div class="section-header">
|
||||
<span class="section-title">
|
||||
<i class="pi pi-file"></i>
|
||||
{{ t('selectFiles') }}
|
||||
</span>
|
||||
</div>
|
||||
<p-fileupload
|
||||
name="file"
|
||||
[customUpload]="true"
|
||||
[multiple]="true"
|
||||
accept=".pdf,.epub,.cbz,.cbr,.cb7,.fb2,.mobi,.azw,.azw3,.m4b,.m4a,.mp3,.opus"
|
||||
(onSelect)="onFilesSelect($event)"
|
||||
(uploadHandler)="uploadFiles($event)">
|
||||
<ng-template #header let-files let-chooseCallback="chooseCallback" let-clearCallback="clearCallback" let-uploadCallback="uploadCallback">
|
||||
<div class="upload-actions">
|
||||
<div class="action-buttons">
|
||||
<p-button
|
||||
severity="info"
|
||||
(onClick)="choose($event, chooseCallback)"
|
||||
icon="pi pi-images"
|
||||
[label]="t('chooseBtn')"
|
||||
[outlined]="true"
|
||||
[disabled]="isChooseDisabled()"
|
||||
[pTooltip]="t('chooseTooltip')"
|
||||
tooltipPosition="top"/>
|
||||
<p-button
|
||||
(onClick)="uploadEvent(uploadCallback)"
|
||||
icon="pi pi-cloud-upload"
|
||||
[label]="t('uploadBtn')"
|
||||
severity="success"
|
||||
[disabled]="isUploadDisabled()"
|
||||
[pTooltip]="t('uploadTooltip')"
|
||||
tooltipPosition="top"/>
|
||||
<p-button
|
||||
(onClick)="onClear(clearCallback)"
|
||||
icon="pi pi-times"
|
||||
[label]="t('clearBtn')"
|
||||
severity="danger"
|
||||
[outlined]="true"
|
||||
[disabled]="!filesPresent() || isUploading"
|
||||
[pTooltip]="t('clearTooltip')"
|
||||
tooltipPosition="top"/>
|
||||
</div>
|
||||
<small class="upload-hint">{{ t('uploadHint') }}</small>
|
||||
</div>
|
||||
</ng-template>
|
||||
<ng-template #content let-files let-removeFileCallback="removeFileCallback">
|
||||
@if (files?.length > 0) {
|
||||
@if (isUploading || uploadCompleted) {
|
||||
<div class="upload-progress-summary">
|
||||
<div class="progress-row">
|
||||
<span class="progress-status">
|
||||
@if (isUploading) {
|
||||
<i class="pi pi-spin pi-spinner"></i>
|
||||
{{ t('uploading') }}
|
||||
} @else {
|
||||
<i class="pi pi-check-circle"></i>
|
||||
{{ t('complete') }}
|
||||
}
|
||||
</span>
|
||||
<span class="progress-files">
|
||||
{{ t('filesCount', { uploaded: getUploadedCount(), total: this.files.length }) }}
|
||||
@if (getFailedCount() > 0) {
|
||||
<span class="failed-count">{{ t('failedCount', { count: getFailedCount() }) }}</span>
|
||||
}
|
||||
</span>
|
||||
<span class="progress-bytes">{{ formatSize(getUploadedBytes()) }} / {{ formatSize(getTotalBytes()) }}</span>
|
||||
<span class="progress-percent">{{ getOverallProgress() }}%</span>
|
||||
</div>
|
||||
<p-progressbar
|
||||
[value]="getOverallProgress()"
|
||||
[showValue]="false"
|
||||
class="overall-progress-bar"/>
|
||||
</div>
|
||||
}
|
||||
<div class="files-list">
|
||||
@for (uploadFile of this.files; track uploadFile; let i = $index) {
|
||||
<div class="file-item">
|
||||
<div class="file-info">
|
||||
<p-badge
|
||||
[value]="getFileStatusLabel(uploadFile)"
|
||||
[severity]="getBadgeSeverity(uploadFile.status)"/>
|
||||
<span class="file-name" [pTooltip]="uploadFile.file.name" tooltipPosition="top">{{ uploadFile.file.name }}</span>
|
||||
<span class="file-size">{{ formatSize(uploadFile.file.size) }}</span>
|
||||
@if (uploadFile.status === 'Uploading') {
|
||||
<span class="file-progress-text">{{ uploadFile.progress }}%</span>
|
||||
}
|
||||
</div>
|
||||
<div class="file-actions">
|
||||
@switch (uploadFile.status) {
|
||||
@case ('Pending') {
|
||||
<p-button
|
||||
icon="pi pi-times"
|
||||
(click)="onRemoveTemplatingFile($event, uploadFile.file, removeFileCallback, i)"
|
||||
severity="danger"
|
||||
[text]="true"
|
||||
[rounded]="true"/>
|
||||
}
|
||||
@case ('Uploading') {
|
||||
<i class="pi pi-spin pi-spinner status-icon uploading"></i>
|
||||
}
|
||||
@case ('Uploaded') {
|
||||
<i class="pi pi-check status-icon success"></i>
|
||||
}
|
||||
@case ('Failed') {
|
||||
@if (uploadFile.errorMessage?.includes('exceeds maximum size')) {
|
||||
@if (hasUploadCompleted()) {
|
||||
<i
|
||||
class="pi pi-exclamation-triangle status-icon warning"
|
||||
[pTooltip]="uploadFile.errorMessage || t('fileTooLargeTooltip')"
|
||||
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 || t('uploadFailedTooltip')"
|
||||
tooltipPosition="top">
|
||||
</i>
|
||||
}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</ng-template>
|
||||
<ng-template #file></ng-template>
|
||||
<ng-template #empty>
|
||||
<div class="empty-state">
|
||||
<div class="empty-icon-wrapper">
|
||||
<i class="pi pi-cloud-upload empty-icon" [class.active]="isUploadZoneActive()"></i>
|
||||
</div>
|
||||
<h3 class="empty-title">{{ t('dragDropTitle') }}</h3>
|
||||
<p class="empty-description">
|
||||
<span [innerHTML]="t('supportedFormats')"></span>
|
||||
<br/>
|
||||
{{ t('maxFileSize', { size: maxFileSizeDisplay }) }}
|
||||
</p>
|
||||
</div>
|
||||
</ng-template>
|
||||
</p-fileupload>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
}
|
||||
@@ -0,0 +1,472 @@
|
||||
@use '../../styles/panel-shared' as panel;
|
||||
|
||||
.book-uploader {
|
||||
width: 700px;
|
||||
max-width: 700px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
@media (max-width: 768px) {
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.panel-header {
|
||||
@include panel.panel-header;
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.upload-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
padding: 1.75rem;
|
||||
background: var(--card-background);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 0 0 10px 10px;
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 1rem;
|
||||
gap: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.destination-section,
|
||||
.library-selection,
|
||||
.file-upload-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
.section-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 0.625rem 0.875rem;
|
||||
background: var(--overlay-background);
|
||||
border-radius: 6px;
|
||||
|
||||
.section-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-color);
|
||||
|
||||
i {
|
||||
color: var(--primary-color);
|
||||
font-size: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 0.5rem 0.625rem;
|
||||
|
||||
.section-title {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.destination-selector {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.selection-fields {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
padding: 0 0.75rem 1rem 0.75rem;
|
||||
|
||||
.field-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
||||
.field-label {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-color);
|
||||
padding-left: 0.25rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.file-upload-section {
|
||||
::ng-deep .p-fileupload {
|
||||
border: 2px dashed var(--border-color);
|
||||
border-radius: 8px;
|
||||
background: var(--ground-background);
|
||||
transition: border-color 0.3s ease, background-color 0.3s ease;
|
||||
|
||||
.p-fileupload-buttonbar {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.p-fileupload-content {
|
||||
padding: 1rem;
|
||||
background: transparent;
|
||||
border: none;
|
||||
transition: background-color 0.3s ease;
|
||||
|
||||
&.p-fileupload-highlight {
|
||||
background: color-mix(in srgb, var(--primary-color) 5%, transparent);
|
||||
|
||||
.empty-icon-wrapper {
|
||||
border-color: var(--primary-color);
|
||||
|
||||
.empty-icon {
|
||||
color: var(--primary-color);
|
||||
opacity: 1;
|
||||
transform: scale(1.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&:has(.p-fileupload-highlight) {
|
||||
border-color: var(--primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
.upload-actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 1rem;
|
||||
width: 100%;
|
||||
|
||||
.action-buttons {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.upload-hint {
|
||||
color: var(--text-secondary-color);
|
||||
font-size: 0.875rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 0.75rem;
|
||||
gap: 0.5rem;
|
||||
|
||||
.action-buttons {
|
||||
width: 100%;
|
||||
flex-direction: column;
|
||||
|
||||
::ng-deep .p-button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-hint {
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.upload-progress-summary {
|
||||
background: var(--card-background);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
padding: 0.625rem 0.875rem;
|
||||
margin-bottom: 0.5rem;
|
||||
|
||||
.progress-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
font-size: 0.85rem;
|
||||
|
||||
.progress-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.375rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-color);
|
||||
|
||||
i {
|
||||
font-size: 0.9rem;
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
.pi-check-circle {
|
||||
color: #22c55e;
|
||||
}
|
||||
}
|
||||
|
||||
.progress-files {
|
||||
color: var(--text-secondary-color);
|
||||
|
||||
.failed-count {
|
||||
color: #ef4444;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
|
||||
.progress-bytes {
|
||||
color: var(--text-secondary-color);
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.progress-percent {
|
||||
font-weight: 700;
|
||||
color: var(--primary-color);
|
||||
min-width: 42px;
|
||||
text-align: right;
|
||||
}
|
||||
}
|
||||
|
||||
::ng-deep .overall-progress-bar {
|
||||
height: 6px;
|
||||
border-radius: 3px;
|
||||
background: var(--overlay-background);
|
||||
|
||||
.p-progressbar-value {
|
||||
border-radius: 3px;
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 0.5rem 0.75rem;
|
||||
|
||||
.progress-row {
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
font-size: 0.8rem;
|
||||
|
||||
.progress-bytes {
|
||||
margin-left: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.files-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
padding-right: 0.25rem;
|
||||
|
||||
&::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar-track {
|
||||
background: var(--overlay-background);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar-thumb {
|
||||
background: var(--border-color);
|
||||
border-radius: 3px;
|
||||
transition: background 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: var(--border-color) var(--overlay-background);
|
||||
|
||||
.file-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
padding: 0.25rem 0.75rem;
|
||||
background: var(--card-background);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--primary-color);
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.file-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
|
||||
.file-name {
|
||||
flex: 1;
|
||||
font-weight: 500;
|
||||
color: var(--text-color);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.file-size {
|
||||
color: var(--text-secondary-color);
|
||||
font-size: 0.875rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.file-progress-text {
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
color: var(--primary-color);
|
||||
min-width: 40px;
|
||||
text-align: right;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.file-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-shrink: 0;
|
||||
|
||||
.status-icon {
|
||||
font-size: 1.25rem;
|
||||
padding: 0.5rem;
|
||||
|
||||
&.uploading {
|
||||
color: var(--primary-color);
|
||||
}
|
||||
|
||||
&.success {
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
&.warning,
|
||||
&.error {
|
||||
color: #f97316;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 0.625rem;
|
||||
gap: 0.5rem;
|
||||
|
||||
.file-info {
|
||||
gap: 0.5rem;
|
||||
|
||||
.file-name {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.file-size {
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
}
|
||||
|
||||
.file-actions {
|
||||
.status-icon {
|
||||
font-size: 1rem;
|
||||
padding: 0.375rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
padding: 1.5rem 1rem;
|
||||
|
||||
.empty-icon-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
background: var(--overlay-background);
|
||||
border-radius: 50%;
|
||||
margin-bottom: 1rem;
|
||||
border: 3px dashed var(--border-color);
|
||||
transition: border-color 0.3s ease;
|
||||
|
||||
.empty-icon {
|
||||
font-size: 2.5rem;
|
||||
color: var(--text-secondary-color);
|
||||
opacity: 0.6;
|
||||
transition: color 0.3s ease, opacity 0.3s ease, transform 0.3s ease;
|
||||
|
||||
&.active {
|
||||
color: #22c55e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.empty-title {
|
||||
margin: 0 0 1.5rem 0;
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.empty-description {
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary-color);
|
||||
max-width: 450px;
|
||||
line-height: 1.5;
|
||||
|
||||
strong {
|
||||
color: var(--text-color);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
padding: 1.5rem 0.75rem;
|
||||
min-height: 180px;
|
||||
|
||||
.empty-icon-wrapper {
|
||||
width: 70px;
|
||||
height: 70px;
|
||||
margin-bottom: 0.75rem;
|
||||
|
||||
.empty-icon {
|
||||
font-size: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
.empty-title {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.empty-description {
|
||||
font-size: 0.8rem;
|
||||
max-width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
::host ::ng-deep .p-fileupload-content p-progressbar {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.full-width {
|
||||
width: 100%;
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
import {Component, inject, OnInit, ViewChild, effect} from '@angular/core';
|
||||
import {FileSelectEvent, FileUpload, FileUploadHandlerEvent} from 'primeng/fileupload';
|
||||
import {Button} from 'primeng/button';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Select} from 'primeng/select';
|
||||
import {Badge} from 'primeng/badge';
|
||||
import {LibraryService} from '../../../features/book/service/library.service';
|
||||
import {Library, LibraryPath} from '../../../features/book/model/library.model';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {HttpClient, HttpEventType, HttpRequest} from '@angular/common/http';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {AppSettingsService} from '../../service/app-settings.service';
|
||||
import {SelectButton} from 'primeng/selectbutton';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {ProgressBar} from 'primeng/progressbar';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
interface UploadingFile {
|
||||
file: File;
|
||||
status: 'Pending' | 'Uploading' | 'Uploaded' | 'Failed';
|
||||
progress: number;
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
type FileChooserCallback = () => void;
|
||||
type FileRemoveCallback = (event: Event, index: number) => void;
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-uploader',
|
||||
standalone: true,
|
||||
imports: [
|
||||
FileUpload,
|
||||
Button,
|
||||
FormsModule,
|
||||
Select,
|
||||
Badge,
|
||||
Tooltip,
|
||||
SelectButton,
|
||||
ProgressBar,
|
||||
TranslocoDirective
|
||||
],
|
||||
templateUrl: './book-uploader.component.html',
|
||||
styleUrl: './book-uploader.component.scss'
|
||||
})
|
||||
export class BookUploaderComponent implements OnInit {
|
||||
@ViewChild(FileUpload) fileUpload!: FileUpload;
|
||||
|
||||
files: UploadingFile[] = [];
|
||||
isUploading: boolean = false;
|
||||
uploadCompleted: boolean = false;
|
||||
_selectedLibrary: Library | null = null;
|
||||
selectedPath: LibraryPath | null = null;
|
||||
|
||||
private readonly libraryService = inject(LibraryService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly appSettingsService = inject(AppSettingsService);
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly ref = inject(DynamicDialogRef);
|
||||
private readonly t = inject(TranslocoService);
|
||||
|
||||
readonly libraries = this.libraryService.libraries;
|
||||
maxFileSizeBytes?: number;
|
||||
maxFileSizeDisplay: string = '100 MB';
|
||||
stateOptions = [
|
||||
{label: this.t.translate('shared.bookUploader.destinationLibrary'), value: 'library'},
|
||||
{label: this.t.translate('shared.bookUploader.destinationBookdrop'), value: 'bookdrop'}
|
||||
];
|
||||
value = 'library';
|
||||
private readonly selectSingleLibraryEffect = effect(() => {
|
||||
const libraries = this.libraries();
|
||||
if (libraries.length !== 1 || this.selectedLibrary) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedLibrary = libraries[0];
|
||||
});
|
||||
|
||||
private readonly loadSettingsEffect = effect(() => {
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
if (!settings) return;
|
||||
const maxSizeMb = settings.maxFileUploadSizeInMb ?? 100;
|
||||
this.maxFileSizeBytes = maxSizeMb * 1024 * 1024;
|
||||
this.maxFileSizeDisplay = `${maxSizeMb} MB`;
|
||||
});
|
||||
|
||||
ngOnInit(): void {
|
||||
}
|
||||
|
||||
get selectedLibrary(): Library | null {
|
||||
return this._selectedLibrary;
|
||||
}
|
||||
|
||||
set selectedLibrary(library: Library | null) {
|
||||
this._selectedLibrary = library;
|
||||
|
||||
if (library?.paths?.length === 1) {
|
||||
this.selectedPath = library.paths[0];
|
||||
}
|
||||
}
|
||||
|
||||
hasPendingFiles(): boolean {
|
||||
return this.files.some(f => f.status === 'Pending');
|
||||
}
|
||||
|
||||
filesPresent(): boolean {
|
||||
return this.files.length > 0;
|
||||
}
|
||||
|
||||
choose(_event: Event, chooseCallback: FileChooserCallback): void {
|
||||
chooseCallback();
|
||||
}
|
||||
|
||||
onClear(clearCallback: () => void): void {
|
||||
clearCallback();
|
||||
this.files = [];
|
||||
}
|
||||
|
||||
onFilesSelect(event: FileSelectEvent): void {
|
||||
if (this.value === 'library' && (!this.selectedLibrary || !this.selectedPath)) {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: this.t.translate('shared.bookUploader.toast.noDestinationSummary'),
|
||||
detail: this.t.translate('shared.bookUploader.toast.noDestinationDetail'),
|
||||
life: 5000
|
||||
});
|
||||
// We need to clear the files input explicitely, otherwise the files remain selected in the file upload component
|
||||
this.fileUpload.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
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',
|
||||
progress: 0,
|
||||
errorMessage: errorMsg
|
||||
});
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: this.t.translate('shared.bookUploader.toast.fileTooLargeSummary'),
|
||||
detail: this.t.translate('shared.bookUploader.toast.fileTooLargeDetail', {fileName: file.name, maxSize: this.formatSize(this.maxFileSizeBytes)}),
|
||||
life: 5000
|
||||
});
|
||||
} else {
|
||||
this.files.unshift({file, status: 'Pending', progress: 0});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onRemoveTemplatingFile(event: Event, file: File, removeFileCallback: FileRemoveCallback, _index?: number): void {
|
||||
// Remove from our tracking array
|
||||
this.files = this.files.filter(f => f.file !== file);
|
||||
|
||||
// Find and remove from p-fileupload's internal array (index may differ from ours)
|
||||
const fileUploadIndex = this.fileUpload.files?.findIndex(f => f.name === file.name && f.size === file.size) ?? -1;
|
||||
if (fileUploadIndex >= 0) {
|
||||
removeFileCallback(event, fileUploadIndex);
|
||||
}
|
||||
}
|
||||
|
||||
uploadEvent(uploadCallback: () => void): void {
|
||||
uploadCallback();
|
||||
}
|
||||
|
||||
uploadFiles(_event: FileUploadHandlerEvent): void {
|
||||
if (this.value === 'library' && (!this.selectedLibrary || !this.selectedPath)) {
|
||||
this.messageService.add({
|
||||
severity: 'warn',
|
||||
summary: this.t.translate('shared.bookUploader.toast.missingDataSummary'),
|
||||
detail: this.t.translate('shared.bookUploader.toast.missingDataDetail'),
|
||||
life: 4000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const filesToUpload = this.files.filter(f => f.status === 'Pending');
|
||||
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();
|
||||
|
||||
this.uploadBatch(filesToUpload, 0, 1, destination, libraryId, pathId);
|
||||
}
|
||||
|
||||
private uploadBatch(files: UploadingFile[], startIndex: number, batchSize: number, destination: string, libraryId?: string, pathId?: string): void {
|
||||
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');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let pending = batch.length;
|
||||
|
||||
for (const uploadFile of batch) {
|
||||
uploadFile.status = 'Uploading';
|
||||
uploadFile.progress = 0;
|
||||
|
||||
const formData = new FormData();
|
||||
const cleanFile = new File([uploadFile.file], uploadFile.file.name, {type: uploadFile.file.type});
|
||||
formData.append('file', cleanFile, uploadFile.file.name);
|
||||
|
||||
let uploadUrl: string;
|
||||
if (destination === 'library') {
|
||||
if (libraryId && pathId) {
|
||||
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`;
|
||||
}
|
||||
|
||||
const req = new HttpRequest('POST', uploadUrl, formData, {
|
||||
reportProgress: true
|
||||
});
|
||||
|
||||
this.http.request(req).subscribe({
|
||||
next: (event) => {
|
||||
if (event.type === HttpEventType.UploadProgress && event.total) {
|
||||
uploadFile.progress = Math.round((event.loaded / event.total) * 100);
|
||||
} else if (event.type === HttpEventType.Response) {
|
||||
uploadFile.status = 'Uploaded';
|
||||
uploadFile.progress = 100;
|
||||
if (--pending === 0) {
|
||||
setTimeout(() => {
|
||||
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
uploadFile.status = 'Failed';
|
||||
uploadFile.progress = 0;
|
||||
uploadFile.errorMessage = err?.error?.message || this.t.translate('shared.bookUploader.toast.uploadFailedDefault');
|
||||
if (--pending === 0) {
|
||||
setTimeout(() => {
|
||||
this.uploadBatch(files, startIndex + batchSize, batchSize, destination, libraryId, pathId);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
isUploadZoneActive(): boolean {
|
||||
if (this.value === 'bookdrop') {
|
||||
return true;
|
||||
}
|
||||
return !!(this.selectedLibrary && this.selectedPath);
|
||||
}
|
||||
|
||||
formatSize(bytes: number): string {
|
||||
const k = 1024;
|
||||
const dm = 2;
|
||||
if (bytes < k) return `${bytes} B`;
|
||||
if (bytes < k * k) return `${(bytes / k).toFixed(dm)} KB`;
|
||||
return `${(bytes / (k * k)).toFixed(dm)} MB`;
|
||||
}
|
||||
|
||||
getBadgeSeverity(status: UploadingFile['status']): 'info' | 'warn' | 'success' | 'danger' {
|
||||
switch (status) {
|
||||
case 'Pending':
|
||||
return 'warn';
|
||||
case 'Uploading':
|
||||
return 'info';
|
||||
case 'Uploaded':
|
||||
return 'success';
|
||||
case 'Failed':
|
||||
return 'danger';
|
||||
default:
|
||||
return 'info';
|
||||
}
|
||||
}
|
||||
|
||||
getFileStatusLabel(uploadFile: UploadingFile): string {
|
||||
if (uploadFile.status === 'Failed' && uploadFile.errorMessage?.includes('exceeds maximum size')) {
|
||||
return this.t.translate('shared.bookUploader.statusTooLarge');
|
||||
}
|
||||
switch (uploadFile.status) {
|
||||
case 'Pending':
|
||||
return this.t.translate('shared.bookUploader.statusReady');
|
||||
case 'Uploading':
|
||||
return this.t.translate('shared.bookUploader.statusUploading');
|
||||
case 'Uploaded':
|
||||
return this.t.translate('shared.bookUploader.statusUploaded');
|
||||
case 'Failed':
|
||||
return this.t.translate('shared.bookUploader.statusFailed');
|
||||
default:
|
||||
return uploadFile.status;
|
||||
}
|
||||
}
|
||||
|
||||
hasUploadCompleted(): boolean {
|
||||
return this.uploadCompleted;
|
||||
}
|
||||
|
||||
closeDialog(): void {
|
||||
this.ref.close();
|
||||
}
|
||||
|
||||
getOverallProgress(): number {
|
||||
if (this.files.length === 0) return 0;
|
||||
const totalProgress = this.files.reduce((sum, f) => sum + f.progress, 0);
|
||||
return Math.round(totalProgress / this.files.length);
|
||||
}
|
||||
|
||||
getUploadedCount(): number {
|
||||
return this.files.filter(f => f.status === 'Uploaded').length;
|
||||
}
|
||||
|
||||
getFailedCount(): number {
|
||||
return this.files.filter(f => f.status === 'Failed').length;
|
||||
}
|
||||
|
||||
getUploadingCount(): number {
|
||||
return this.files.filter(f => f.status === 'Uploading').length;
|
||||
}
|
||||
|
||||
getTotalBytes(): number {
|
||||
return this.files.reduce((sum, f) => sum + f.file.size, 0);
|
||||
}
|
||||
|
||||
getUploadedBytes(): number {
|
||||
return this.files
|
||||
.filter(f => f.status === 'Uploaded')
|
||||
.reduce((sum, f) => sum + f.file.size, 0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user