First Commit
This commit is contained in:
253
app.js
Normal file
253
app.js
Normal file
@@ -0,0 +1,253 @@
|
||||
port = 8000;
|
||||
|
||||
// Require the libraries:
|
||||
var SocketIOFileUpload = require('socketio-file-upload'),
|
||||
socketio = require('socket.io'),
|
||||
express = require('express');
|
||||
|
||||
const app = express()
|
||||
.use(SocketIOFileUpload.router)
|
||||
.use(express.static(__dirname + "/public"));
|
||||
const http = require('http');
|
||||
const server = http.createServer(app);
|
||||
const { Server } = require("socket.io");
|
||||
const io = new Server(server);
|
||||
|
||||
// Make your Express server:
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + "/forms/select.html");
|
||||
})
|
||||
|
||||
app.get('/upload', (req, res) => {
|
||||
res.sendFile(__dirname + "/forms/upload.html");
|
||||
})
|
||||
|
||||
app.get('/download', (req, res) => {
|
||||
res.sendFile(__dirname + "/forms/download.html");
|
||||
})
|
||||
|
||||
app.get('/download/:hash', (req, res) => {
|
||||
mac = req.params.hash;
|
||||
res.send("sdadsd");
|
||||
})
|
||||
|
||||
app.get('/upload', (req, res) => {
|
||||
// parsedUrl = req.socket.handshake.headers.referer.split(req.socket.handshake.headers.host, 2);
|
||||
// console.log(parsedUrl[0]);
|
||||
// console.log(parsedUrl[1]);
|
||||
|
||||
// Make an instance of SocketIOFileUpload and listen on this socket:
|
||||
res.send("upload");
|
||||
})
|
||||
|
||||
|
||||
|
||||
// Start up Socket.IO:
|
||||
// var io = new socketio.Server(app.listen(port));
|
||||
io.sockets.on("connection", function(socket){
|
||||
if(socket.handshake.url.startsWith("/socket.io")) {
|
||||
var uploader = new SocketIOFileUpload();
|
||||
uploader.dir = "uploads";
|
||||
|
||||
uploader.uploadValidator = function(event, callback){
|
||||
// asynchronous operations allowed here; when done,
|
||||
console.log(event);
|
||||
if (false) {
|
||||
callback(true);
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
};
|
||||
|
||||
uploader.listen(socket);
|
||||
|
||||
// Do something when a file is saved:
|
||||
uploader.on("saved", function(event){
|
||||
console.log(event.file);
|
||||
});
|
||||
// Error handler:
|
||||
uploader.on("error", function(event){
|
||||
console.log("Error from uploader", event);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
res.status(404);
|
||||
console.log(req.query);
|
||||
ans = { error: 'Not found', code: 404 }
|
||||
if (req.query.format == "xml"){
|
||||
res.set('Content-Type', 'text/xml');
|
||||
res.send(xml({xml: ans}));
|
||||
} else if (req.query.format == "csv") {
|
||||
res.send(ans.error);
|
||||
} else if (req.query.format == "json") {
|
||||
res.json(ans);
|
||||
}
|
||||
|
||||
// respond with html page
|
||||
if (req.accepts('html') && !req.url.startsWith("/api")) {
|
||||
res.send('<h1>404 Not found</h1>');
|
||||
return;
|
||||
}
|
||||
|
||||
// respond with json
|
||||
if (req.accepts('json')) {
|
||||
res.json({ error: 'Not found', code: 404 });
|
||||
return;
|
||||
}
|
||||
|
||||
// default to plain-text. send()
|
||||
res.send('404');
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log('listening on *:8000');
|
||||
});
|
||||
|
||||
|
||||
// https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
|
||||
function makeid(length) {
|
||||
let result = '';
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const charactersLength = characters.length;
|
||||
let counter = 0;
|
||||
while (counter < length) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// const express = require('express');
|
||||
// const app = express()
|
||||
// const server = require('http').createServer(app);
|
||||
// var siofu = require("socketio-file-upload");
|
||||
// const port = process.env.PORT || 8080;
|
||||
// const io = require('socket.io')(server);
|
||||
// const fs = require("fs")
|
||||
|
||||
// app.use(express.static("public"));
|
||||
|
||||
|
||||
// io.on("connection", (socket) => {
|
||||
// console.log("req")
|
||||
// var uploader = new siofu();
|
||||
// uploader.dir = "./uploads";
|
||||
// uploader.listen(socket);
|
||||
// });
|
||||
|
||||
// app.listen(8000, () => {
|
||||
// console.log(`Server started...`);
|
||||
// });
|
||||
|
||||
|
||||
// server.listen(port, function() {
|
||||
// console.log(`Listening on port ${port}`);
|
||||
// });
|
||||
|
||||
|
||||
// const express = require('express');
|
||||
// const app = express();
|
||||
// const fs = require("fs")
|
||||
// const s = require("socket.io");
|
||||
|
||||
// const io = new s.Server();
|
||||
|
||||
// io.on("connection", (socket) => {
|
||||
// socket.on("upload", (file, callback) => {
|
||||
// console.log(file); // <Buffer 25 50 44 ...>
|
||||
|
||||
// // save the content to the disk, for example
|
||||
// fs.writeFile("./tmp/upload", file, (err) => {
|
||||
// callback({ message: err ? "failure" : "success" });
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // const multer = require("multer");
|
||||
|
||||
// // const date = Date.now();
|
||||
|
||||
// // const storage = multer.diskStorage({
|
||||
// // destination: function (req, file, cb) {
|
||||
// // if(req.body.name == "test"){
|
||||
// // storage.error("Already Exists");
|
||||
// // }
|
||||
// // const nameFolder = date + "-" +req.body.name;
|
||||
// // const dirRep = nameFolder.replace(/\s/g, '');
|
||||
// // const folderPath = "lessons/"+dirRep+"/";
|
||||
// // fs.mkdirSync(folderPath, {recursive: true})
|
||||
// // cb(null, folderPath)
|
||||
// // },
|
||||
// // filename: function (req, file, cb) {
|
||||
// // const filename = Date.now() + "-" + file.originalname;
|
||||
// // const rep = filename.replace(/\s/g, '')
|
||||
// // cb(null, rep)
|
||||
// // },
|
||||
// // });
|
||||
|
||||
// // const upload = multer({
|
||||
// // storage: storage,
|
||||
// // limits: {
|
||||
// // fileSize: 50000000000
|
||||
// // }
|
||||
// // });
|
||||
// app.use(express.urlencoded({ extended: true }));
|
||||
// app.use(express.static('public'));
|
||||
|
||||
// app.get('/download/:hash', (req, res) => {
|
||||
|
||||
// })
|
||||
|
||||
// // app.post("/upload", upload.array("files"), uploadFiles);
|
||||
|
||||
// // function uploadFiles(req, res) {
|
||||
// // console.log(req.body);
|
||||
// // console.log(req.files);
|
||||
// // res.json({ message: "Successfully uploaded files" });
|
||||
// // }
|
||||
|
||||
// function checkPerm(req, res) {
|
||||
// if(req.body.name == "kuba"){
|
||||
// throw("Invalid name")
|
||||
// }else {
|
||||
// return req, res
|
||||
// }
|
||||
// }
|
||||
|
||||
// app.use(function (req, res, next) {
|
||||
// res.status(404);
|
||||
// console.log(req.query);
|
||||
// ans = { error: 'Not found', code: 404 }
|
||||
// if (req.query.format == "xml"){
|
||||
// res.set('Content-Type', 'text/xml');
|
||||
// res.send(xml({xml: ans}));
|
||||
// } else if (req.query.format == "csv") {
|
||||
// res.send(ans.error);
|
||||
// } else if (req.query.format == "json") {
|
||||
// res.json(ans);
|
||||
// }
|
||||
|
||||
// // respond with html page
|
||||
// if (req.accepts('html') && !req.url.startsWith("/api")) {
|
||||
// res.send('<h1>404 Not found</h1>');
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // respond with json
|
||||
// if (req.accepts('json')) {
|
||||
// res.json({ error: 'Not found', code: 404 });
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // default to plain-text. send()
|
||||
// res.send('404');
|
||||
// });
|
||||
|
||||
// app.listen(5000, () => {
|
||||
// console.log(`Server started...`);
|
||||
// });
|
||||
Reference in New Issue
Block a user