Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
e66ff18fda
commit
a0f4a5f393
2
.gitignore
vendored
2
.gitignore
vendored
@ -25,3 +25,5 @@ yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
.storage
|
||||
/public/uploads/*
|
||||
!/public/uploads/.gitkeep
|
||||
|
@ -3,7 +3,44 @@ import Coin from '../../models/Coin'
|
||||
|
||||
export default async (req, res) => {
|
||||
await connexion.sync()
|
||||
res.statusCode = 200
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.end(JSON.stringify(await Coin.findAll()))
|
||||
const distanceVal = toInt(req.query.distance)
|
||||
const coordx = toFloat(req.query.coordx)
|
||||
const coordy = toFloat(req.query.coordy)
|
||||
const coins = await Coin.findAll()
|
||||
if (distanceVal && coordx && coordy) {
|
||||
const result = []
|
||||
for (const coin of coins) {
|
||||
const tmp = coin.toJSON()
|
||||
console.log(tmp)
|
||||
console.log(distance(tmp.coordx, tmp.coordy, coordx, coordy))
|
||||
if ((distance(tmp.coordx, tmp.coordy, coordx, coordy) * 1000) <= distanceVal) {
|
||||
result.push(tmp)
|
||||
}
|
||||
}
|
||||
return res.status(200).end(JSON.stringify(result))
|
||||
}
|
||||
|
||||
res.status(200).end(JSON.stringify(coins))
|
||||
}
|
||||
|
||||
function toFloat(number) {
|
||||
const res = parseFloat(number)
|
||||
return !isNaN(res) ? res : undefined
|
||||
}
|
||||
|
||||
function toInt(number) {
|
||||
const res = parseInt(number)
|
||||
return !isNaN(res) ? res : undefined
|
||||
}
|
||||
|
||||
// return distance in km
|
||||
function distance(lat1, lon1, lat2, lon2) {
|
||||
var p = 0.017453292519943295;
|
||||
var c = Math.cos;
|
||||
var a = 0.5 - c((lat2 - lat1) * p)/2 +
|
||||
c(lat1 * p) * c(lat2 * p) *
|
||||
(1 - c((lon2 - lon1) * p))/2;
|
||||
|
||||
return 12742 * Math.asin(Math.sqrt(a))
|
||||
}
|
||||
|
66
pages/api/photos.js
Normal file
66
pages/api/photos.js
Normal file
@ -0,0 +1,66 @@
|
||||
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.location = 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
|
||||
}
|
||||
}
|
0
public/uploads/.gitkeep
Normal file
0
public/uploads/.gitkeep
Normal file
Reference in New Issue
Block a user