From 02980571451fbe6d2c4ab258a9c1228c2b3c355e Mon Sep 17 00:00:00 2001 From: Martin Schibel Date: Sun, 13 Sep 2020 00:10:34 +0200 Subject: [PATCH] multiple upload #8 --- app.js | 5 ++ controllers/upload.js | 111 +++++++++++++++++++++++++++++++++--- views/image/multiUpload.pug | 29 ++++++++++ 3 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 views/image/multiUpload.pug diff --git a/app.js b/app.js index fbff886..43573a6 100644 --- a/app.js +++ b/app.js @@ -196,6 +196,11 @@ app.get('/api', apiController.getApi); // app.get('/api/lob', apiController.getLob); app.get('/api/upload', lusca({ csrf: true }), uploadController.getFileUpload); app.post('/api/upload', upload.single('myFile'), lusca({ csrf: true }), uploadController.handleSourceUrl, uploadController.postFileUpload); + + +app.get('/image/multi_upload', lusca({ csrf: true }), uploadController.getMultiFileUpload); +app.post('/image/multi_upload', lusca({ csrf: true }), uploadController.handleMultiUrlUpload); + // app.get('/api/pinterest', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getPinterest); // app.post('/api/pinterest', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.postPinterest); // app.get('/api/here-maps', apiController.getHereMaps); diff --git a/controllers/upload.js b/controllers/upload.js index a0d6cb8..dace9f8 100644 --- a/controllers/upload.js +++ b/controllers/upload.js @@ -26,6 +26,108 @@ const download = (url, path, callback) => { }); }; +function createThumbnail(filename){ + sharp('uploads/' + filename).resize(200).toFile('uploads/thumbnails/' + filename, (err, resizeImage) => { + if (err) { + console.log(err); + } else { + console.log(resizeImage); + } + }); +} + +exports.getMultiFileUpload = async (req, res) => { + // res.send(req.params); + + if (!req.query.ids) { + req.flash('errors', { msg: 'Parameter ids is missing' }); + res.redirect('/api/upload'); + } + const ids = req.query.ids.split(","); + console.log(ids); + // for(let id in ids){ + // if (!isEntityId(id)) { + // req.flash('errors', { msg: 'Some ids are invalid.' }); + // res.redirect('/api/upload'); + // } + // } + + // + const wikidataEntities = await getItem(ids, 'en'); + let entities = []; + // console.log(wikidataEntities); + // for(var id, enty in wikidataEntities){ + for(var i in wikidataEntities) { + console.log("Start"+i); + var label = wdk.simplify.labels(wikidataEntities[i].labels).en; + var claims = wdk.simplify.claims(wikidataEntities[i].claims); + var existing = await Image.find({ wikidataEntity: getNumericId(i) }, null, { sort: { name: 1 }, limit: 1 }); + // console.log(claims); + entities.push({ + id: i, + link: wdk.getSitelinkUrl({ site: 'wikidata', title: i }), + images: claims.P18, + existing: existing, + label: label, + }); + } + console.log(entities); + // const label = wdk.simplify.labels(wikidataInfo[wikidataId].labels).en; + res.render('image/multiUpload', { + title: 'File Upload', + entities: entities + // query: req.query + }); +}; + +exports.handleMultiUrlUpload = async (req, res, next) => { + const urls = req.body.sourceUrl; + if(urls === undefined){ + req.flash('errors', {msg: 'Nothing.'}); + res.redirect(req.header('Referer') || '/'); + } + const wikidataInfo = await getItem(Object.keys(urls), 'en'); + let uploadedCount = 0; + for(let wikidataId in urls) { + try { + var file = getRandomFilename(); + var sourceUrl = urls[wikidataId]; + if(sourceUrl) { + download(sourceUrl, "uploads/" + file, () => { + // res.savedUrl = file; + let imageDetails = { + wikidataEntity: getNumericId(wikidataId), + wikidataLabel: wdk.simplify.labels(wikidataInfo[wikidataId].labels).en, + sourceUrl: sourceUrl, + internalFileName: file, + // originalFilename: sourceUrl + viewCount: 0 + }; + imageDetails.mimetype = 'image/jpeg'; + var image = new Image(imageDetails); + image.save((err) => { + if (err) { + console.log(err); + req.flash('errors', {msg: 'Entry couldn\'t be saved'}); + res.redirect(req.header('Referer') || '/'); + } + }); + createThumbnail(imageDetails.internalFileName); + uploadedCount++; + }); + // next(); + } + + } catch (err) { + req.flash('errors', {msg: 'URL invalid.'}); + res.redirect(req.header('Referer') || '/'); + } + } + req.flash('success', { msg: `${uploadedCount} files uploaded.` }); + res.redirect(req.header('Referer') || '/'); + +}; + exports.getFileUpload = (req, res) => { res.render('api/upload', { title: 'File Upload', @@ -94,14 +196,7 @@ exports.postFileUpload = async (req, res, next) => { } }); - sharp('uploads/' + imageDetails.internalFileName).resize(200).toFile('uploads/thumbnails/' + imageDetails.internalFileName, (err, resizeImage) => { - if (err) { - console.log(err); - } else { - console.log(resizeImage); - } - }); - + createThumbnail(imageDetails.internalFileName); const wikidataLink = wdk.getSitelinkUrl({ site: 'wikidata', title: wikidataId }); req.flash('success', { msg: `File was uploaded successfully link and entered into the DBs.` }); diff --git a/views/image/multiUpload.pug b/views/image/multiUpload.pug new file mode 100644 index 0000000..466cfce --- /dev/null +++ b/views/image/multiUpload.pug @@ -0,0 +1,29 @@ +extends ../layout + +block content + .pb-2.mt-2.mb-4.border-bottom + h2 + i.fas.fa-upload.fa-sm + | Multiple entity image upload + + //h3 File Upload Form + .row + .col-md-6 + p All files will be uploaded + form(action='/image/multi_upload' role='form', method='POST') + input(type='hidden', name='_csrf', value=_csrf) + .form-group + for entry in entities + label.col-form-label.font-weight-bold + a(href=entry.link)=entry.label + .col-md-12 + //input(type='hidden', name='wikidataEntityId' id='wikidataEntity_id', value=entry.id) + if entry.images + span Image already present on Wikidata + else if entry.existing.length > 0 + span Image already present in Database + else + input(type='text', name='sourceUrl['+entry.id+']') + span(style='margin-left:20px') + a(target='_blank' href='https://www.google.com/search?tbm=isch&as_q='+entry.label) google + button.btn.btn-primary(type='submit') Submit