import { Express , Router , Request , Response } from 'express' ; import * as multer from 'multer' ; import * as multerS3 from 'multer-s3' ; import * as AWS from 'aws-sdk' ; const s3 = new AWS . S3 ( ) ; const bucketName = 'cj-wang-mean-start-upload-file' ; const upload = multer ( { storage : multerS3 ( { s3 : s3 , bucket : bucketName , acl : 'public-read' , key : function ( req , file , cb ) { cb ( null , file . originalname ) ; } } ) } ) ; /**
 * Store infomation of all the uploaded files
 * TODO: Use DB stoorage
 */ const files = { } ; export { files } ; export default Router ( ) . post ( '/upload-file' , upload . single ( 'file' ) , uploadFile ) . get ( '/upload-file' , listFiles ) . delete ( '/upload-file/:_filename' , remove ) . delete ( '/upload-file' , removeAll ) ; param req param res export function uploadFile ( req , res ) { const file = req . file ; file [ 'uploadDate' ] = new Date ( ) ; files [ file . originalname ] = file ; res . end ( ) ; } ; param req param res export function listFiles ( req , res ) { const filesArray = [ ] ; Object . keys ( files ) . forEach ( ( filename ) => { filesArray . push ( files [ filename ] ) ; } ) ; res . json ( { data : filesArray } ) ; } ; param req param res export function remove ( req , res ) { const file = files [ req . params . _filename ] ; delete files [ req . params . _filename ] ; res . end ( ) ; } param req param res export function removeAll ( req , res ) { Object . keys ( files ) . forEach ( ( filename ) => { delete files [ filename ] ; } ) ; res . end ( ) ; }