-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from fga-eps-mds/i83_scannear_planta
Adding scanner plant router
- Loading branch information
Showing
9 changed files
with
2,773 additions
and
1,128 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,53 @@ | ||
const express = require('express'); | ||
|
||
const router = express.Router(); | ||
const fs = require('fs'); // File System | Node.js | ||
const axios = require('axios'); // HTTP client | ||
const FormData = require('form-data'); // Readable "multipart/form-data" streams | ||
const path = require('path'); | ||
require('dotenv').config(); | ||
|
||
function openFileReadStream(filePath) { | ||
return new Promise((resolve, reject) => { | ||
const readStream = fs.createReadStream(filePath); | ||
readStream.on('open', () => resolve(readStream)); | ||
readStream.on('error', (err) => reject(err)); | ||
}); | ||
} | ||
|
||
router.post('/', async (req, res, next) => { | ||
try { | ||
const apiKey = process.env.PLANT_NET_API_KEY; // api key do plant net | ||
const { data, filename, mime, plantType } = req.body; | ||
const requireFields = { data, filename, mime, plantType }; | ||
const erros = []; | ||
Object.entries(requireFields).forEach(([key, value]) => { | ||
if (!value) erros.push(`${key} is required`); | ||
}); | ||
if (erros.length) throw erros; | ||
const fileFormat = mime.split('/').pop(); | ||
const filePath = path.join(__dirname, '..', `${filename}.${fileFormat}`); | ||
fs.writeFileSync(filePath, data, { encoding: 'base64' }); | ||
const form = new FormData(); | ||
form.append('organs', plantType); | ||
const stream = await openFileReadStream(filePath); | ||
|
||
form.append('images', stream); | ||
|
||
const response = await axios.post( | ||
`https://my-api.plantnet.org/v2/identify/all?api-key=${apiKey}`, | ||
form, | ||
{ | ||
headers: form.getHeaders(), | ||
} | ||
); | ||
fs.unlinkSync(filePath); | ||
|
||
res.send(response.data); | ||
} catch (err) { | ||
if (Array.isArray(err)) res.status(400).send({ errors: err }); | ||
next(err); | ||
} | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.