Created
June 4, 2021 13:33
-
-
Save ahmedbaig/0187322199a1bb2833561bd2fbdd4d97 to your computer and use it in GitHub Desktop.
Alternative to multer file uploader using busboy-body-parser in Typescript
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
// Server Code ... | |
const busboyBodyParser = require('busboy-body-parser'); | |
// Express TCP requests parsing | |
app.use(busboyBodyParser({ limit: '10mb', multi:true })); | |
// Some more server code ... |
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
import { Uploader } from "uploader"; | |
export const router = express.Router(); | |
router.post('/uploader', Uploader.fields([{ name: "images" }]), (req, res)=>{console.log(req.body.files}) |
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
import compose from "composable-middleware" | |
import fs from 'fs'; | |
import path from 'path'; | |
import { SenderService } from "../http/services/sender.service"; // Optional Service for res handling | |
// This implementation requires busboy-body-parser initialized in app | |
interface IFields { | |
name: string; | |
} | |
export class Uploader { | |
static dest: string = './public/images'; | |
public static fields(names: IFields[]) { | |
return ( | |
compose() | |
.use((req, res, next) => { | |
if (req.files == null) { next(); } else { | |
names.forEach(o => { | |
if (req.files[o.name] != null) { | |
Uploader.fileFilter(req.files[o.name], (error, status) => { | |
if (!status) SenderService.errorSend(res, { success: false, msg: error, status: 500 }) | |
Uploader.fileStorage(req.files[o.name], (error, status, files) => { | |
if (!status) SenderService.errorSend(res, { success: false, msg: error, status: 500 }) | |
req.body.files = files; | |
next(); | |
}) | |
}) | |
} | |
}) | |
} | |
}) | |
) | |
} | |
public static async fileStorage(files, cb) { | |
let filePaths = await Promise.all(files.map(async file => { | |
let filePath = path.join(Uploader.dest, `${new Date().toISOString().replace(/:/g, "-")}-${file.name}`); | |
await fs.writeFileSync(filePath, file.data); | |
return filePath; | |
})) | |
cb(null, true, filePaths); | |
} | |
public static fileFilter(files, cb) { | |
files.forEach(file => { | |
if ( | |
file.mimetype !== "image/png" && | |
file.mimetype !== "image/jpg" && | |
file.mimetype !== "image/jpeg" | |
) { | |
cb("Image uploaded is not of type jpg/jpeg or png", false); | |
} | |
}); | |
cb(null, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment