Skip to content

Commit

Permalink
FileSize
Browse files Browse the repository at this point in the history
  • Loading branch information
sorja committed Aug 30, 2024
1 parent 5b608f6 commit b570cd6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/meta/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/server/controller/cycleData/repository/getFileMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,8 @@ export const getFileMeta = async (props: Props): Promise<Returned> => {
FileRepository.getSummary({ fileUuid: repositoryItem.fileUuid }),
])

summary.size = await FileStorage.getFileSize({ key: repositoryItem.fileUuid })

return {
usages,
summary: { ...summary, repositoryItemUuid: repositoryItem.uuid },
Expand Down
2 changes: 1 addition & 1 deletion src/server/repository/public/file/fields.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const fieldsFileSummary = ['id', 'name', 'uuid', 'created_at' /* 'length(file) as size' */]
export const fieldsFileSummary = ['id', 'name', 'uuid', 'created_at']
14 changes: 13 additions & 1 deletion src/server/service/fileStorage/fileStorage.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -39,7 +39,19 @@ const uploadFile = async (props: {
await s3Client.send(command)
}

const getFileSize = async (props: { key: string; bucket?: string; path?: string }): Promise<number> => {
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,
}

0 comments on commit b570cd6

Please sign in to comment.