diff --git a/backend/routes/entry.routes.js b/backend/routes/entry.routes.js index ce0a8ff..c2468a2 100644 --- a/backend/routes/entry.routes.js +++ b/backend/routes/entry.routes.js @@ -13,6 +13,7 @@ const EntrySeen = require('../models/EntrySeen'); const Season = require('../models/Season'); const Media = require('../models/Media'); const Episode = require('../models/Episode'); +const { entryPicturesUpload } = require('../services/mutlerStorageLocations'); // ! /entries @@ -304,6 +305,101 @@ entryRouter.get('/:id/refrech', async (req, res, next) => { // res.status(201).json(entryData); }); +const upload = entryPicturesUpload.fields([ + { name: 'poster', maxCount: 1 }, + { name: 'backdrop', maxCount: 1 }, + { name: 'logo', maxCount: 1 } +]); +entryRouter.post('/:id/media', upload, async (req, res) => { + const id = req.params.id; + if (req.files) { + + const entry = await Entry.findByPk(id); + if (!entry) { + return res.status(404).json({ message: 'Entry not found' }); + } + + const posterPath = req.files.poster ? req.files.poster[0].filename : null; + if (posterPath) { + const createdMedia = await Media.create({ + entry_id: id, + type: 'poster', + path: posterPath, + is_local: true, + is_selected: false, + }); + return res.status(200).json(createdMedia); + } + + const backdropPath = req.files.backdrop ? req.files.backdrop[0].filename : null; + if (backdropPath) { + const createdMedia = await Media.create({ + entry_id: id, + type: 'backdrop', + path: backdropPath, + is_local: true, + is_selected: false, + }); + return res.status(200).json(createdMedia); + } + + const logoPath = req.files.logo ? req.files.logo[0].filename : null; + if (logoPath) { + const createdMedia = await Media.create({ + entry_id: id, + type: 'logo', + path: logoPath, + is_local: true, + is_selected: false, + }); + return res.status(200).json(createdMedia); + } + + } +}); + +entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => { + const entryId = req.params.entryId; + const mediaId = req.params.mediaId; + + const entry = await Entry.findByPk(entryId); + if (!entry) { + return res.status(404).json({ message: 'Entry not found' }); + } + + const media = await Media.findByPk(mediaId); + if (!media) { + return res.status(404).json({ message: 'Media not found' }); + } + + await Media.update( + { is_selected: false }, + { + where: { + entry_id: entryId, + type: media.type + } + } + ); + + const updatedMedia = await media.update({ is_selected: true }); + + return res.status(200).json(updatedMedia); +}); + +entryRouter.delete('/:entryId/media/:mediaId', async (req, res) => { + const mediaId = req.params.mediaId; + + // const media = await Media.findByPk(mediaId); + // if (!media) { + // return res.status(404).json({ message: 'Media not found' }); + // } + + await Media.destroy({ where: { id: mediaId } }) + + res.status(200).json(); +}); + // * DELETE - ENTRY BY ID entryRouter.delete('/:id', async (req, res) => { const { id } = req.params; diff --git a/backend/services/mutlerStorageLocations.js b/backend/services/mutlerStorageLocations.js new file mode 100644 index 0000000..11faa74 --- /dev/null +++ b/backend/services/mutlerStorageLocations.js @@ -0,0 +1,31 @@ +const multer = require('multer'); +const { v4: uuidv4 } = require('uuid'); +const path = require('path'); +const { getSettings } = require('./settingsService'); + +// ? users +const entryPicturesUpload = multer({ + storage: + multer.diskStorage({ + destination: function (req, file, cb) { + let folder = 'posters'; + if (file.fieldname == 'banner') folder = 'backdrops'; + if (file.fieldname == 'logo') folder = 'logos'; + + const settings = getSettings(); + const dataPath = settings.userDataPath; + + cb(null, path.join(dataPath, 'images', 'entries', folder)); + }, + filename: function (req, file, cb) { + const uniqueFilename = uuidv4(); + const extension = file.originalname.split('.').pop(); + cb(null, `${uniqueFilename}.${extension}`); + } + }) +}); + + +module.exports = { + entryPicturesUpload, +} \ No newline at end of file diff --git a/frontend/src/app/core/services/entry.service.ts b/frontend/src/app/core/services/entry.service.ts index e9cf206..fc24b93 100644 --- a/frontend/src/app/core/services/entry.service.ts +++ b/frontend/src/app/core/services/entry.service.ts @@ -5,6 +5,7 @@ import { Entry } from '../../shared/models/entry.model'; import { EntryId } from '../../shared/models/entryId.model'; import { EntryRating } from '../../shared/models/entryRating.model'; import { environment } from '../../../environments/environment.prod'; +import { Media } from '../../shared/models/media.model'; @Injectable({ providedIn: 'root', @@ -41,6 +42,26 @@ export class EntryService { ); } + addEntryMedia(entryId: number, data: FormData): Observable { + return this.http.post( + `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media`, + data + ); + } + + selectEntryMedia(entryId: number, mediaId: number): Observable { + return this.http.put( + `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media/${mediaId}`, + {} + ); + } + + deleteEntryMedia(entryId: number, mediaId: number): Observable { + return this.http.delete( + `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media/${mediaId}` + ); + } + // update entry refrechEntryById(entryId: number) { return this.http.get( diff --git a/frontend/src/app/features/entry/entries.module.ts b/frontend/src/app/features/entry/entries.module.ts index 328cf1f..3b1ba0f 100644 --- a/frontend/src/app/features/entry/entries.module.ts +++ b/frontend/src/app/features/entry/entries.module.ts @@ -5,9 +5,16 @@ import { EntryPageComponent } from './entry-page/entry-page.component'; import { EntriesRoutingModule } from './entries-routing.module'; import { CommonModule } from '@angular/common'; import { SeasonMainCardComponent } from './season-main-card/season-main-card.component'; +import { MediaModalComponent } from './media-modal/media-modal.component'; +import { MediaUploadModalComponent } from './media-upload-modal/media-upload-modal.component'; @NgModule({ - declarations: [EntryPageComponent, SeasonMainCardComponent], + declarations: [ + EntryPageComponent, + SeasonMainCardComponent, + MediaModalComponent, + MediaUploadModalComponent, + ], imports: [ CommonModule, SharedModule, diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.html b/frontend/src/app/features/entry/entry-page/entry-page.component.html index 9afe859..b42a3ec 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.html +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.html @@ -160,6 +160,12 @@ update + + diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.ts b/frontend/src/app/features/entry/entry-page/entry-page.component.ts index 27261e2..232a2c7 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.ts +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.ts @@ -20,6 +20,7 @@ import { LibraryService } from '../../../core/services/library.service'; import { ToastrService } from 'ngx-toastr'; import { EntrySeen } from '../../../shared/models/entrySeen.model'; import { EpisodeSeenModalComponent } from '../../../shared/components/modals/episode-seen-modal/episode-seen-modal.component'; +import { MediaModalComponent } from '../media-modal/media-modal.component'; @Component({ selector: 'app-entry-page', @@ -205,7 +206,7 @@ export class EntryPageComponent implements OnInit { // ? from the watchlist, so now we need to refrech libraryService in order to // ? update the watchlist page if (this.entry.watchlist) { - this.libraryService.refrechEntries().subscribe(); + this.libraryService.refreshEntries().subscribe(); } } }, @@ -608,6 +609,26 @@ export class EntryPageComponent implements OnInit { window.open(poster, '_blank'); } + mediaModal() { + const dialogRef = this.dialog.open(MediaModalComponent, { + width: '800px', + height: '600px', + minHeight: '50%', + panelClass: 'media-dialog-container', + data: { + entry: this.entry, + }, + }); + + dialogRef.afterClosed().subscribe((result) => { + if (result) { + // TODO show notification + } else { + // console.log('Seen dialog was cancelled'); + } + }); + } + // * getters get status() { return this.entry.status; diff --git a/frontend/src/app/features/entry/media-modal/media-modal.component.css b/frontend/src/app/features/entry/media-modal/media-modal.component.css new file mode 100644 index 0000000..c35d86f --- /dev/null +++ b/frontend/src/app/features/entry/media-modal/media-modal.component.css @@ -0,0 +1,110 @@ +.media-dialog-container { + height: 100%; + padding-bottom: 40px; +} + +.media-dialog-main { + height: 100%; +} + +.card { + width: 100%; + height: 100% !important; + overflow-y: auto !important; + margin-bottom: 0 !important; +} + +.card-body { + height: auto !important; + padding-bottom: 20px; +} + +.media-dialog-main #posters .card-img-top { + height: auto; + /* Height should adjust to maintain aspect ratio */ + width: 100%; + aspect-ratio: 2 / 3; + /* Using the aspect-ratio property to specify a 2:3 ratio */ + object-fit: cover; + /* Crop to cover the area of the container */ +} + +/* Banner images */ +.media-dialog-main #banners .card-img-top { + height: auto; + width: 100%; + aspect-ratio: 8 / 3; + /* An example ratio for wide banners */ + object-fit: cover; +} + +/* Logo images */ +.media-dialog-main #logos .card-img-top { + height: auto; + width: 100%; + aspect-ratio: 2 / 1; + /* More wide than tall for logos */ + object-fit: contain; + /* Ensure the entire logo is visible */ +} + + + +.media-dialog-main .card .card-img-top { + display: block; + /* Ensure images are block elements to fill space correctly */ +} + + +/* ! media hover buttons */ +.image-hover-container { + position: relative; + overflow: hidden; + /* Ensures no overflow outside the container */ +} + +.image-hover-container img { + width: 100%; + display: block; + transition: transform 0.3s ease; + transform-origin: center; + /* Ensures scaling is centered */ +} + + +.image-hover-overlay { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: rgba(0, 0, 0, 0.6); + /* Semi-transparent overlay */ + display: flex; + justify-content: center; + align-items: center; + opacity: 0; + transition: opacity 0.3s ease; + overflow: hidden; +} + +.image-hover-container:hover .image-hover-overlay { + opacity: 1; +} + + +.image-hover-container:hover img { + /* transform: scale(1.05); */ +} + +.btn-icon { + background: transparent; + border: none; + color: white; + font-size: 24px; + /* Adjust size as needed */ +} + +.btn-icon:hover { + color: var(--shz-gold-1); +} \ No newline at end of file diff --git a/frontend/src/app/features/entry/media-modal/media-modal.component.html b/frontend/src/app/features/entry/media-modal/media-modal.component.html new file mode 100644 index 0000000..5c1bf9f --- /dev/null +++ b/frontend/src/app/features/entry/media-modal/media-modal.component.html @@ -0,0 +1,74 @@ + +
+ +
+ + +
+
+
+
+
+ Media Image +
+ + + +
+
+
+
+
+
+
+
+
+ Poster +
+
+
+
+
+
+
+
+ Poster +
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/frontend/src/app/features/entry/media-modal/media-modal.component.spec.ts b/frontend/src/app/features/entry/media-modal/media-modal.component.spec.ts new file mode 100644 index 0000000..3dfe765 --- /dev/null +++ b/frontend/src/app/features/entry/media-modal/media-modal.component.spec.ts @@ -0,0 +1,28 @@ +/* tslint:disable:no-unused-variable */ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement } from '@angular/core'; + +import { MediaModalComponent } from './media-modal.component'; + +describe('MediaModalComponent', () => { + let component: MediaModalComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ MediaModalComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(MediaModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/entry/media-modal/media-modal.component.ts b/frontend/src/app/features/entry/media-modal/media-modal.component.ts new file mode 100644 index 0000000..4d2cd8a --- /dev/null +++ b/frontend/src/app/features/entry/media-modal/media-modal.component.ts @@ -0,0 +1,100 @@ +import { Component, Inject, OnInit } from '@angular/core'; +import { + MatDialogRef, + MAT_DIALOG_DATA, + MatDialog, +} from '@angular/material/dialog'; +import { Entry } from '../../../shared/models/entry.model'; +import { environment } from '../../../../environments/environment.prod'; +import { MediaUploadModalComponent } from '../media-upload-modal/media-upload-modal.component'; +import { EntryService } from '../../../core/services/entry.service'; + +@Component({ + selector: 'app-media-modal', + templateUrl: './media-modal.component.html', + styleUrls: ['./media-modal.component.css'], +}) +export class MediaModalComponent implements OnInit { + entry!: Entry; + tab: string = 'poster'; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private dialog: MatDialog, + private entryService: EntryService + ) {} + + ngOnInit(): void { + if (this.data && this.data.entry) { + this.entry = this.data.entry; + } else { + this.dialogRef.close(); + } + } + + onClose(): void { + this.dialogRef.close(); + } + + filterMedia(type: string) { + return this.entry.medias.filter((media) => media.type == type); + } + + openUploadModal() { + const dialogRef = this.dialog.open(MediaUploadModalComponent, { + data: { + entryId: this.entry.id, + mediaType: this.tab, + }, + }); + + dialogRef.afterClosed().subscribe((result) => { + if (result) { + this.entry.medias.push(result.data); + } + }); + } + + getPath(mediaEnd: string) { + return environment.API_URL + '/media/entries' + mediaEnd; + } + + setTab(tab: string) { + this.tab = tab; + } + + deleteMedia(id: number) { + this.entryService.deleteEntryMedia(this.entry.id, id).subscribe({ + next: (result) => { + // TODO add confirm modal + this.entry.medias = this.entry.medias.filter((m) => m.id != id); + }, + error: (error) => { + console.log(error); + }, + }); + } + + selectMedia(media: any) { + this.entryService.selectEntryMedia(this.entry.id, media.id).subscribe({ + next: (result) => { + this.entry.medias.forEach((m) => { + if (m.type == media.type) { + m.is_selected = false; + } + if ((m.id = result.id)) { + m.is_selected = true; + } + }); + }, + error: (error) => { + console.log(error); + }, + }); + } + + expandMedia(media: any) { + // Implementation to expand or view media in a larger view + } +} diff --git a/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.css b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.html b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.html new file mode 100644 index 0000000..9bd159f --- /dev/null +++ b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.html @@ -0,0 +1,29 @@ +
+ +
+
+
+
+
+
+ +
+ File is required +
+
+
+
+ +
+
+ + +
+
+
+
\ No newline at end of file diff --git a/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.spec.ts b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.spec.ts new file mode 100644 index 0000000..f18a1d1 --- /dev/null +++ b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.spec.ts @@ -0,0 +1,28 @@ +/* tslint:disable:no-unused-variable */ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement } from '@angular/core'; + +import { MediaUploadModalComponent } from './media-upload-modal.component'; + +describe('MediaUploadModalComponent', () => { + let component: MediaUploadModalComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ MediaUploadModalComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(MediaUploadModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.ts b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.ts new file mode 100644 index 0000000..b408778 --- /dev/null +++ b/frontend/src/app/features/entry/media-upload-modal/media-upload-modal.component.ts @@ -0,0 +1,63 @@ +import { Component, Inject, OnInit } from '@angular/core'; +import { FormBuilder, FormGroup } from '@angular/forms'; +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; +import { EntryService } from '../../../core/services/entry.service'; + +@Component({ + selector: 'app-media-upload-modal', + templateUrl: './media-upload-modal.component.html', + styleUrls: ['./media-upload-modal.component.css'], +}) +export class MediaUploadModalComponent implements OnInit { + fileForm: FormGroup; + mediaType!: string; + entryId!: number; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private formBuilder: FormBuilder, + private entryService: EntryService + ) { + this.fileForm = this.formBuilder.group({ + file: [null], + }); + } + + ngOnInit() { + if (this.data && this.data.mediaType && this.data.entryId) { + this.mediaType = this.data.mediaType; + this.entryId = this.data.entryId; + } else { + this.dialogRef.close(); + } + } + + onFileChange(event: any) { + const file = event.target.files[0]; + this.fileForm.patchValue({ file: file }); + this.fileForm.get('file')?.updateValueAndValidity(); + } + + onSubmit() { + if (!this.fileForm.valid) return; + + const formData = new FormData(); + + const fileValue = this.fileForm.get('file')?.value; + if (fileValue) formData.append(this.mediaType, fileValue); + + this.entryService.addEntryMedia(this.entryId, formData).subscribe({ + next: (result) => { + this.dialogRef.close({ data: result }); + }, + error: (error) => { + console.log(error); + }, + }); + } + + onClose() { + this.dialogRef.close(); + } +}