Skip to content

Commit

Permalink
3855 - create and read files from s3
Browse files Browse the repository at this point in the history
  • Loading branch information
sorja committed Aug 30, 2024
1 parent 1e023c4 commit df8df99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/server/api/cycleData/repository/getRepositoryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Translations } from 'meta/translation'

import { AssessmentController } from 'server/controller/assessment'
import { CycleDataController } from 'server/controller/cycleData'
import { FileStorage, FileStorageUtils } from 'server/service/fileStorage'
import Requests from 'server/utils/requests'
import { Responses } from 'server/utils/responses'

Expand All @@ -26,7 +27,10 @@ export const getRepositoryFile = async (req: Request, res: Response) => {
const extension = file.name.split('.').pop()
const fileName = `${label}.${extension}`

Responses.sendFile(res, fileName, file.file)
const key = repositoryItem.fileUuid
const fileStream = await FileStorage.getFile({ key })

Responses.sendFileStream(res, fileName, fileStream, FileStorageUtils.getContentType(extension))
} catch (e) {
Requests.sendErr(res, e)
}
Expand Down
11 changes: 10 additions & 1 deletion src/server/controller/cycleData/repository/getManyFiles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Readable } from 'stream'

import { CountryIso } from 'meta/area'
import { Assessment, Cycle } from 'meta/assessment'
import { Lang } from 'meta/lang'
import { Translations } from 'meta/translation'

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 @@ -15,7 +18,7 @@ type Props = {

type Returned = Array<{
fileName: string
file: Buffer
file: Readable
}>

export const getManyFiles = async (props: Props): Promise<Returned> => {
Expand All @@ -27,6 +30,12 @@ export const getManyFiles = async (props: Props): Promise<Returned> => {
const repositoryProps = { fileUuids: repositoryItems.map((item) => item.fileUuid) }
const files = await FileRepository.getMany(repositoryProps)

// eslint-disable-next-line no-restricted-syntax
for await (const file of files) {
const { uuid: key } = file
file.file = await FileStorage.getFile({ key })
}

return files.map((file) => {
const repositoryItem = repositoryItems.find((item) => item.fileUuid === file.uuid)
const label = Translations.getLabel({ translation: repositoryItem.props.translation, language: Lang.en })
Expand Down
10 changes: 8 additions & 2 deletions src/server/controller/file/createMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { User } from 'meta/user'
import { BaseProtocol, DB } from 'server/db'
import { ActivityLogRepository } from 'server/repository/public/activityLog'
import { FileRepository } from 'server/repository/public/file'
import { FileStorage } from 'server/service/fileStorage'

type Props = {
assessment: Assessment
Expand All @@ -19,9 +20,14 @@ export const createMany = async (props: Props, client: BaseProtocol = DB): Promi
return client.tx(async (t) => {
return Promise.all(
files.map(async (multerFile) => {
const file = await FileRepository.create({ ...props, file: multerFile }, t)
const fileName = multerFile.originalname
const file = await FileRepository.create({ ...props, fileName }, t)
const { uuid } = file
const key = uuid
const body = multerFile.buffer
await FileStorage.uploadFile({ key, body })

const target = { fileName: file.name, uuid: file.uuid }
const target = { fileName, uuid }
const message = ActivityLogMessage.fileCreate
const activityLog = { target, section: 'assessment', message, user }
const activityLogParams = { activityLog, assessment, cycle }
Expand Down

0 comments on commit df8df99

Please sign in to comment.