fix: uploading endpoint

This commit is contained in:
Oleg Proskurin 2026-02-01 20:04:46 +07:00
parent bf248c3f2f
commit 196b91ae6a
7 changed files with 35 additions and 44 deletions

View File

@ -63,14 +63,14 @@ uploadRouter.post(
`[${timestamp}] [${requestId}] Uploading file: ${file.originalname} as ${imageId} (${file.size} bytes)`, `[${timestamp}] [${requestId}] Uploading file: ${file.originalname} as ${imageId} (${file.size} bytes)`,
); );
const uploadResult = await storageService.uploadFile( const uploadResult = await storageService.uploadFile({
orgSlug, orgSlug,
projectSlug, projectSlug,
imageId, imageId,
file.buffer, buffer: file.buffer,
file.mimetype, contentType: file.mimetype,
file.originalname, originalFilename: file.originalname,
); });
if (!uploadResult.success) { if (!uploadResult.success) {
const errorResponse: UploadFileResponse = { const errorResponse: UploadFileResponse = {

View File

@ -197,15 +197,16 @@ imagesRouter.post(
try { try {
const storageService = await StorageFactory.getInstance(); const storageService = await StorageFactory.getInstance();
const imageId = randomUUID();
const uploadResult = await storageService.uploadFile( const uploadResult = await storageService.uploadFile({
orgId, orgSlug: orgId,
projectSlug, projectSlug,
'uploads', imageId,
file.originalname, buffer: file.buffer,
file.buffer, contentType: file.mimetype,
file.mimetype, originalFilename: file.originalname,
); });
if (!uploadResult.success) { if (!uploadResult.success) {
res.status(500).json({ res.status(500).json({

View File

@ -70,14 +70,14 @@ export class ImageGenService {
// Original filename for metadata (e.g., "my-image.png") // Original filename for metadata (e.g., "my-image.png")
const originalFilename = `generated-image.${generatedData.fileExtension}`; const originalFilename = `generated-image.${generatedData.fileExtension}`;
const uploadResult = await storageService.uploadFile( const uploadResult = await storageService.uploadFile({
finalOrgSlug, orgSlug: finalOrgSlug,
finalProjectSlug, projectSlug: finalProjectSlug,
imageId, imageId,
generatedData.buffer, buffer: generatedData.buffer,
generatedData.mimeType, contentType: generatedData.mimeType,
originalFilename, originalFilename,
); });
if (uploadResult.success) { if (uploadResult.success) {
return { return {

View File

@ -1,5 +1,5 @@
import { Client as MinioClient } from 'minio'; import { Client as MinioClient } from 'minio';
import { StorageService, FileMetadata, UploadResult } from './StorageService'; import { StorageService, FileMetadata, UploadResult, UploadFileParams } from './StorageService';
export class MinioStorageService implements StorageService { export class MinioStorageService implements StorageService {
private client: MinioClient; private client: MinioClient;
@ -102,14 +102,9 @@ export class MinioStorageService implements StorageService {
return await this.client.bucketExists(this.bucketName); return await this.client.bucketExists(this.bucketName);
} }
async uploadFile( async uploadFile(params: UploadFileParams): Promise<UploadResult> {
orgSlug: string, const { orgSlug, projectSlug, imageId, buffer, contentType, originalFilename } = params;
projectSlug: string,
imageId: string,
buffer: Buffer,
contentType: string,
originalFilename?: string,
): Promise<UploadResult> {
// Validate inputs first // Validate inputs first
this.validatePath(orgSlug, projectSlug, imageId); this.validatePath(orgSlug, projectSlug, imageId);

View File

@ -21,6 +21,15 @@ export interface UploadResult {
error?: string; error?: string;
} }
export interface UploadFileParams {
orgSlug: string;
projectSlug: string;
imageId: string;
buffer: Buffer;
contentType: string;
originalFilename?: string;
}
export interface StorageService { export interface StorageService {
/** /**
* Create the main bucket if it doesn't exist * Create the main bucket if it doesn't exist
@ -35,22 +44,8 @@ export interface StorageService {
/** /**
* Upload a file to storage * Upload a file to storage
* Path format: {orgSlug}/{projectSlug}/img/{imageId} * Path format: {orgSlug}/{projectSlug}/img/{imageId}
*
* @param orgSlug Organization slug
* @param projectSlug Project slug
* @param imageId UUID for the file (same as image.id in DB)
* @param buffer File buffer
* @param contentType MIME type
* @param originalFilename Original filename from user (for metadata)
*/ */
uploadFile( uploadFile(params: UploadFileParams): Promise<UploadResult>;
orgSlug: string,
projectSlug: string,
imageId: string,
buffer: Buffer,
contentType: string,
originalFilename?: string,
): Promise<UploadResult>;
/** /**
* Download a file from storage * Download a file from storage

View File

@ -1,6 +1,6 @@
# @base = http://localhost:3000 # @base = http://localhost:3000
@base = https://api.banatie.app @base = https://api.banatie.app
@apiKey = bnt_e6eb544c505922b9bfe5b088e067fc3940efff16b1b88585c5518946630d4a66 @apiKey = bnt_415a15747796a9676cb47c663fcede36af3583cedaec19893b831ec7a2ae0311
############################################################################### ###############################################################################
# BASIC GENERATION TESTS # BASIC GENERATION TESTS

View File

@ -2,7 +2,7 @@
# @base = https://api.banatie.app # @base = https://api.banatie.app
# @apiKey = bnt_e6eb544c505922b9bfe5b088e067fc3940efff16b1b88585c5518946630d4a66 # @apiKey = bnt_e6eb544c505922b9bfe5b088e067fc3940efff16b1b88585c5518946630d4a66
@apiKey = bnt_f4fcf12c7b62a48dd660dcebccc744d4e668f66ffdf54b873c4ee866c0008763 @apiKey = bnt_415a15747796a9676cb47c663fcede36af3583cedaec19893b831ec7a2ae0311
############################################################################### ###############################################################################
# IMAGE UPLOAD & CRUD TESTS # IMAGE UPLOAD & CRUD TESTS