From b570cd6ff9b6b1d5da613d29a977bc3c7937f699 Mon Sep 17 00:00:00 2001 From: sorja Date: Fri, 30 Aug 2024 14:25:53 +0300 Subject: [PATCH] FileSize --- src/meta/file/file.ts | 2 +- .../controller/cycleData/repository/getFileMeta.ts | 3 +++ src/server/repository/public/file/fields.ts | 2 +- src/server/service/fileStorage/fileStorage.ts | 14 +++++++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/meta/file/file.ts b/src/meta/file/file.ts index 524ce98c31..8d99a04519 100644 --- a/src/meta/file/file.ts +++ b/src/meta/file/file.ts @@ -6,7 +6,7 @@ export type FileSummary = { readonly createdAt: string readonly id: number readonly name: string - readonly size: number + size: number readonly uuid: string readonly repositoryItemUuid: string } diff --git a/src/server/controller/cycleData/repository/getFileMeta.ts b/src/server/controller/cycleData/repository/getFileMeta.ts index e7863a7e6c..832a77f4b5 100644 --- a/src/server/controller/cycleData/repository/getFileMeta.ts +++ b/src/server/controller/cycleData/repository/getFileMeta.ts @@ -3,6 +3,7 @@ import { FileMeta } from 'meta/file' import { RepositoryRepository } from 'server/repository/assessmentCycle/repository' import { FileRepository } from 'server/repository/public/file' +import { FileStorage } from 'server/service/fileStorage' type Props = { assessment: Assessment @@ -24,6 +25,8 @@ export const getFileMeta = async (props: Props): Promise => { FileRepository.getSummary({ fileUuid: repositoryItem.fileUuid }), ]) + summary.size = await FileStorage.getFileSize({ key: repositoryItem.fileUuid }) + return { usages, summary: { ...summary, repositoryItemUuid: repositoryItem.uuid }, diff --git a/src/server/repository/public/file/fields.ts b/src/server/repository/public/file/fields.ts index d01547339c..3749db13c7 100644 --- a/src/server/repository/public/file/fields.ts +++ b/src/server/repository/public/file/fields.ts @@ -1 +1 @@ -export const fieldsFileSummary = ['id', 'name', 'uuid', 'created_at' /* 'length(file) as size' */] +export const fieldsFileSummary = ['id', 'name', 'uuid', 'created_at'] diff --git a/src/server/service/fileStorage/fileStorage.ts b/src/server/service/fileStorage/fileStorage.ts index 39d600def3..eea4bae6ae 100644 --- a/src/server/service/fileStorage/fileStorage.ts +++ b/src/server/service/fileStorage/fileStorage.ts @@ -1,5 +1,5 @@ import { Readable } from 'stream' -import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3' +import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3' import { ProcessEnv } from 'server/utils' @@ -39,7 +39,19 @@ const uploadFile = async (props: { await s3Client.send(command) } +const getFileSize = async (props: { key: string; bucket?: string; path?: string }): Promise => { + const { key, bucket = ProcessEnv.s3BucketName, path = 'public' } = props + const command = new HeadObjectCommand({ + Bucket: bucket, + Key: `${path}/${key}`, + }) + + const response = await s3Client.send(command) + return response.ContentLength || 0 +} + export const FileStorage = { getFile, uploadFile, + getFileSize, }