Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from "dotenv";
import { CHUNK_SIZE, BASE_URL } from "./utils/constants.js";
import express from "express";
import multer from "multer";
import { insertToDb, fetchFromDb, getDbClient } from "./mongo.js";
Expand Down Expand Up @@ -31,7 +32,7 @@ app.get("/video/:id", async (req, res) => {
logger.error("id doesn't exist in database");
return res.send(404, "No file found with given id");
}
const CHUNK_SIZE = 5 * 10 ** 6;

let range = req.headers.range;
if (!range) {
range = `0-${CHUNK_SIZE}`;
Expand All @@ -41,9 +42,9 @@ app.get("/video/:id", async (req, res) => {
const videoSize = doc.length;

const start = Number(range.split("-")[0].replace(/\D/g, ""));
console.log("start " + start);

const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
console.log("end " + end);

const contentLength = end - start + 1;

const headers = {
Expand Down Expand Up @@ -74,6 +75,8 @@ app.get("/image/:id", async function (req, res) {
});

app.post("/file/upload", upload.single("file"), async (req, res) => {
const start = Date.now();

const { file } = req;
//make video streammable before uploading in db
const hash = await getFileHash(file, "sha1");
Expand All @@ -91,7 +94,7 @@ app.post("/file/upload", upload.single("file"), async (req, res) => {
});
return res.send(
409,
"File already exists " + "http://localhost:" + port + "/image/" + fileId
"File already exists " + BASE_URL + port + "/image/" + fileId
);
}

Expand All @@ -107,22 +110,21 @@ app.post("/file/upload", upload.single("file"), async (req, res) => {
" bytes"
);
clearUploads("uploads");
const end = Date.now();

logger.info(`Took ${end - start} ms to process upload req`);
if (file.mimetype.startsWith("image/"))
return res.send(
"uploaded file at :" + "http://localhost:" + port + "/image/" + id
);
return res.send("uploaded file at :" + BASE_URL + port + "/image/" + id);
if (file.mimetype.startsWith("video"))
return res.send(
"uploaded file at :" + "http://localhost:" + port + "/video/" + id
);
return res.send("uploaded file at :" + BASE_URL + port + "/video/" + id);
});

const clearUploads = (directory) => {
fs.readdir(directory, (err, files) => {
if (err) throw err;

for (const file of files) {
fs.unlink(path.join(directory, file), (err) => {
fs.unlinkSync(path.join(directory, file), (err) => {
if (err) throw err;
});
}
Expand All @@ -131,6 +133,5 @@ const clearUploads = (directory) => {
};

app.listen(port, function () {
logger.info("Server started");
console.log("Server started on port: " + port);
});
2 changes: 2 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const BASE_URL = "http://localhost:";
export const CHUNK_SIZE = 5 * 10 ** 6;