Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *** UPLOAD ***
- router.post(
- '/upload',
- auth.ifAuthThenNext,
- asyncMiddleware(async (req, res) => {
- const { relative_path } = req.query
- // *** VALIDATIONS ***
- // validate query
- const validationQueryResult = await validation.files.upload.query.validate(req.query)
- // error
- if (validationQueryResult.error) {
- return res.status(httpStatus.BAD_REQUEST).json({
- success: false,
- message: 'Некорректный формат входных параметров',
- })
- }
- // *** UPLOAD FILES ***
- const uploadPath = path.join(sourceFilesDirectory, relative_path)
- setDirectoryWatherFreeze(true)
- // it's works synchronously
- createDirectoryIfNotExists(uploadPath)
- console.log(relative_path)
- console.log(uploadPath)
- const form = formidable({
- multiples: true,
- uploadDir: uploadPath,
- keepExtensions: true,
- maxFileSize: process.env.UPLOAD_MAX_SIZE,
- maxFields: process.env.UPLOAD_MAX_NUM,
- })
- // handle file with exact image extensions
- form.onPart = (part) => {
- if (['image/png', 'image/jpg', 'image/jpeg'].includes(part.mime)) {
- form.handlePart(part)
- }
- }
- const parseFormResult = await parseForm(form, req)
- console.log(parseFormResult)
- onDirectoryChange(false, req.user.id)
- setDirectoryWatherFreeze(false)
- if (parseFormResult === null) {
- return res.status(httpStatus.BAD_REQUEST).json({
- success: false,
- data: {},
- message: 'Ошибка при загрузке файлов, свяжитесь с администратором',
- })
- }
- return res.status(httpStatus.OK).json({ success: true, data: parseFormResult })
- }),
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement