From 393fa0f1c8a9eb48b95ec47c7c1fe44f3fe5c456 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Thu, 26 Jun 2025 13:09:05 -0600 Subject: [PATCH 1/2] feat(thumbnails): add file_uuid to thumbnail model and update CreateThumbnail logic --- src/app/models/thumbnail.ts | 22 +++++++++++++--------- src/app/routes/storage.ts | 8 +++++++- src/app/services/files.js | 14 ++++++++++++++ src/app/services/thumbnails.js | 21 +++++++++------------ 4 files changed, 43 insertions(+), 22 deletions(-) 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..693bcf1e 100644 --- a/src/app/services/files.js +++ b/src/app/services/files.js @@ -350,6 +350,19 @@ 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 }, + }, + }); + + if (file) return file; + + throw new Error('File not found'); + }; + const getRecentFiles = (user, limit) => { return Model.file .findAll({ @@ -406,5 +419,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 }, From 696b301e568eab479f3980524a4c1aea01e0dfe2 Mon Sep 17 00:00:00 2001 From: Andres Pinto Date: Thu, 26 Jun 2025 20:25:34 -0400 Subject: [PATCH 2/2] fix: do not throw if file not found to prevent the backend from throwing error 500 --- src/app/services/files.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/services/files.js b/src/app/services/files.js index 693bcf1e..c0278e8c 100644 --- a/src/app/services/files.js +++ b/src/app/services/files.js @@ -358,9 +358,7 @@ module.exports = (Model, App) => { }, }); - if (file) return file; - - throw new Error('File not found'); + return file; }; const getRecentFiles = (user, limit) => {