Add app.js
This commit is contained in:
85
app.js
85
app.js
@@ -11,60 +11,99 @@ const app = express()
|
||||
const http = require('http');
|
||||
const server = http.createServer(app);
|
||||
const { Server } = require("socket.io");
|
||||
const { SqliteHandler, UploadObject } = require('./handlers/sqliteHandler');
|
||||
var fs = require('fs')
|
||||
const io = new Server(server);
|
||||
|
||||
sqliteobj = new SqliteHandler("database.sqlite");
|
||||
db = sqliteobj.getDatabaseObj();
|
||||
|
||||
// Make your Express server:
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + "/forms/select.html");
|
||||
})
|
||||
|
||||
app.get('/api', (req, res) => {
|
||||
res.status(200).json({status:"ok", code:200})
|
||||
})
|
||||
|
||||
app.get('/api/download/:hash', (req, res) => {
|
||||
if(handler == undefined) {
|
||||
var handler = new UploadObject(db);
|
||||
}
|
||||
handler.load(req.params.hash);
|
||||
if(handler.isLoaded()){
|
||||
res.status(200).json({result: {
|
||||
files: handler.getFiles()
|
||||
}, code:200})
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/upload', (req, res) => {
|
||||
res.sendFile(__dirname + "/forms/upload.html");
|
||||
})
|
||||
|
||||
app.get('/download', (req, res) => {
|
||||
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");
|
||||
})
|
||||
// app.get('/download/:hash', (req, res) => {
|
||||
// if(handler == undefined) {
|
||||
// var handler = new UploadObject(db);
|
||||
// }
|
||||
// console.log(handler)
|
||||
// handler.load(req.params.hash)
|
||||
// if(handler.isLoaded()) {
|
||||
// res.send("Found");
|
||||
// } else {
|
||||
// notFound();
|
||||
// }
|
||||
|
||||
|
||||
// })
|
||||
// app.get('/download/:hash/:adminhash', (req, res) => {
|
||||
// handler.load(req.params.hash, req.params.adminhash)
|
||||
// res.send("sdadsd");
|
||||
// })
|
||||
|
||||
|
||||
// Start up Socket.IO:
|
||||
// var io = new socketio.Server(app.listen(port));
|
||||
io.sockets.on("connection", function(socket){
|
||||
var handler = new UploadObject(db);
|
||||
console.log(handler);
|
||||
if(socket.handshake.url.startsWith("/socket.io")) {
|
||||
|
||||
var uploader = new SocketIOFileUpload();
|
||||
uploader.dir = "uploads";
|
||||
uploader.dir = "tmp";
|
||||
|
||||
uploader.uploadValidator = function(event, callback){
|
||||
// asynchronous operations allowed here; when done,
|
||||
console.log(event);
|
||||
if (false) {
|
||||
if (true) {
|
||||
callback(true);
|
||||
} else {
|
||||
callback(false);
|
||||
}
|
||||
};
|
||||
|
||||
uploader.listen(socket);
|
||||
uploader.on("start", function(event){
|
||||
// console.log(handler);
|
||||
handler.new();
|
||||
});
|
||||
|
||||
|
||||
// Do something when a file is saved:
|
||||
uploader.on("saved", function(event){
|
||||
if (event.file.success) {
|
||||
fs.renameSync(event.file.pathName, "uploads/"+handler.getDir()+"/"+event.file.name)
|
||||
handler.registerFile(event.file.name);
|
||||
}
|
||||
console.log(event.file);
|
||||
|
||||
});
|
||||
uploader.on("complete", function(event){
|
||||
socket.emit("linkCreated", handler.getHash(), handler.getAdminHash());
|
||||
});
|
||||
// Error handler:
|
||||
uploader.on("error", function(event){
|
||||
@@ -73,10 +112,8 @@ io.sockets.on("connection", function(socket){
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
function notFound(req, res) {
|
||||
res.status(404);
|
||||
console.log(req.query);
|
||||
ans = { error: 'Not found', code: 404 }
|
||||
if (req.query.format == "xml"){
|
||||
res.set('Content-Type', 'text/xml');
|
||||
@@ -101,8 +138,14 @@ app.use(function (req, res, next) {
|
||||
|
||||
// default to plain-text. send()
|
||||
res.send('404');
|
||||
}
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
notFound(req,res)
|
||||
});
|
||||
|
||||
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log('listening on *:8000');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user