This repository has been archived on 2023-06-06. You can view files and clone it, but cannot push or open issues or pull requests.
IMIE_CC/pages/api/photos.js
Florian Bouillon 62444d7f7b
presque finis #6 vreste plus qu'a avoir un design propres
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-04-29 15:16:17 +02:00

67 lines
1.6 KiB
JavaScript

import { connexion } from '../../libs/util'
import Photo from '../../models/Photo'
import { promises } from 'fs'
export default async (req, res) => {
await connexion.sync()
res.setHeader('Content-Type', 'application/json')
const pseudo = req.query.pseudo
const picName = req.query.filename
const location = req.query.location
if (location && isNaN(parseInt(location))) {
return res.status(400).end()
}
if (req.method === 'POST') {
if (!pseudo || !picName || !location) {
return res.status(400).end()
}
// post de la photo et upload du body
const path = `/uploads/${pseudo}-${Math.random() * 100000000000000000}-${picName}`
const backendPath = `./public/${path}`
await promises.writeFile(backendPath, await streamToBuffer(req))
try {
const photo = await Photo.create({
pseudo: pseudo,
path,
coinId: parseInt(location)
})
res.statusCode = 200
return res.end(JSON.stringify(photo))
} catch (e) {
res.statusCode = 400
await promises.unlink(backendPath)
console.log(e)
return res.end()
}
} else {
res.statusCode = 200
let builder = {}
if (pseudo) {
builder.pseudo = pseudo
}
if (location) {
builder.coinId = parseInt(location)
}
res.end(JSON.stringify(await Photo.findAll(Object.keys(builder).length > 0 ? {where: builder} : undefined)))
}
}
// Stream.Readable
function streamToBuffer(str){
return new Promise((resolve, reject) => {
const chunks = []
str.once('end', () => resolve(Buffer.concat(chunks)))
str.once('error', err => reject(err))
str.on('data', chunk => chunks.push(chunk))
})
}
export const config = {
api: {
bodyParser: false
}
}