diff --git a/src/app/models/thumbnail.ts b/src/app/models/thumbnail.ts index d2333738..8c208230 100644 --- a/src/app/models/thumbnail.ts +++ b/src/app/models/thumbnail.ts @@ -3,6 +3,7 @@ import { Sequelize, ModelDefined, DataTypes } from 'sequelize'; export interface ThumbnailAttributes { id: number; file_id: number; + file_uuid: string; type: string; size: number; bucket_id: string; @@ -22,35 +23,38 @@ export default (database: Sequelize): ThumbnailModel => { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, - allowNull: false + allowNull: false, }, file_id: { type: DataTypes.INTEGER, - allowNull: false + allowNull: false, + }, + file_uuid: { + type: DataTypes.STRING, }, max_width: { type: DataTypes.INTEGER, - allowNull: false + allowNull: false, }, max_height: { type: DataTypes.INTEGER, - allowNull: false + allowNull: false, }, type: { type: DataTypes.STRING, - allowNull: false + allowNull: false, }, size: { - type: DataTypes.BIGINT.UNSIGNED + type: DataTypes.BIGINT.UNSIGNED, }, bucket_id: { - type: DataTypes.STRING(24) + type: DataTypes.STRING(24), }, bucket_file: { - type: DataTypes.STRING(24) + type: DataTypes.STRING(24), }, encrypt_version: { - type: DataTypes.STRING(20) + type: DataTypes.STRING(20), }, created_at: { type: DataTypes.VIRTUAL, diff --git a/src/app/routes/storage.ts b/src/app/routes/storage.ts index 39aa54d9..0dd6b39d 100644 --- a/src/app/routes/storage.ts +++ b/src/app/routes/storage.ts @@ -803,7 +803,13 @@ export class StorageController { return res.status(400).json({ error: 'Invalid metadata for new thumbnail' }); } - const result = await this.services.Thumbnails.CreateThumbnail(behalfUser, thumbnail); + const file = await this.services.Files.getFileByUserAndNumericId(behalfUser, thumbnail.file_id); + if (!file) { + this.logger.error(`File not found for thumbnail ${thumbnail.file_id}`); + return res.status(404).json({ error: 'File not found' }); + } + + const result = await this.services.Thumbnails.CreateThumbnail(behalfUser, thumbnail, file); res.status(200).json(result); } diff --git a/src/app/services/files.js b/src/app/services/files.js index 16851a80..c0278e8c 100644 --- a/src/app/services/files.js +++ b/src/app/services/files.js @@ -350,6 +350,17 @@ module.exports = (Model, App) => { }); }; + const getFileByUserAndNumericId = async (user, numericId) => { + const file = await Model.file.findOne({ + where: { + id: { [Op.eq]: numericId }, + userId: { [Op.eq]: user.id }, + }, + }); + + return file; + }; + const getRecentFiles = (user, limit) => { return Model.file .findAll({ @@ -406,5 +417,6 @@ module.exports = (Model, App) => { getFileByFolder, getByFolderAndUserId, getFileByFileId, + getFileByUserAndNumericId, }; }; diff --git a/src/app/services/thumbnails.js b/src/app/services/thumbnails.js index d2bc4b5b..86994a32 100644 --- a/src/app/services/thumbnails.js +++ b/src/app/services/thumbnails.js @@ -2,8 +2,7 @@ const sequelize = require('sequelize'); const { Op } = sequelize; module.exports = (Model, App) => { - const CreateThumbnail = async (user, thumbnail) => { - + const CreateThumbnail = async (user, thumbnail, file) => { const thumbnailExists = await Model.thumbnail.findOne({ where: { file_id: { [Op.eq]: thumbnail.file_id }, @@ -13,6 +12,7 @@ module.exports = (Model, App) => { const thumbnailInfo = { file_id: thumbnail.file_id, + file_uuid: file.uuid, type: thumbnail.type, max_width: thumbnail.max_width, max_height: thumbnail.max_height, @@ -27,17 +27,14 @@ module.exports = (Model, App) => { if (thumbnailExists) { thumbnailInfo.updated_at = new Date(); await App.services.Inxt.DeleteFile(user, thumbnailExists.bucket_id, thumbnailExists.bucket_file); - await Model.thumbnail.update( - thumbnailInfo, - { - where: { - file_id: { [Op.eq]: thumbnail.file_id }, - max_width: { [Op.eq]: thumbnail.max_width }, - max_height: { [Op.eq]: thumbnail.max_height }, - type: { [Op.eq]: thumbnail.type }, - } + await Model.thumbnail.update(thumbnailInfo, { + where: { + file_id: { [Op.eq]: thumbnail.file_id }, + max_width: { [Op.eq]: thumbnail.max_width }, + max_height: { [Op.eq]: thumbnail.max_height }, + type: { [Op.eq]: thumbnail.type }, }, - ); + }); return await Model.thumbnail.findOne({ where: { file_id: { [Op.eq]: thumbnail.file_id },