Skip to content

Commit 42a70ba

Browse files
committed
feat: add userId handling in file upload methods and syncFile function
1 parent fdb6069 commit 42a70ba

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/server/controllers/files.controller.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ export class FilesController {
100100
const filePath = `${dirPath}/${filename}`.replace(/\\/g, '/');
101101
await FileUtils.createFileWithPermissions(filePath, buffer);
102102

103+
const userId = (request as any).user?.id as string | undefined;
104+
103105
// Sync file after upload
104-
await FilesController.syncFile(filePath);
106+
await FilesController.syncFile(filePath, filename, userId);
105107

106108
// Prepare response info
107109
const fileInfo = {
@@ -140,6 +142,7 @@ export class FilesController {
140142
await FileUtils.ensureDirectoryWithPermissions(dirPath);
141143

142144
const results = [];
145+
const userId = (request as any).user?.id as string | undefined;
143146
for (const file of files) {
144147
const { base64, filename, mimetype } = file;
145148
if (!base64 || !filename || !mimetype) {
@@ -160,7 +163,7 @@ export class FilesController {
160163
await FileUtils.createFileWithPermissions(filePath, buffer);
161164

162165
// Sync file after upload
163-
await FilesController.syncFile(filePath);
166+
await FilesController.syncFile(filePath, filename, userId);
164167

165168
results.push({
166169
fileName: filename,
@@ -218,6 +221,8 @@ export class FilesController {
218221
}
219222
const sanitizedFiles = (files as Express.Multer.File[]).map(({ fieldname, ...fileData }) => fileData);
220223

224+
const userId = (request as any).user?.id as string | undefined;
225+
221226
// reduce value of key "path" to be relative to storage directory
222227
for (const file of sanitizedFiles) {
223228
// Save original filesystem path before overwriting
@@ -236,7 +241,7 @@ export class FilesController {
236241
Object.assign(file, sanitizedFile);
237242

238243
// Sync file after upload using the original filesystem path
239-
await FilesController.syncFile(originalFilePath, file.originalname);
244+
await FilesController.syncFile(originalFilePath, file.originalname, userId);
240245
}
241246

242247
sendSuccess(response, sanitizedFiles, 'Files uploaded successfully', 200);
@@ -248,7 +253,7 @@ export class FilesController {
248253
* @param filePath Path to the file to sync
249254
* @param originalFilename Optional original filename before any transformations
250255
*/
251-
static async syncFile(filePath: string, originalFilename?: string): Promise<void> {
256+
static async syncFile(filePath: string, originalFilename?: string, userId?: string): Promise<void> {
252257
try {
253258
// Normalize path separators
254259
const normalizedPath = filePath.replace(/\\/g, '/');
@@ -311,6 +316,7 @@ export class FilesController {
311316
createdAt: stats.birthtime.toISOString(),
312317
modifiedAt: stats.mtime.toISOString(),
313318
uploadedAt: new Date().toISOString(),
319+
userId: userId || null,
314320
tags: [storageFolder],
315321
metadata: {
316322
storageFolder: storageFolder

src/server/routes/file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express, { Request, Response } from 'express';
22
import { FilesController, uploadFiles } from '../controllers/files.controller.js';
33
import { PreviewController } from '../controllers/preview.controller.js';
44
import { fileUploadRateLimiter } from '../middlewares/rate-limit.js';
5+
import { jwtAuthMiddleware } from '../middlewares/auth.js';
56

67
const router = express.Router();
78

@@ -44,7 +45,7 @@ const router = express.Router();
4445
* - files: Array of files with base64, filename, mimetype
4546
*
4647
*/
47-
router.post('/upload', fileUploadRateLimiter, (req, res) => {
48+
router.post('/upload', jwtAuthMiddleware, fileUploadRateLimiter, (req, res) => {
4849

4950
/**
5051
* Single File Upload via JSON Body

0 commit comments

Comments
 (0)