diff --git a/backend/models/Media.js b/backend/models/Media.js index 5282451..d790aa7 100644 --- a/backend/models/Media.js +++ b/backend/models/Media.js @@ -5,18 +5,20 @@ const Media = sequelize.define("media", { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, - entry_id: { + entryId: { type: DataTypes.INTEGER, references: { model: 'Entries', key: 'id' - } + }, + field: 'entry_id' }, type: { type: DataTypes.STRING(50) }, path: { type: DataTypes.STRING(200) }, - is_local: { type: DataTypes.BOOLEAN }, - is_selected: { type: DataTypes.BOOLEAN }, + isLocal: { type: DataTypes.BOOLEAN, field: 'is_local' }, + isSelected: { type: DataTypes.BOOLEAN, field: 'is_selected', defaultValue: false }, + isCustom: { type: DataTypes.BOOLEAN, field: 'is_custom', defaultValue: false }, } ); diff --git a/backend/routes/custom-media.routes.js b/backend/routes/custom-media.routes.js new file mode 100644 index 0000000..c6e5675 --- /dev/null +++ b/backend/routes/custom-media.routes.js @@ -0,0 +1,24 @@ +const express = require('express'); +const EntrySeen = require('../models/EntrySeen'); +const Entry = require('../models/Entry'); +const Media = require('../models/Media'); + +const customMediaRouter = express.Router(); + +// ! /custom-media +// * GET - ALL CUSTOM MEDIA +customMediaRouter.get('/', async (req, res) => { + + const allCustomMedia = await Media.findAll({ + where: { + isCustom: true + }, + include: [ + { model: Entry, as: 'entry' } + ] + }) + + res.status(200).json(allCustomMedia); +}); + +module.exports = customMediaRouter; diff --git a/backend/routes/media.routes.js b/backend/routes/media.routes.js new file mode 100644 index 0000000..626f8c8 --- /dev/null +++ b/backend/routes/media.routes.js @@ -0,0 +1,77 @@ +const express = require('express'); +const path = require('path'); +const fs = require('fs'); + +const { getSettings } = require('../services/settingsService'); +const { generatePreview } = require('../services/mediaService'); + +const mediaRouter = express.Router(); + +// ! /media +// ? Routes : media access routes +mediaRouter.get('/:category/:type/:size/:fileName', (req, res, next) => { + const { category, type, size, fileName } = req.params; + + if (!['entries', 'collections', 'franchises'].includes(category)) { + return res.status(404).send('Category Not Found'); + } + + if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) { + return res.status(404).send('Type Not Found'); + } + + const settings = getSettings(); + const dataPath = settings.userDataPath; + + const mediaPath = path.join(dataPath, 'images', category, type); + // const mediaPath = path.join(__dirname, '..', `data/images/${category}/${type}`); + + // * preparing to serve the preview + // ? basicaly im checking if the file requested is a preview file + // ? if so I make sure to change the extention to '_preview.jpg' (bcs all previews are jpg) + let filePath = path.join(mediaPath, fileName); + if (size == 'preview') { + const previewFileName = fileName.split('.')[0] + '_preview.jpg' + filePath = path.join(mediaPath, previewFileName); + } + + fs.access(filePath, fs.constants.F_OK, (err) => { + // * generating a preview if it doesn't exist + // ? then if the preview doesn't exist I generate a new one + // ? by taking the file name and removing '_preview' from it to get + // ? the original file name, and starting from that I join it to the media path + // ? and generate a preview file, but still return 202 http code since the file + // ? is still generating + if (err && size == 'preview') { + const originalPath = path.join(mediaPath, fileName); + const previewFileName = fileName.split('.')[0] + '_preview.jpg' + const previewPath = path.join(mediaPath, previewFileName); + + let previewWidth = 342; + if (type == 'backdrops') previewWidth = 780; + if (type == 'logos') previewWidth = 300; + + generatePreview(originalPath, previewPath, previewWidth).then((result) => { + console.log(`generated a preview: `, previewFileName); + const stream = fs.createReadStream(filePath); + stream.pipe(res); + }).catch((err) => { + + });; + // const stream = fs.createReadStream(filePath); + // stream.pipe(res); + // return res.status(202).send('Preview is still generating. Please try again later.'); + } else { + const options = { + fallthrough: false, // ! Important: don't call next middleware if file isn't found + }; + + // express.static(filePath, options)(req, res, next); + // Serve the file using fs.createReadStream + const stream = fs.createReadStream(filePath); + stream.pipe(res); + } + }); +}); + +module.exports = mediaRouter; diff --git a/backend/routes/routes.js b/backend/routes/routes.js index e863f6b..fe722d3 100644 --- a/backend/routes/routes.js +++ b/backend/routes/routes.js @@ -9,9 +9,11 @@ const episodeSeenRouter = require('./episode-seen.routes'); const settingsRouter = require('./settings.routes'); const maintenanceRouter = require('./maintenance.routes'); const statisticsRouter = require('./statistics.routes'); +const customMediaRouter = require('./custom-media.routes'); +const mediaRouter = require('./media.routes'); const router = express.Router(); - +router.use('/media', mediaRouter); router.use('/entries', entryRouter); router.use('/user-collections', userCollectionRoutes); router.use('/entry-seen', entrySeenRouter); @@ -20,6 +22,8 @@ router.use('/entry-ratings', entryRatingsRouter); router.use('/seasons', seasonRouter); router.use('/episode-seen', episodeSeenRouter); +router.use('/custom-media', customMediaRouter); + router.use('/statistics', statisticsRouter); router.use('/settings', settingsRouter); diff --git a/backend/services/mediaService.js b/backend/services/mediaService.js index 6996902..d106160 100644 --- a/backend/services/mediaService.js +++ b/backend/services/mediaService.js @@ -5,6 +5,7 @@ const path = require('path'); const { v4: uuidv4 } = require('uuid'); const { fetchEntryMedia } = require('./tmdbService'); const { getSettings } = require('./settingsService'); +const sharp = require('sharp'); async function downloadImage(url, filePath) { const response = await axios({ @@ -67,7 +68,7 @@ async function getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos' // Utility to find the preferred media (English if available, otherwise the first one) function findPreferredMedia(mediaArray, preferredLanguage = 'en') { - return mediaArray.find(media => media.iso_639_1 === preferredLanguage) || mediaArray[0]; + return mediaArray.find(media => media.iso_639_1 === preferredLanguage && !media.file_path.includes('.svg')) || mediaArray.find(media => !media.file_path.includes('.svg')) || mediaArray[0]; } // Adjusted scheduleDownloads to accept local arrays @@ -97,6 +98,13 @@ function scheduleDownloads(media, mediaType, mediaDir, downloadTasks, downloaded ); } +function generatePreview(originalPath, previewPath, previewWidth, previewQuality = 80) { + return sharp(originalPath) + .resize({ width: previewWidth }) + .jpeg({ quality: previewQuality }) + .toFile(previewPath); +} + function getMediaFolder() { return getSettings().userDataPath; } @@ -122,4 +130,5 @@ module.exports = { getEntryMedia, createMediaFolders, getMediaFolder, + generatePreview }; \ No newline at end of file diff --git a/frontend/src/app/app-routing.module.ts b/frontend/src/app/app-routing.module.ts index b6f8223..df22a88 100644 --- a/frontend/src/app/app-routing.module.ts +++ b/frontend/src/app/app-routing.module.ts @@ -16,13 +16,20 @@ const routes: Routes = [ { path: 'discover', loadChildren: () => - import('./features/discover/discover.module').then((m) => m.DiscoverModule), + import('./features/discover/discover.module').then( + (m) => m.DiscoverModule + ), }, { path: 'library', loadChildren: () => import('./features/library/library.module').then((m) => m.LibraryModule), }, + { + path: 'media', + loadChildren: () => + import('./features/media/media.module').then((m) => m.MediaModule), + }, { path: 'favorit', component: FavoritComponent }, { path: 'watchlist', component: WatchlistComponent }, { diff --git a/frontend/src/app/core/components/sidebar/sidebar.component.html b/frontend/src/app/core/components/sidebar/sidebar.component.html index fc582e0..f488454 100644 --- a/frontend/src/app/core/components/sidebar/sidebar.component.html +++ b/frontend/src/app/core/components/sidebar/sidebar.component.html @@ -83,6 +83,15 @@ +
  • + +
    + filter + +
    + +
    +
  • @@ -105,26 +114,17 @@
  • -
    +
    - Media Image + Media Image
    - - -
    @@ -50,20 +53,42 @@
    -
    -
    +
    +
    -
    - Poster +
    + Media Image +
    + + + +
    -
    +
    -
    - Poster +
    + Media Image +
    + + + +
    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 index 4d2cd8a..77d615b 100644 --- a/frontend/src/app/features/entry/media-modal/media-modal.component.ts +++ b/frontend/src/app/features/entry/media-modal/media-modal.component.ts @@ -28,6 +28,7 @@ export class MediaModalComponent implements OnInit { ngOnInit(): void { if (this.data && this.data.entry) { this.entry = this.data.entry; + // console.log(this.entry.medias); } else { this.dialogRef.close(); } @@ -56,8 +57,8 @@ export class MediaModalComponent implements OnInit { }); } - getPath(mediaEnd: string) { - return environment.API_URL + '/media/entries' + mediaEnd; + getPath(fileType: string, filePath: string) { + return `${environment.API_URL}/media/entries/${fileType}/preview/${filePath}`; } setTab(tab: string) { @@ -81,10 +82,10 @@ export class MediaModalComponent implements OnInit { next: (result) => { this.entry.medias.forEach((m) => { if (m.type == media.type) { - m.is_selected = false; + m.isSelected = false; } if ((m.id = result.id)) { - m.is_selected = true; + m.isSelected = true; } }); }, @@ -95,6 +96,7 @@ export class MediaModalComponent implements OnInit { } expandMedia(media: any) { - // Implementation to expand or view media in a larger view + const fullSizePath = `${environment.API_URL}/media/entries/${media.type}s/original/${media.path}`; + window.open(fullSizePath, '_blank'); } } 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 index 9bd159f..b73823d 100644 --- 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 @@ -21,8 +21,8 @@
    - - + +
    diff --git a/frontend/src/app/features/media/media-page/media-page.component.css b/frontend/src/app/features/media/media-page/media-page.component.css new file mode 100644 index 0000000..d85e988 --- /dev/null +++ b/frontend/src/app/features/media/media-page/media-page.component.css @@ -0,0 +1,106 @@ +.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; + width: 100%; + aspect-ratio: 2 / 3; + object-fit: cover; +} + +/* backdrops images */ +.media-dialog-main #backdrops .card-img-top { + height: auto; + width: 100%; + aspect-ratio: 8 / 3; + object-fit: cover; +} + +/* Logo images */ +.media-dialog-main #logos .card-img-top { + height: auto; + width: 100%; + aspect-ratio: 2 / 1; + object-fit: contain; +} + +.media-dialog-main .card .card-img-top { + display: block; +} + +.add-btn { + color: var(--shz-blue-1); +} + +.add-btn:hover { + color: var(--shz-blue-default); +} + +/* ! media hover buttons */ +.image-hover-container { + position: relative; + overflow: hidden; +} + +.image-hover-container img { + width: 100%; + display: block; + transition: transform 0.3s ease; + transform-origin: center; +} + +.image-hover-overlay { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: rgba(0, 0, 0, 0.6); + 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; +} + +.btn-icon { + background: transparent; + border: none; + color: white; + font-size: 24px; +} + +.btn-icon-delete:hover { + color: var(--shz-red-danger); +} + +.btn-icon-select:hover { + color: var(--shz-blue-default); +} + +.btn-icon-expand:hover { + color: var(--shz-green-success); +} \ No newline at end of file diff --git a/frontend/src/app/features/media/media-page/media-page.component.html b/frontend/src/app/features/media/media-page/media-page.component.html new file mode 100644 index 0000000..4b6b66c --- /dev/null +++ b/frontend/src/app/features/media/media-page/media-page.component.html @@ -0,0 +1,101 @@ +
    +
    +
    Custom Uploaded Media
    +
    +
    +
    + +
    +
    +
    +
    +
    + Media Image +
    + + + +
    +
    +
    +
    +
    +
    +
    +
    +
    + Media Image +
    + + + +
    +
    +
    +
    +
    +
    +
    +
    +
    + Media Image +
    + + + +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    \ No newline at end of file diff --git a/frontend/src/app/features/media/media-page/media-page.component.spec.ts b/frontend/src/app/features/media/media-page/media-page.component.spec.ts new file mode 100644 index 0000000..e1cbeb5 --- /dev/null +++ b/frontend/src/app/features/media/media-page/media-page.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 { MediaPageComponent } from './media-page.component'; + +describe('MediaPageComponent', () => { + let component: MediaPageComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ MediaPageComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(MediaPageComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/media/media-page/media-page.component.ts b/frontend/src/app/features/media/media-page/media-page.component.ts new file mode 100644 index 0000000..2b95e93 --- /dev/null +++ b/frontend/src/app/features/media/media-page/media-page.component.ts @@ -0,0 +1,77 @@ +import { Component, OnInit } from '@angular/core'; +import { CustomMediaService } from '../../../core/services/custom-media.service'; +import { Media } from '../../../shared/models/media.model'; +import { environment } from '../../../../environments/environment.prod'; +import { EntryService } from '../../../core/services/entry.service'; + +@Component({ + selector: 'app-media-page', + templateUrl: './media-page.component.html', + styleUrls: ['./media-page.component.css'], +}) +export class MediaPageComponent implements OnInit { + customMedia: Media[] = []; + tab: string = 'poster'; + + constructor( + private customMediaService: CustomMediaService, + private entryService: EntryService + ) {} + + ngOnInit() { + this.customMediaService.fetchCustomMedia().subscribe({ + next: (result) => { + this.customMedia = result; + console.log(result); + }, + error: (error) => {}, + }); + } + + filterMedia(type: string) { + return this.customMedia.filter((media) => media.type == type); + } + + getPath(type: string, fileName: string) { + return `${environment.API_URL}/media/entries/${type}/preview/${fileName}`; + } + + setTab(tab: string) { + this.tab = tab; + } + + deleteMedia(media: Media) { + this.entryService.deleteEntryMedia(media.entry.id, media.id).subscribe({ + next: (result) => { + // TODO add confirm modal + this.customMedia = this.customMedia.filter((m) => m.id != media.id); + }, + error: (error) => { + console.log(error); + }, + }); + } + + selectMedia(media: any) { + this.entryService.selectEntryMedia(media.entry.id, media.id).subscribe({ + next: (result) => { + this.customMedia.forEach((m) => { + if (m.type == media.type) { + m.isSelected = false; + } + if ((m.id = result.id)) { + m.isSelected = true; + } + }); + }, + error: (error) => { + console.log(error); + }, + }); + } + + expandMedia(media: any) { + const fullSizePath = `${environment.API_URL}/media/entries/${media.type}s/original/${media.path}`; + window.open(fullSizePath, '_blank'); + } +} diff --git a/frontend/src/app/features/media/media-routing.module.ts b/frontend/src/app/features/media/media-routing.module.ts new file mode 100644 index 0000000..9ed7b4f --- /dev/null +++ b/frontend/src/app/features/media/media-routing.module.ts @@ -0,0 +1,17 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { MediaPageComponent } from './media-page/media-page.component'; + +// ! /media +const routes: Routes = [ + { + path: '', + component: MediaPageComponent, + }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule], +}) +export class MediaRoutingModule {} diff --git a/frontend/src/app/features/media/media.module.ts b/frontend/src/app/features/media/media.module.ts new file mode 100644 index 0000000..96f08e4 --- /dev/null +++ b/frontend/src/app/features/media/media.module.ts @@ -0,0 +1,23 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { SharedModule } from '../../shared/shared.module'; + +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; + +import { MediaPageComponent } from './media-page/media-page.component'; +import { MediaRoutingModule } from './media-routing.module'; + +@NgModule({ + imports: [ + CommonModule, + SharedModule, + + FormsModule, + ReactiveFormsModule, + + MediaRoutingModule, + ], + declarations: [MediaPageComponent], + exports: [], +}) +export class MediaModule {} diff --git a/frontend/src/app/shared/models/media.model.ts b/frontend/src/app/shared/models/media.model.ts index 3c21abf..f5ac5d1 100644 --- a/frontend/src/app/shared/models/media.model.ts +++ b/frontend/src/app/shared/models/media.model.ts @@ -1,13 +1,16 @@ +import { Entry } from './entry.model'; + export interface Media { id: number; entry_id: number; + entry: Entry; type: string; path: string; - is_local: boolean; - is_selected: boolean; + isLocal: boolean; + isSelected: boolean; createdAt: Date; updatedAt: Date;