Skip to content

Commit

Permalink
store file with a uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
jerekm committed Oct 16, 2024
1 parent f930114 commit 45c9e61
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion backend/src/v1/middlewares/storage/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import os from 'os';
import retry from 'async-retry';
import { S3_BUCKET, S3_OPTIONS } from '../../../constants/admin';
import PATH from 'path';
import { v4 as uuidv4 } from 'uuid';

interface Options {
folder: string;
Expand All @@ -23,6 +24,8 @@ export const useUpload = (options: Options) => {
logger.log('info', 'Uploading file to S3');

const { path, name, type, size } = file;
const lastDotIndex = name.lastIndexOf('.');
const ext = lastDotIndex !== -1 ? name.substring(lastDotIndex + 1) : '';

if (!path.startsWith(os.tmpdir())) {
logger.error('File not uploaded to temp directory');
Expand All @@ -41,7 +44,7 @@ export const useUpload = (options: Options) => {
const stream = fs.createReadStream(path);
const uploadParams: PutObjectCommandInput = {
Bucket: S3_BUCKET,
Key: `${options.folder}/${data.attachmentId}/${name}`,
Key: `${options.folder}/${data.attachmentId}/${uuidv4()}.${ext}`,
Body: stream,
ContentType: type,
ContentLength: size,
Expand Down

0 comments on commit 45c9e61

Please sign in to comment.