Created
August 19, 2019 13:32
-
-
Save buroz/a02cfadbd662d345d6a1b0c2f58118be to your computer and use it in GitHub Desktop.
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 { Readable } = require("stream"); | |
const { promisify } = require("util"); | |
const express = require("express"); | |
const mongodb = require("mongodb"); | |
const multer = require("multer"); | |
const ObjectID = require("mongodb").ObjectID; | |
const app = express(); | |
const connect = promisify(mongodb.MongoClient.connect); | |
const db = (async () => { | |
const connection = await connect( | |
"mongodb://localhost:27017", | |
{ | |
useUnifiedTopology: true, | |
useNewUrlParser: true | |
} | |
); | |
const db = connection.db("tracksDB"); | |
const trackRoute = express.Router(); | |
trackRoute.get("/:trackID", (req, res) => { | |
try { | |
var trackID = new ObjectID(req.params.trackID); | |
} catch (err) { | |
return res.status(400).json({ | |
message: "Invalid trackID" // Must be a single String of 12 bytes or a string of 24 hex characters" | |
}); | |
} | |
res.set("content-type", "audio/mp3"); | |
res.set("accept-ranges", "bytes"); | |
let bucket = new mongodb.GridFSBucket(db, { | |
bucketName: "tracks" | |
}); | |
let downloadStream = bucket.openDownloadStream(trackID); | |
downloadStream.on("data", chunk => { | |
res.write(chunk); | |
}); | |
downloadStream.on("error", () => { | |
res.sendStatus(404); | |
}); | |
downloadStream.on("end", () => { | |
res.end(); | |
}); | |
}); | |
trackRoute.post("/", (req, res) => { | |
const storage = multer.memoryStorage(); | |
const upload = multer({ | |
storage: storage, | |
limits: { fields: 1, fileSize: 600000000, files: 1, parts: 2 } | |
}); | |
upload.single("track")(req, res, err => { | |
if (err) { | |
console.log(err); | |
return res | |
.status(400) | |
.json({ message: "Upload Request Validation Failed" }); | |
} else if (!req.body.name) { | |
return res | |
.status(400) | |
.json({ message: "No track name in request body" }); | |
} | |
let trackName = req.body.name; | |
// Covert buffer to Readable Stream | |
const readableTrackStream = new Readable(); | |
readableTrackStream.push(req.file.buffer); | |
readableTrackStream.push(null); | |
let bucket = new mongodb.GridFSBucket(db, { | |
bucketName: "tracks" | |
}); | |
let uploadStream = bucket.openUploadStream(trackName); | |
let id = uploadStream.id; | |
readableTrackStream.pipe(uploadStream); | |
uploadStream.on("error", () => { | |
return res.status(500).json({ message: "Error uploading file" }); | |
}); | |
uploadStream.on("finish", () => { | |
return res.status(201).json({ | |
message: | |
"File uploaded successfully, stored under Mongo ObjectID: " + id | |
}); | |
}); | |
}); | |
}); | |
app.use("/tracks", trackRoute); | |
app.listen(3005, () => { | |
console.log("App listening on port 3005!"); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment