diff --git a/package.json b/package.json index 1bd34fb..62dec31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "niffler", - "version": "1.4.1", + "version": "1.6.0", "description": "A minio based storage microservice", "main": "dist/index.js", "scripts": { diff --git a/src/data/repositories/StorageRepository.ts b/src/data/repositories/StorageRepository.ts index e744091..84a4979 100644 --- a/src/data/repositories/StorageRepository.ts +++ b/src/data/repositories/StorageRepository.ts @@ -1,6 +1,6 @@ import { S3 } from 'aws-sdk' -import { Body } from 'aws-sdk/clients/s3' import { IFile } from '../../domain/structures/interfaces/IFile' +import { IFileBuffer } from '../../domain/structures/interfaces/IFileBuffer' export interface IStorageRepositoryConfig { ttl: number, @@ -48,10 +48,10 @@ export class StorageRepository { return file } - async download (id: string): Promise { - const { Body: buffer } = await this.s3.getObject({ Key: id, Bucket: this.bucket }) + async download (id: string): Promise { + const { Body: buffer, ContentType: mimetype } = await this.s3.getObject({ Key: id, Bucket: this.bucket }) .promise() - return buffer + return { buffer, mimetype } } } diff --git a/src/domain/structures/interfaces/IFileBuffer.ts b/src/domain/structures/interfaces/IFileBuffer.ts new file mode 100644 index 0000000..e10b983 --- /dev/null +++ b/src/domain/structures/interfaces/IFileBuffer.ts @@ -0,0 +1,6 @@ +import { Body } from "aws-sdk/clients/s3"; + +export interface IFileBuffer { + buffer: Body | undefined, + mimetype: string | undefined +} diff --git a/src/presentation/routes/download.ts b/src/presentation/routes/download.ts index 10b74e3..68d4ca6 100644 --- a/src/presentation/routes/download.ts +++ b/src/presentation/routes/download.ts @@ -8,9 +8,11 @@ import { Request, Response, RequestHandler, NextFunction } from 'express' export function factory (service: StorageService): RequestHandler[] { return [ rescue(async (req: Request, res: Response) => { - const file = await service.download(req.params[0]) + const { buffer, mimetype } = await service.download(req.params[0]) - res.status(200).send(file) + res.set({ 'Content-Type': mimetype }) + + res.status(200).send(buffer) }), (err: AWSError, _req: Request, _res: Response, next: NextFunction) => { if (err.statusCode === 404) { diff --git a/src/services/StorageService.ts b/src/services/StorageService.ts index 28ed94c..b3d4057 100644 --- a/src/services/StorageService.ts +++ b/src/services/StorageService.ts @@ -1,5 +1,5 @@ -import { Body } from 'aws-sdk/clients/s3' import { IFile } from '../domain/structures/interfaces/IFile' +import { IFileBuffer } from '../domain/structures/interfaces/IFileBuffer' import { StorageRepository } from '../data/repositories/StorageRepository' export class StorageService { @@ -13,7 +13,7 @@ export class StorageService { return this.repository.findById(id) } - async download (id: string): Promise { + async download (id: string): Promise { return this.repository.download(id) } }