Created
October 24, 2018 17:49
-
-
Save mStirner/2dd58d30d5a99dbbb72ce1bcddb03553 to your computer and use it in GitHub Desktop.
Express/Multer http upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require("os"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const express = require('express'); | |
const multer = require('multer'); | |
const bodyParser = require('body-parser'); | |
// build/normalize paths | |
const src = path.resolve(os.tmpdir(), "uploads"); | |
const dest = path.resolve(__dirname, "videos"); | |
// create app/upload middleware | |
const app = express(); | |
const upload = multer({ | |
dest: src | |
}); | |
/** | |
* List/return current video files | |
*/ | |
app.get("/", function(req, res){ | |
fs.readdir(dest, function(err, files){ | |
if(err){ | |
console.log("ERROR >>", err); | |
res.end("error: " + err); | |
}else{ | |
// send back to client | |
res.json(files); | |
} | |
}); | |
}); | |
/** | |
* Upload files to "videos" folder | |
*/ | |
app.put('/', upload.array('movies'), function (req, res) { | |
// req.files is array of `photos` files | |
// req.body will contain the text fields, if there were any | |
let counter = req.files.length; | |
// check if upload complete | |
// all shit is done, files moved, etc... | |
const done = function done() { | |
if (counter === 0) { | |
// feedback | |
console.log("All files uploaded!"); | |
res.end("complete"); | |
} | |
}; | |
if (req.files.length > 0) { | |
req.files.forEach(function (file) { | |
let target = path.resolve(dest, file.originalname); | |
// feedback | |
console.log("move %s to %s", file.path, target); | |
// move from temp to video destination | |
fs.rename(file.path, target, function (err) { | |
if (err) { | |
// feedback | |
console.log("Could not move %s to destination", file.path); | |
res.write("Error: " + file.originalname); | |
} else { | |
// remove from temp location | |
fs.unlink(file.path, function (err) { | |
// check if upload complete | |
// ignore errors, the OS does this for us | |
counter--; | |
done(); | |
}); | |
} | |
}); | |
}); | |
} | |
}); | |
/** | |
* Remove files from "videos" folder | |
*/ | |
app.delete("/", bodyParser.json(), function (req, res) { | |
if (req.body) { | |
// feedback | |
console.log("Delete files", req.body); | |
let counter = req.body.length; | |
// check if upload complete | |
// all shit is done, files moved, etc... | |
const done = function done() { | |
if (counter === 0) { | |
// feedback | |
console.log("All files uploaded!"); | |
res.end("complete"); | |
} | |
}; | |
// remove each file from /videos | |
// feedback to clien/server | |
req.body.forEach(function (file) { | |
fs.unlink(path.resolve(dest, file), function (err) { | |
if(err && err.code === "ENOENT"){ | |
counter--; | |
return done(); | |
} | |
if (err) { | |
// feedback | |
console.log("Error", err); | |
res.end(err); | |
} else { | |
// feedback | |
res.write(file + "\n"); | |
// decrement counter | |
// check if complete | |
counter--; | |
done(); | |
} | |
}); | |
}); | |
} | |
}); | |
// fire server up | |
// feedback | |
app.listen(3020, "0.0.0.0", function(){ | |
let addr = this.address(); | |
console.log("Server running on http://%s:%s/", addr.address, addr.port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment