-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from Roger13579/dev
Dev
- Loading branch information
Showing
16 changed files
with
410 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextFunction, Response } from 'express'; | ||
import { UploadService } from '../service/uploadService'; | ||
import { IUploadFileReq } from '../types/upload.type'; | ||
import { BaseController } from './baseController'; | ||
import { UploadFileDTO } from '../dto/uploadFileDto'; | ||
import { CustomResponseType } from '../types/customResponseType'; | ||
import { Request } from 'express'; | ||
|
||
class UploadController extends BaseController { | ||
private readonly uploadService: UploadService = new UploadService(); | ||
|
||
public uploadFile = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
const uploadFileDto = new UploadFileDTO(req as IUploadFileReq); | ||
const url = await this.uploadService.uploadFile(uploadFileDto, res, next); | ||
return this.formatResponse( | ||
CustomResponseType.OK_MESSAGE, | ||
CustomResponseType.OK, | ||
{ url }, | ||
); | ||
}; | ||
} | ||
|
||
export default UploadController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IUser } from '../models/user'; | ||
import { IUploadFileReq } from '../types/upload.type'; | ||
import path from 'path'; | ||
|
||
export class UploadFileDTO { | ||
private readonly _file: Express.Multer.File; | ||
private readonly _name: string; | ||
|
||
get file() { | ||
return this._file; | ||
} | ||
get name() { | ||
return this._name; | ||
} | ||
|
||
constructor(req: IUploadFileReq) { | ||
const { files, params, user } = req; | ||
this._file = files[0]; | ||
this._name = `${params.type}/${params.category}/${(user as IUser)._id}${path.extname(files[0].originalname).toLowerCase()}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { NextFunction } from 'express'; | ||
import firebaseAdmin from '../utils/firebase'; | ||
import { AppError } from '../utils/errorHandler'; | ||
import { HttpStatus } from '../types/responseType'; | ||
import { CustomResponseType } from '../types/customResponseType'; | ||
|
||
const storage = firebaseAdmin.storage(); | ||
const bucket = storage.bucket(); | ||
|
||
const blobConfig = { | ||
action: 'read' as const, | ||
expires: '12-31-2500', | ||
}; | ||
|
||
export class UploadRepository { | ||
public uploadFile = async ( | ||
file: Express.Multer.File, | ||
name: string, | ||
next: NextFunction, | ||
) => { | ||
const blob = bucket.file(name); | ||
return await new Promise<string>((resolve, reject) => { | ||
const blobStream = blob.createWriteStream(); | ||
|
||
blobStream | ||
.on('finish', () => { | ||
blob.getSignedUrl(blobConfig, (err, url) => { | ||
if (err || !url) { | ||
reject( | ||
new AppError( | ||
CustomResponseType.INVALID_UPLOAD, | ||
HttpStatus.BAD_REQUEST, | ||
CustomResponseType.INVALID_UPLOAD_MESSAGE + | ||
'檔案 URL 取得失敗', | ||
), | ||
); | ||
} else { | ||
resolve(url); | ||
} | ||
}); | ||
}) | ||
.on('error', () => { | ||
reject( | ||
next( | ||
new AppError( | ||
CustomResponseType.INVALID_UPLOAD, | ||
HttpStatus.BAD_REQUEST, | ||
CustomResponseType.INVALID_UPLOAD_MESSAGE + '檔案上傳失敗', | ||
), | ||
), | ||
); | ||
}) | ||
.end(file.buffer); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import UploadController from '../controller/uploadController'; | ||
import { UserVerify } from '../middleware/userVerify'; | ||
import { uploadFile } from '../utils/upload'; | ||
import { UploadFilePipe } from '../validator/upload/uploadFile.pipe'; | ||
import { BaseRoute } from './baseRoute'; | ||
|
||
export class UploadRoute extends BaseRoute { | ||
protected controller!: UploadController; | ||
|
||
constructor() { | ||
super(); | ||
this.initial(); | ||
} | ||
|
||
protected initial() { | ||
this.controller = new UploadController(); | ||
this.setRouters(); | ||
} | ||
|
||
protected setRouters() { | ||
this.router.post( | ||
'/v1/uploadFile/:type/:category', | ||
/** | ||
* #swagger.tags= ['Upload'] | ||
* #swagger.summary = '上傳檔案' | ||
* #swagger.security=[{"Bearer": []}], | ||
*/ | ||
|
||
/* #swagger.parameters['type'] = { | ||
in: 'path', | ||
required: true, | ||
description: '上傳種類,e.g. photo', | ||
type: 'string', | ||
} | ||
#swagger.parameters['category'] = { | ||
in: 'path', | ||
required: true, | ||
description: '上傳類型,e.g. user / product', | ||
type: 'string', | ||
} | ||
*/ | ||
/* | ||
#swagger.responses[200] = { | ||
description: 'OK', | ||
schema: { | ||
$ref: '#/definitions/UploadFileSuccess' } | ||
} | ||
*/ | ||
UserVerify, | ||
this.usePipe(UploadFilePipe), | ||
uploadFile, | ||
this.responseHandler(this.controller.uploadFile), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { NextFunction, Response } from 'express'; | ||
import { UploadFileDTO } from '../dto/uploadFileDto'; | ||
import { UploadRepository } from '../repository/uploadRepository'; | ||
import { AppError } from '../utils/errorHandler'; | ||
import { CustomResponseType } from '../types/customResponseType'; | ||
import { HttpStatus } from '../types/responseType'; | ||
|
||
export class UploadService { | ||
private readonly uploadRepository: UploadRepository = new UploadRepository(); | ||
|
||
public uploadFile = async ( | ||
uploadFileDto: UploadFileDTO, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
const { file, name } = uploadFileDto; | ||
if (!file) { | ||
return next( | ||
new AppError( | ||
CustomResponseType.INVALID_UPLOAD, | ||
HttpStatus.BAD_REQUEST, | ||
CustomResponseType.INVALID_UPLOAD_MESSAGE + '上傳檔案不得為空', | ||
), | ||
); | ||
} | ||
return await this.uploadRepository.uploadFile(file, name, next); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.