This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release v1 api and refactor images into 3 types #3
- Loading branch information
Showing
9 changed files
with
90 additions
and
22 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* VERSION 1 of the API | ||
* 2020 October | ||
* do not make big changes | ||
*/ | ||
const ImageModel = require('../../models/Image'); | ||
const path = require('path'); | ||
|
||
exports.showImageByWikidata = async (req, res) => { | ||
const { id, type } = req.params; | ||
// if (!Number.isInteger(id)) { //already checked at app.js | ||
// res.json({ error: 'Only numbers allowed' }); | ||
// } | ||
const allowedTypes = ['thumbnail','facecrop','original']; | ||
if (allowedTypes.indexOf(type) === -1){ | ||
res.json({ error: true, errorMessage: 'Type not allowed' }); | ||
} | ||
const image = await ImageModel.find({ wikidataEntity: id }, null, { sort: { name: 1 }, limit: 1 }); | ||
if (!image.length) { | ||
res.sendStatus(404); | ||
// res.json({ error: 404 }); | ||
} | ||
if ( !image[0].mimetype) { | ||
res.json({ error: true, errorMessage: 'mimetype', entry: image }); | ||
} | ||
|
||
await ImageModel.updateOne( {_id:image[0]._id} , {viewCount:(image[0].viewCount+1)} ); | ||
|
||
res.setHeader('content-type', image[0].mimetype); | ||
res.sendFile(path.resolve('uploads/'+type+'/' + image[0].id)); | ||
}; | ||
|
||
exports.showImageById = async (req, res) => { | ||
const { id, type } = req.params; | ||
res.setHeader('content-type', "image/jpeg"); | ||
res.sendFile(path.resolve('uploads/'+type+'/' + id)); | ||
}; | ||
|
||
|
||
exports.imageInfo = async (req, res) => { | ||
const { id } = req.params; | ||
const image = await ImageModel.find({ wikidataEntity: id }, null, { sort: { name: 1 }, limit: 1 }); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.json({images:image}); | ||
}; |
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
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
File renamed without changes.
Empty file.
Empty file.
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
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