76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
//nextcloud client
|
|
const { Client, Server } = require("nextcloud-node-client");
|
|
const server = new Server({
|
|
basicAuth: {
|
|
password: process.env.nextcloud_PASSWD,
|
|
username: process.env.nextcloud_ID,
|
|
},
|
|
url: process.env.nextcloud_URL,
|
|
});
|
|
|
|
//built-in
|
|
const path = require("path");
|
|
const fs = require('fs')
|
|
const util = require('util')
|
|
const { pipeline } = require('stream')
|
|
const pump = util.promisify(pipeline)
|
|
|
|
//uuid
|
|
const {
|
|
v1: uuidv1,
|
|
v4: uuidv4,
|
|
} = require('uuid');
|
|
|
|
//moment
|
|
const moment = require("moment-timezone");
|
|
|
|
//fastify
|
|
const fastify = require("fastify")({
|
|
logger: false,
|
|
});
|
|
fastify.register(require("fastify-static"), {
|
|
root: path.join(__dirname, "public"),
|
|
prefix: "/",
|
|
});
|
|
fastify.register(require("fastify-formbody"));
|
|
fastify.register(require("fastify-multipart"));
|
|
|
|
//'get'
|
|
fastify.get("/", function (request, reply) {
|
|
reply.sendFile("/index.html")
|
|
});
|
|
|
|
//'post'
|
|
fastify.post("/", async function (request, reply) {
|
|
|
|
// -- "accumulate whole file in memory"
|
|
const data = await request.file()
|
|
const buffer = await data.toBuffer()
|
|
// <<< https://github.com/fastify/fastify-multipart/#accumulate-whole-file-in-memory
|
|
// ^--- this is needed.. dont understand fully, though. related to 'stream' receiving.
|
|
|
|
const client = new Client(server);
|
|
|
|
//create unique folder ==> timestamp + uuid
|
|
const folder = await client.createFolder("Storage/sound-parade/" + moment().tz('Asia/Seoul').format('YYYYMMDD-HHmmss - ') + uuidv1());
|
|
|
|
//files
|
|
console.log(data.fields.audiofile);
|
|
const file = await folder.createFile(data.fields.audiofile.filename, await data.fields.audiofile.toBuffer());
|
|
console.log(data.fields);
|
|
const text = await folder.createFile("message.txt", Buffer.from(data.fields.message.value));
|
|
console.log(data.fields.pixels);
|
|
const image = await folder.createFile(data.fields.pixels.filename, await data.fields.pixels.toBuffer());
|
|
|
|
reply.sendFile("/index.html");
|
|
});
|
|
|
|
//listen
|
|
fastify.listen(process.env.PORT, function (err, address) {
|
|
if (err) {
|
|
fastify.log.error(err)
|
|
process.exit(1)
|
|
}
|
|
console.log(`Your app is listening on ${address}`)
|
|
fastify.log.info(`server listening on ${address}`)
|
|
}); |