diff --git a/backend/db/relations.js b/backend/db/relations.js index acc633f..ce72c03 100644 --- a/backend/db/relations.js +++ b/backend/db/relations.js @@ -46,7 +46,7 @@ Entry.belongsToMany(UserCollection, { foreignKey: 'entry_id', otherKey: 'user_collection_id', onDelete: 'CASCADE', - as: 'userCollections', + as: 'collections', }); UserCollection.belongsToMany(Entry, { @@ -86,8 +86,8 @@ EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' }); // * ENTRY & MEDIA - ONE TO MANY -Entry.hasMany(Media, { foreignKey: 'entry_id', as: 'medias', onDelete: 'CASCADE', }); -Media.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); +Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', }); +Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' }); // * ENTRY & SEASON - ONE TO MANY Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', }); diff --git a/backend/db/sequelizeHooks.js b/backend/db/sequelizeHooks.js index fe4d855..4d7e434 100644 --- a/backend/db/sequelizeHooks.js +++ b/backend/db/sequelizeHooks.js @@ -81,7 +81,6 @@ UserCollectionEntry.afterDestroy(async (instance, options) => { name: sequelize.literal('name') } - // ? updating collection poster const collectionsEntries = await UserCollectionEntry.findAll({ where: { user_collection_id: instance.user_collection_id @@ -91,14 +90,15 @@ UserCollectionEntry.afterDestroy(async (instance, options) => { ] }) + // ? updating collection poster // ? remove the poster if the collection is empty now if (collectionsEntries.length == 0) { updates.poster = null; } else { - // todo add is customPoster field - if (userCollection.poster_path == entry.poster_path) { - updates.poster_path = collectionsEntries[0].entry.poster_path; - } + // ? replace poster it belonged to the removed entry + // if (userCollection.poster_path == entry.poster_path) { + // updates.poster_path = collectionsEntries[0].entry.poster_path; + // } } // ? updating order, only if the removed entry isn't placed at the end diff --git a/backend/models/Entry.js b/backend/models/Entry.js index 0df0096..45345e5 100644 --- a/backend/models/Entry.js +++ b/backend/models/Entry.js @@ -6,9 +6,9 @@ const Entry = sequelize.define("entry", id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, title: { type: DataTypes.STRING(100) }, - original_title: { type: DataTypes.STRING(100) }, - release_date: { type: DataTypes.DATE }, - release_status: { type: DataTypes.STRING(50) }, + original_title: { type: DataTypes.STRING(100), field: 'original_title' }, + release_date: { type: DataTypes.DATE, field: 'release_date' }, + release_status: { type: DataTypes.STRING(50), field: 'release_status' }, runtime: { type: DataTypes.INTEGER }, tagline: { type: DataTypes.STRING(200) }, @@ -16,23 +16,23 @@ const Entry = sequelize.define("entry", country: { type: DataTypes.STRING(30) }, certification: { type: DataTypes.INTEGER }, - network_studio: { type: DataTypes.STRING(40) }, - collection_id: { type: DataTypes.INTEGER }, - max_cast: { type: DataTypes.INTEGER, defaultValue: 10 }, + network_studio: { type: DataTypes.STRING(40), field: 'network_studio' }, + collection_id: { type: DataTypes.INTEGER, field: 'collection_id' }, + max_cast: { type: DataTypes.INTEGER, field: 'max_cast', defaultValue: 10 }, watchlist: { type: DataTypes.BOOLEAN, defaultValue: false }, favorit: { type: DataTypes.BOOLEAN, defaultValue: false }, hidden: { type: DataTypes.BOOLEAN, defaultValue: false }, note: { type: DataTypes.STRING(2000) }, - is_tvseries: { type: DataTypes.BOOLEAN }, + is_tvseries: { type: DataTypes.BOOLEAN, field: 'is_tvseries' }, status: { type: DataTypes.STRING(20) }, - poster_path: { type: DataTypes.STRING(200) }, - banner_path: { type: DataTypes.STRING(200) }, + poster_path: { type: DataTypes.STRING(200), field: 'poster_path' }, + banner_path: { type: DataTypes.STRING(200), field: 'banner_path' }, - date_first_seen: { type: DataTypes.DATEONLY }, - date_last_seen: { type: DataTypes.DATEONLY }, + date_first_seen: { type: DataTypes.DATEONLY, field: 'date_first_seen' }, + date_last_seen: { type: DataTypes.DATEONLY, field: 'date_last_seen' }, last_fetch: { type: DataTypes.DATEONLY }, } diff --git a/backend/server.js b/backend/server.js index 391d333..a951a18 100644 --- a/backend/server.js +++ b/backend/server.js @@ -2,8 +2,6 @@ const express = require('express'); const bodyParser = require('body-parser'); const routes = require('./routes/routes.js'); const sequelize = require('./db/database.js'); -const path = require('path'); -const fs = require('fs'); const app = express(); @@ -14,6 +12,7 @@ const killerRouter = require('./routes/killer.routes.js'); const errorHandler = require('./middleware/errorHandler'); const { getSettings } = require('./services/settingsService.js'); const { logToFile } = require('./services/logService.js'); +const { generatePreview } = require('./services/mediaService.js'); // ? variables let settings = getSettings() @@ -38,43 +37,6 @@ app.use((req, res, next) => { next(); }); -// ? Routes : media access routes -app.use('/api/v1/media/:category/:type/:file', (req, res, next) => { - const category = req.params.category; - const type = req.params.type; - const file = req.params.file; - - if (!['entries', 'collections', 'franchises'].includes(category)) { - return res.status(404).send('Not Found'); - } - - if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) { - return res.status(404).send('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}`); - const filePath = path.join(mediaPath, file); - logToFile(filePath) - fs.access(filePath, fs.constants.F_OK, (err) => { - if (err) { - return res.status(404).send('Not Found'); - } - - 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); - }); -}); - // ? other api routes app.use('/api/v1', routes); diff --git a/backend/services/importerService.js b/backend/services/importerService.js index 878e126..036b279 100644 --- a/backend/services/importerService.js +++ b/backend/services/importerService.js @@ -6,7 +6,7 @@ const EntrySeen = require("../models/EntrySeen"); const Episode = require("../models/Episode"); const EpisodeSeen = require("../models/EpisodeSeen"); const Season = require("../models/Season"); -const { saveEntry } = require("./entryService"); +const { saveEntry } = require("./models-services/entryService"); const { fetchEntryData } = require("./tmdbService"); async function bulkImportMovies(movies) { diff --git a/backend/services/logService.js b/backend/services/logService.js index 5290a57..4b4a729 100644 --- a/backend/services/logService.js +++ b/backend/services/logService.js @@ -1,6 +1,7 @@ // logService.js const fs = require('fs'); const util = require('util'); +const moment = require('../utils/moment'); // Convert fs.writeFile to a promise-based function const writeFile = util.promisify(fs.writeFile); @@ -20,7 +21,10 @@ fs.open(logFilePath, 'a', (err, fd) => { // Asynchronous function to append a message to the log file async function logToFile(message) { try { - await writeFile(logFilePath, `${message}\n`, { flag: 'a' }); + const time = moment().format('YYYY-MM-DD HH:mm:ss') + const formatedMsg = `[${time}] ${message}`; + console.log(formatedMsg); + await writeFile(logFilePath, formatedMsg + '\n', { flag: 'a' }); } catch (err) { console.error("Failed to write to log file:", err); } diff --git a/backend/services/mutlerStorageLocations.js b/backend/services/mutlerStorageLocations.js index 11faa74..60f9aa8 100644 --- a/backend/services/mutlerStorageLocations.js +++ b/backend/services/mutlerStorageLocations.js @@ -9,7 +9,7 @@ const entryPicturesUpload = multer({ multer.diskStorage({ destination: function (req, file, cb) { let folder = 'posters'; - if (file.fieldname == 'banner') folder = 'backdrops'; + if (file.fieldname == 'backdrop') folder = 'backdrops'; if (file.fieldname == 'logo') folder = 'logos'; const settings = getSettings();