added example servers

This commit is contained in:
srgooglo 2021-06-07 14:54:42 +02:00
parent 9c2ac1fea1
commit 4e7c3feabb
5 changed files with 72 additions and 17 deletions

View File

@ -1,10 +0,0 @@
module.exports = {
default: (req, res, next) => {
const { files } = req
if (typeof files.file !== "undefined") {
const { data, name, size, mimetype, md5 } = files.file
}
}
}

View File

@ -1,7 +0,0 @@
[
{
"method": "PUT",
"route": "/upload",
"controller": "upload"
}
]

View File

@ -0,0 +1,48 @@
const { Storage } = require('@@classes')
const path = require('path')
const { performance } = require('perf_hooks')
// TODO: Access token, permission object type
const handler = new Storage({ driver: "fs", root: path.resolve(process.cwd(), "uploads") })
module.exports = {
set: (req, res, next) => {
const { files } = req
if (typeof files.file !== "undefined") {
let file = files.file
file.name = `${file.md5}_${file.name}`
const timeBefore = performance.now()
handler.setSync(file, undefined)
.then((stat) => {
const tooks = (performance.now() - timeBefore).toFixed(2)
return res.json({
tooks: `~${tooks}ms`,
filename: file.name,
mimetype: file.mimetype,
encoding: file.encoding,
size: stat.size
})
})
}
},
get: (req, res, next) => {
const { query } = req
if (typeof query.file === "undefined") {
res.status(404)
return res.json({ error: "Not provided filename" })
}
handler.getSync(query.file)
.then((data) => {
res.write(data.buffer, 'binary')
res.end(null, 'binary')
})
.catch((error) => {
res.status(404)
res.json({ error: error.message })
})
}
}

View File

@ -0,0 +1,14 @@
[
{
"method": "PUT",
"route": "/upload",
"controller": "filecontroller",
"fn": "set"
},
{
"method": "GET",
"route": "/resources",
"controller": "filecontroller",
"fn": "get"
}
]

View File

@ -0,0 +1,10 @@
{
"name": "uploadServer",
"version": "0.1.0",
"scripts": {
"start": "relic-server ."
},
"dependencies": {
"@ragestudio/relic-server": "^0.2.2"
}
}