Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Commit

Permalink
multiple upload #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mshd committed Sep 12, 2020
1 parent 7fbc69d commit 0298057
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
111 changes: 103 additions & 8 deletions controllers/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 <a href="${wikidataLink}">link</a> and entered into the DBs.` });
Expand Down
29 changes: 29 additions & 0 deletions views/image/multiUpload.pug
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0298057

Please sign in to comment.