Add file upload functionality

This commit is contained in:
aditya.chandel
2024-12-16 16:50:49 -07:00
parent 7973d309de
commit 696649a24f
34 changed files with 508 additions and 141 deletions
+1
View File
@@ -1 +1,2 @@
<p-toast></p-toast>
<router-outlet></router-outlet>
+3 -4
View File
@@ -17,13 +17,11 @@ import {InputIconModule} from 'primeng/inputicon';
import {ToggleButtonModule} from 'primeng/togglebutton';
import {PasswordModule} from 'primeng/password';
import {ToastModule} from 'primeng/toast';
import {LibraryBrowserComponent} from './book/component/library-browser/library-browser.component';
import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
import {SearchComponent} from './book/component/search/search.component';
import { BookMetadataComponent } from './book/component/book-metadata/book-metadata.component';
import { BooksMetadataDialogComponent } from './book/component/books-metadata-dialog/books-metadata-dialog.component';
import {MessageService} from 'primeng/api';
import { NotificationComponent } from './book/component/notification/notification.component';
import {FileUploadComponent} from './book/component/file-upload/file-upload.component';
import {DropdownModule} from 'primeng/dropdown';
@NgModule({
declarations: [
@@ -47,6 +45,7 @@ import { NotificationComponent } from './book/component/notification/notificatio
ToastModule,
InfiniteScrollDirective,
SearchComponent,
DropdownModule,
],
providers: [DialogService, MessageService],
bootstrap: [AppComponent]
@@ -1,10 +1,10 @@
.book-metadata-container {
height: calc(100vh - 10rem); /* Full height minus header and footer */
overflow-y: auto; /* Enable vertical scrolling */
transition: transform 0.2s, left 0.2s; /* Smooth transition for movements */
height: calc(100vh - 10rem);
overflow-y: auto;
transition: transform 0.2s, left 0.2s;
background-color: var(--surface-card);
border-radius: 12px;
padding: 1.5rem 2rem; /* Adjust padding */
padding: 1.5rem 2rem;
}
.book-metadata {
@@ -33,7 +33,7 @@
.title-row {
display: flex;
justify-content: space-between; /* Ensures title and buttons are spaced out */
justify-content: space-between;
align-items: center;
margin-bottom: 0.6rem;
}
@@ -46,7 +46,7 @@
.button-group {
display: flex;
gap: 1.5rem; /* Spacing between buttons */
gap: 1.5rem;
}
.p-button-sm {
@@ -1,7 +1,10 @@
<div *ngIf="!isLoading && bookMetadataList.length > 0" class="search-container">
<div class="input-group">
<input type="text" [(ngModel)]="searchText" placeholder="Enter book title"/>
<p-button severity="success" (click)="populateTitle()">Search</p-button>
<p-button
severity="success"
[disabled]="isButtonDisabled"
(click)="populateTitle()">Search</p-button>
</div>
</div>
@@ -5,13 +5,13 @@
.input-group {
display: flex;
justify-content: space-between;
gap: 10px; /* Add space between the text box and button */
gap: 10px;
}
input[type="text"] {
padding: 10px;
font-size: 1rem;
width: 90%; /* Adjust width as necessary */
width: 90%;
border: 1px solid #ddd;
border-radius: 5px;
}
@@ -43,22 +43,22 @@ button:focus {
.metadata-header {
display: flex;
gap: 20px; /* Space between image and text */
gap: 20px;
}
.book-image {
width: 100px; /* Adjust the width as needed */
height: 150px; /* Adjust the height as needed */
object-fit: cover; /* Ensure the image covers the area without distorting */
width: 100px;
height: 150px;
object-fit: cover;
border-radius: 5px;
margin-bottom: 10px; /* Add spacing between the image and the button */
margin-bottom: 10px;
}
.select-button {
width: auto; /* Button width adjusts to content */
padding: 8px 15px; /* Adjust the button padding */
margin-top: 10px; /* Space between the image and button */
text-align: center; /* Ensure text is centered in the button */
width: auto;
padding: 8px 15px;
margin-top: 10px;
text-align: center;
}
.book-metadata {
@@ -23,6 +23,7 @@ export class BooksMetadataDialogComponent implements OnInit {
isLoading = true;
errorMessage: string | null = null;
searchText: string = '';
isButtonDisabled = false;
bookId: number = 0;
libraryId: number = 0;
@@ -64,15 +65,27 @@ export class BooksMetadataDialogComponent implements OnInit {
}
populateTitle() {
this.isButtonDisabled = true;
setTimeout(() => {
if (this.isButtonDisabled) {
this.isButtonDisabled = false;
this.errorMessage = 'Request timed out. Please try again.';
}
}, 5000);
this.bookService.fetchBookMetadataByTerm(this.searchText).subscribe({
next: (metadataList) => {
this.bookMetadataList = metadataList;
this.isLoading = false;
this.isButtonDisabled = false;
},
error: (error) => {
this.errorMessage = 'Failed to load metadata';
this.isLoading = false;
this.isButtonDisabled = false;
}
})
});
}
}
@@ -33,7 +33,7 @@ export class DashboardComponent {
createNewLibrary(event: MouseEvent) {
const buttonRect = (event.target as HTMLElement).getBoundingClientRect();
const dialogWidthPercentage = 50; // Use percentage-based width
const dialogWidthPercentage = 50;
const viewportWidth = window.innerWidth;
const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth;
@@ -0,0 +1,52 @@
<div class="card">
<div class="flex">
<p-dropdown
[options]="libraries"
optionLabel="name"
placeholder="Select Library"
[(ngModel)]="selectedLibrary">
</p-dropdown>
</div>
<div class="flex">
<p-dropdown
[options]="selectedLibrary?.paths"
placeholder="Select Upload Location"
[(ngModel)]="selectedPath"
[disabled]="!selectedLibrary">
</p-dropdown>
</div>
<div class="flex">
<div class="button-container">
<p-button
severity="info"
label="Choose"
icon="pi pi-file"
(click)="triggerFileInput(fileInput)"
[disabled]="!selectedPath || isUploading">
</p-button>
<span *ngIf="selectedFileName" class="file-name">{{ selectedFileName }}</span>
</div>
<input
type="file"
#fileInput
style="display: none;"
(change)="onFileSelected($event)"
[disabled]="!selectedPath || isUploading"/>
</div>
<div class="flex">
<p-button
[label]="isUploading ? 'Uploading...' : 'Upload'"
[disabled]="!isFormValid() || isUploading"
icon="pi pi-cloud-upload"
(click)="uploadFile()">
</p-button>
<div class="error-message-container">
<span *ngIf="errorMessage" class="error-message">{{ errorMessage }}</span>
</div>
</div>
</div>
@@ -0,0 +1,28 @@
.flex {
display: flex;
}
.flex + .flex {
margin-top: 15px;
}
.button-container {
display: flex;
align-items: center;
gap: 10px;
}
.file-name {
font-weight: bold;
color: var(--text-color-secondary);
}
.error-message-container {
display: flex;
align-items: center;
}
.error-message {
color: var(--red-600);
margin-left: 10px;
}
@@ -0,0 +1,89 @@
import {Component, OnInit} from '@angular/core';
import {Library} from '../../model/library.model';
import {LibraryService} from '../../service/library.service';
import {FileUploadService} from '../../../file-upload.service';
import {Button} from 'primeng/button';
import {NgIf} from '@angular/common';
import {DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
import {ToastService} from '../../../toast.service';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
@Component({
selector: 'app-file-upload-v2',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.scss'],
imports: [
Button,
NgIf,
DropdownModule,
FormsModule
],
})
export class FileUploadComponent implements OnInit {
libraries: Library[] = [];
selectedLibrary: Library | null = null;
selectedPath: string = '';
selectedFileName: string = '';
isUploading: boolean = false;
errorMessage: string | null = null;
constructor(
private libraryService: LibraryService,
private fileUploadService: FileUploadService,
private toastService: ToastService,
private dynamicDialogRef: DynamicDialogRef
) {
}
ngOnInit() {
this.libraries = this.libraryService.libraries();
}
triggerFileInput(fileInput: HTMLInputElement): void {
fileInput.click();
}
onFileSelected(event: Event): void {
const input = event.target as HTMLInputElement;
if (input && input.files) {
const file = input.files[0];
if (file) {
this.selectedFileName = file.name;
}
}
}
isFormValid(): boolean {
return !!this.selectedLibrary && !!this.selectedPath && !!this.selectedFileName;
}
uploadFile(): void {
this.errorMessage = null;
if (!this.isFormValid()) {
return;
}
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
return;
}
const file = fileInput.files[0];
const libraryId = this.selectedLibrary?.id ? this.selectedLibrary.id.toString() : '';
const path = this.selectedPath || '';
this.isUploading = true;
this.fileUploadService.uploadFile(file, libraryId, path)
.subscribe(
(response) => {
console.log('File uploaded successfully');
this.toastService.showSuccess("File uploaded", file.name);
this.isUploading = false;
this.dynamicDialogRef.close();
},
(error) => {
this.isUploading = false;
this.toastService.showError("Upload failed", error.error.message);
this.errorMessage = "Upload failed. " + error.error.message + ".";
}
);
}
}
@@ -1,14 +1,14 @@
<div class="layout-wrapper" [ngClass]="containerClass">
<app-topbar></app-topbar>
<div class="layout-sidebar layout-sidebar-custom">
<app-sidebar></app-sidebar>
<app-topbar></app-topbar>
<div class="layout-sidebar layout-sidebar-custom">
<app-sidebar></app-sidebar>
</div>
<div class="layout-main-container layout-main-container-custom">
<div class="layout-main">
<router-outlet></router-outlet>
</div>
<div class="layout-main-container layout-main-container-custom">
<div class="layout-main" >
<router-outlet ></router-outlet>
</div>
<app-footer></app-footer>
</div>
<app-config></app-config>
<div class="layout-mask"></div>
<app-footer></app-footer>
</div>
<app-config></app-config>
<div class="layout-mask"></div>
</div>
@@ -7,6 +7,7 @@ import { AppTopBarComponent } from './app.topbar.component';
import {AppFooterComponent} from "./app.footer.component";
import {AppConfigComponent} from "./config/app.config.component";
import {NgClass} from '@angular/common';
import {ToastModule} from 'primeng/toast';
@Component({
selector: 'app-layout',
@@ -16,7 +17,8 @@ import {NgClass} from '@angular/common';
AppConfigComponent,
AppSidebarComponent,
AppTopBarComponent,
NgClass
NgClass,
ToastModule
],
templateUrl: './app.layout.component.html'
})
@@ -123,22 +123,16 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
}
itemClick(event: Event) {
// avoid processing disabled items
if (this.item.disabled) {
event.preventDefault();
return;
}
// execute command
if (this.item.command) {
this.item.command({ originalEvent: event, item: this.item });
}
// toggle active state
if (this.item.items) {
this.active = !this.active;
}
this.menuService.onMenuStateChange({ key: this.key });
}
@@ -24,8 +24,10 @@
tooltipPosition="top">
<i class="pi pi-plus-circle"></i>
</button>
<button class="p-link layout-topbar-button">
<i class="pi pi-user"></i>
<button
(click)="openFileUploadDialog()"
class="p-link layout-topbar-button">
<i class="pi pi-upload"></i>
<span>Profile</span>
</button>
<button class="p-link layout-topbar-button" [routerLink]="'/documentation'">
@@ -1,14 +1,15 @@
import { Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
import { MenuItem } from 'primeng/api';
import { LayoutService } from './service/app.layout.service';
import { RouterLink } from '@angular/router';
import { DialogService as PrimeDialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
import { LibraryCreatorComponent } from '../library-creator/library-creator.component';
import { TooltipModule } from 'primeng/tooltip';
import { FormsModule } from '@angular/forms';
import { InputTextModule } from 'primeng/inputtext';
import { SearchComponent } from '../search/search.component';
import { NotificationComponent } from '../notification/notification.component'; // Import the NotificationComponent
import {Component, ElementRef, OnDestroy, ViewChild} from '@angular/core';
import {MenuItem} from 'primeng/api';
import {LayoutService} from './service/app.layout.service';
import {RouterLink} from '@angular/router';
import {DialogService as PrimeDialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {LibraryCreatorComponent} from '../library-creator/library-creator.component';
import {TooltipModule} from 'primeng/tooltip';
import {FormsModule} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {SearchComponent} from '../search/search.component';
import {NotificationComponent} from '../notification/notification.component';
import {FileUploadComponent} from '../file-upload/file-upload.component';
@Component({
selector: 'app-topbar',
@@ -18,7 +19,7 @@ import { NotificationComponent } from '../notification/notification.component';
FormsModule,
InputTextModule,
SearchComponent,
NotificationComponent // Add NotificationComponent to imports
NotificationComponent
],
templateUrl: './app.topbar.component.html',
})
@@ -30,7 +31,8 @@ export class AppTopBarComponent implements OnDestroy {
@ViewChild('topbarmenubutton') topbarMenuButton!: ElementRef;
@ViewChild('topbarmenu') menu!: ElementRef;
constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService) {}
constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService) {
}
openLibraryCreatorDialog(): void {
this.ref = this.dialogService.open(LibraryCreatorComponent, {
@@ -46,4 +48,14 @@ export class AppTopBarComponent implements OnDestroy {
this.ref.close();
}
}
openFileUploadDialog() {
this.ref = this.dialogService.open(FileUploadComponent, {
header: 'Upload Book',
modal: false,
width: '45%',
height: '40%',
style: { bottom: '20%' }
});
}
}
@@ -15,21 +15,20 @@
height: auto;
background-color: var(--surface-card);
border-radius: 8px;
position: relative; /* Positioning container for the buttons and overlay */
position: relative;
}
.book-cover {
width: 100%;
height: 225px; /* Adjust this as per the image height */
height: 225px;
object-fit: cover;
background-color: #444;
transition: background-color 0.3s ease, filter 0.3s ease; /* Add transition for blur effect */
position: relative; /* Make this the reference point for the overlay and buttons */
transition: background-color 0.3s ease, filter 0.3s ease;
position: relative;
}
/* Blur and overlay effect */
.book-item:hover .book-cover {
filter: blur(2px); /* Apply blur effect to the image */
filter: blur(2px);
}
.book-item:hover .book-cover::after {
@@ -39,8 +38,8 @@
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5); /* Semi-transparent black overlay */
z-index: 0; /* Put overlay behind the buttons */
background: rgba(0, 0, 0, 0.5);
z-index: 0;
}
.book-info {
@@ -50,7 +49,7 @@
flex-direction: column;
justify-content: flex-start;
flex-grow: 1;
z-index: 1; /* Ensure the text stays above the overlay */
z-index: 1;
}
h4 {
@@ -85,23 +84,22 @@ p {
font-style: italic;
}
/* PrimeNG button styling */
.view-btn, .read-btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -20%); /* Center buttons within the book-cover */
display: none; /* Hide by default */
margin: -60px 0; /* Add spacing between the buttons */
transform: translate(-50%, -20%);
display: none;
margin: -60px 0;
}
.book-item:hover .view-btn {
display: block; /* Show the View button when the card is hovered */
display: block;
}
.book-item:hover .read-btn {
display: block; /* Show the Read button when the card is hovered */
top: 75%; /* Slightly adjust the position to be below the View button */
display: block;
top: 75%;
}
.infinite-scroll {
@@ -4,12 +4,12 @@
justify-content: center;
align-items: center;
position: relative;
height: 100%; /* Fills the parent container */
height: 100%;
}
.textbox {
padding: 10px;
margin-bottom: 20px; /* Space between textbox and button */
margin-bottom: 20px;
}
.button {
@@ -35,10 +35,10 @@
}
.mt-3 {
margin-top: 1rem; /* Adds spacing between the button and the table */
margin-top: 1rem;
}
.w-100 {
width: 100%; /* Ensures full-width alignment for consistent layout */
width: 100%;
}
@@ -1,30 +1,28 @@
.search-container {
margin-left: 20px;
position: relative; /* Ensure the dropdown is positioned relative to the container */
position: relative;
}
.search-dropdown {
position: absolute;
left: 0; /* Align the dropdown to the left of the container */
background-color: var(--surface-overlay); /* Use theme's background color */
left: 0;
background-color: var(--surface-overlay);
border: 1px solid var(--surface-border);
border-radius: var(--border-radius);
z-index: 1000;
max-height: 300px; /* Limit the height of the dropdown */
overflow-y: auto; /* Scroll if the content exceeds max-height */
width: 600px; /* Ensures the dropdown has the same width as the search bar */
max-height: 300px;
overflow-y: auto;
width: 600px;
opacity: 0;
height: 0;
transition: opacity 0.3s ease, height 0.3s ease;
}
/* Show the dropdown */
.search-dropdown.show {
opacity: 1;
height: auto;
}
/* Dropdown item styles */
.search-dropdown-item {
display: flex;
align-items: center;
@@ -32,32 +30,28 @@
cursor: pointer;
transition: background-color 0.3s;
/* Hover effect */
&:hover {
background-color: var(--surface-hover);
}
}
/* Book cover styling */
.search-book-cover {
width: 25px; /* Set a fixed width */
height: 37px; /* Set a fixed height */
object-fit: cover; /* Ensure the image fits the area without distortion */
max-width: 100px; /* Max width to prevent images from becoming too big */
max-height: 150px; /* Max height to maintain consistency */
border-radius: 4px; /* Rounded corners */
margin-right: 10px; /* Space between the image and the title */
width: 25px;
height: 37px;
object-fit: cover;
max-width: 100px;
max-height: 150px;
border-radius: 4px;
margin-right: 10px;
}
/* Book title styling */
.search-book-name {
font-size: 14px;
font-weight: 500;
color: var(--text-color);
flex-grow: 1; /* Allow the title to take up the remaining space */
flex-grow: 1;
}
/* Optionally customize the transition duration or timing */
.search-dropdown.show {
opacity: 1;
height: auto;
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class FileUploadService {
private uploadUrl = 'http://localhost:8080/api/v1/files/upload';
constructor(private http: HttpClient) { }
uploadFile(file: File, libraryId: string, filePath: string): Observable<any> {
const formData: FormData = new FormData();
formData.append('file', file);
formData.append('libraryId', libraryId);
formData.append('filePath', filePath);
return this.http.post(this.uploadUrl, formData).pipe(
catchError((error) => {
throw error;
})
);
}
}
+26
View File
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import {MessageService} from 'primeng/api';
@Injectable({
providedIn: 'root'
})
export class ToastService {
constructor(private messageService: MessageService) {}
showSuccess(summary: string, detail: string) {
this.messageService.add({ severity: 'success', summary, detail });
}
showError(summary: string, detail: string) {
this.messageService.add({ severity: 'error', summary, detail });
}
showInfo(summary: string, detail: string) {
this.messageService.add({ severity: 'info', summary, detail });
}
showWarning(summary: string, detail: string) {
this.messageService.add({ severity: 'warn', summary, detail });
}
}