separated file into upload and download files, changed single quotes to double quotes, added progress indicator
This commit is contained in:
parent
aad54b34b9
commit
c25c97fbaa
6 changed files with 212 additions and 177 deletions
40
app.js
40
app.js
|
@ -1,40 +1,40 @@
|
|||
const express = require('express')
|
||||
var busboy = require('connect-busboy'); //middleware for form/file upload
|
||||
var path = require('path'); //used for file path
|
||||
var fs = require('fs-extra'); //File System - for file manipulation
|
||||
const express = require("express")
|
||||
var busboy = require("connect-busboy"); //middleware for form/file upload
|
||||
var path = require("path"); //used for file path
|
||||
var fs = require("fs-extra"); //File System - for file manipulation
|
||||
const app = express()
|
||||
var redis = require("redis"),
|
||||
client = redis.createClient();
|
||||
|
||||
client.on('error', function(err) {
|
||||
client.on("error", function(err) {
|
||||
console.log(err);
|
||||
})
|
||||
|
||||
app.use(busboy());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.send('Hello World!')
|
||||
app.get("/", function (req, res) {
|
||||
res.send("Hello World!")
|
||||
})
|
||||
|
||||
app.get('/download/:id', function(req, res) {
|
||||
res.sendFile(path.join(__dirname + '/public/download.html'));
|
||||
app.get("/download/:id", function(req, res) {
|
||||
res.sendFile(path.join(__dirname + "/public/download.html"));
|
||||
});
|
||||
|
||||
app.get('/assets/download/:id', function(req, res) {
|
||||
app.get("/assets/download/:id", function(req, res) {
|
||||
|
||||
let id = req.params.id;
|
||||
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
|
||||
if (!reply) {
|
||||
res.sendStatus(404);
|
||||
} else {
|
||||
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
|
||||
res.setHeader('Content-Type', 'application/octet-stream');
|
||||
res.setHeader("Content-Disposition", "attachment; filename=" + reply);
|
||||
res.setHeader("Content-Type", "application/octet-stream");
|
||||
|
||||
res.download(__dirname + '/static/' + id, reply, function(err) {
|
||||
res.download(__dirname + "/static/" + id, reply, function(err) {
|
||||
if (!err) {
|
||||
client.del(id);
|
||||
fs.unlinkSync(__dirname + '/static/' + id);
|
||||
fs.unlinkSync(__dirname + "/static/" + id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -42,18 +42,18 @@ app.get('/assets/download/:id', function(req, res) {
|
|||
|
||||
});
|
||||
|
||||
app.route('/upload/:id')
|
||||
app.route("/upload/:id")
|
||||
.post(function (req, res, next) {
|
||||
|
||||
var fstream;
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on('file', function (fieldname, file, filename) {
|
||||
req.busboy.on("file", function (fieldname, file, filename) {
|
||||
console.log("Uploading: " + filename);
|
||||
|
||||
//Path where image will be uploaded
|
||||
fstream = fs.createWriteStream(__dirname + '/static/' + req.params.id);
|
||||
fstream = fs.createWriteStream(__dirname + "/static/" + req.params.id);
|
||||
file.pipe(fstream);
|
||||
fstream.on('close', function () {
|
||||
fstream.on("close", function () {
|
||||
let id = req.params.id;
|
||||
client.hset(id, "filename", filename, redis.print);
|
||||
client.hset(id, "expiration", 0, redis.print);
|
||||
|
@ -67,6 +67,6 @@ app.route('/upload/:id')
|
|||
|
||||
|
||||
app.listen(3000, function () {
|
||||
console.log('Portal app listening on port 3000!')
|
||||
console.log("Portal app listening on port 3000!")
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue