152 lines
3.9 KiB
JavaScript
152 lines
3.9 KiB
JavaScript
//dotenv
|
|
require('dotenv').config();
|
|
|
|
//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"));
|
|
fastify.register(require("point-of-view"), {
|
|
engine: {
|
|
handlebars: require("handlebars")
|
|
}
|
|
});
|
|
|
|
//socket.io
|
|
var io = require("socket.io")(fastify.server, {
|
|
pingInterval: 1000,
|
|
pingTimeout: 3000
|
|
});
|
|
|
|
//'get'
|
|
fastify.get("/", function (request, reply) {
|
|
reply.view("/src/pages/parade.hbs", {});
|
|
});
|
|
|
|
["/entry", "/entry/"].forEach(function(path) {
|
|
fastify.get(path, function (request, reply) {
|
|
reply.view("/src/pages/entry.hbs", {});
|
|
});
|
|
});
|
|
|
|
//'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.view("/src/pages/entry.hbs", {});
|
|
});
|
|
|
|
//
|
|
var score = require("./public/score.json");
|
|
|
|
//
|
|
//there will be 16 rooms called: "room0", "room1", ... , "room15"
|
|
//if any other room is requested.. well, we will simply reject.
|
|
var roommax = 16;
|
|
|
|
//
|
|
io.on("connection", function(socket) {
|
|
console.log("someone connected.");
|
|
socket.on("disconnect", function() { console.log("someone disconnected."); });
|
|
|
|
socket.on("room", function(room, fn) {
|
|
// parseInt(room)
|
|
if (room >= 0 && room < roommax) {
|
|
socket.join("room" + room);
|
|
fn(true);
|
|
} else {
|
|
fn(false);
|
|
}
|
|
});
|
|
});
|
|
|
|
//
|
|
var pointer = 0; // pointer : 0 ~ (length-1)
|
|
var looper;
|
|
(looper = function(timeout) {
|
|
setTimeout(function() {
|
|
|
|
//pointer = 20;
|
|
// console.log(score[pointer]);
|
|
|
|
//
|
|
for (var index = 0; index < roommax; index++) {
|
|
|
|
// NOTE: 'pointer' must be 'remembered' since 'pointer' will increase almost immediately! pass as argument => 'pointed'
|
|
// NOTE: 'index' is same => 'indexed'
|
|
setTimeout(function(pointed, indexed) {
|
|
|
|
io.to("room" + indexed).emit("post", score[pointed]);
|
|
|
|
}, score[pointer].object.showtime * index, pointer, index);
|
|
}
|
|
|
|
var timegap = score[pointer].timegap.base + Math.random()*score[pointer].timegap.random;
|
|
// console.log(timegap);
|
|
|
|
pointer++;
|
|
if (pointer >= score.length) pointer = 0;
|
|
|
|
looper(timegap);
|
|
}, timeout);
|
|
})(1000);
|
|
|
|
//listen
|
|
fastify.listen(10000, 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}`)
|
|
});
|