1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| const router = require('express').Router(); const mongoose = require('mongoose'); const upload = require('../lib/local_upload_util');
const UploadModel = mongoose.model('uploads');
module.exports = (app) => { app.use('/', router); };
router.get('/', (req, res, next) => { UploadModel.find((err, lists) => { if (err) return next(err); return res.render('list', { title: '업로드한 리스트', lists, }); }); });
router.get('/single', (req, res, next) => { res.render('single', { title: '이미지 1개 업로드 form', }); });
router.post('/single', (req, res, next) => { const uploadInit = upload.multerInit('images').single('attachment'); uploadInit(req, res, (err) => { if (err) return next(err); const uploadData = new UploadModel({ title: req.body.title, uploadImagePath: [upload.changeDBPath(req.file.path)], uploadImageName: [req.file.filename], imageOriginalName: [req.file.originalname], }); return uploadData.save((saveErr) => { if (saveErr) return next(saveErr); return res.redirect('/'); }); }); });
router.get('/array', (req, res, next) => { res.render('array', { title: '이미지 여러개 업로드 form', }); });
router.post('/array', (req, res, next) => { const uploadInit = upload.multerInit('images').array('attachment'); uploadInit(req, res, (err) => { if (err) return next(err); const convertMulterData = upload.convertMulterData(req.files); if (!convertMulterData) return next(new Error('업로드된 파일들의 정보를 변환할 수 업음')); const uploadData = new UploadModel({ title: req.body.title, uploadImagePath: convertMulterData.uploadPaths, uploadImageName: convertMulterData.fileName, imageOriginalName: convertMulterData.fileOriginalName, }); return uploadData.save((saveErr) => { if (saveErr) return next(saveErr); return res.redirect('/'); }); }); });
router.get('/fields', (req, res, next) => { res.render('fields', { title: '이미지 각각 테스트 form', }); });
router.post('/fields', (req, res, next) => { const uploadInit = upload.multerInit('images').fields([{ name: 'attachment1', maxCount: 1 }, { name: 'attachment2', maxCount: 1 }]); uploadInit(req, res, (err) => { if (err) return next(err); const uploadData = new UploadModel({ title: req.body.title, uploadImagePath: [upload.changeDBPath(req.files.attachment1[0].path), upload.changeDBPath(req.files.attachment2[0].path)], uploadImageName: [req.files.attachment1[0].filename, req.files.attachment2[0].filename], imageOriginalName: [req.files.attachment1[0].originalname, req.files.attachment2[0].originalname], }); return uploadData.save((saveErr) => { if (saveErr) return next(saveErr); return res.redirect('/'); }); }); });
router.get('/download', (req, res, next) => { UploadModel.findOne({ _id: req.query._id }, (err, data) => { if (err) return next(err); if (data.uploadImagePath.length === 1) { res.cookie('isDownload', 'complete', { maxAge: 10000, }); res.setHeader('Content-disposition', 'attachment'); return res.download(upload.changeUploadPath(data.uploadImagePath[0]), data.imageOriginalName[0]); } return upload.zipDownload(data.uploadImagePath, data.imageOriginalName).then((result) => { res.cookie('isDownload', 'complete', { maxAge: 10000, }); res.setHeader('Content-disposition', 'attachment'); return res.download(result); }).catch((zipDownloadErr) => next(zipDownloadErr)); }); });
|