-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.js
94 lines (85 loc) · 2.56 KB
/
media.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const path = require("path");
const fs = require("fs");
const db = require("../db/roomdb.js");
const {Message} = require("../conf/mongo_conf");
const image_formats = [".png", ".jpg", ".gif"];
const crypto = require("crypto");
// Based on https://stackoverflow.com/a/15773267
// Expects multi-part post body, with the file
// contained in the "file" field and sender information
// contained in "source"
exports.uploadMedia = (req, res) => {
const data = {
sender: req.body.sender,
room_id: req.body.room_id,
};
const temp_path = req.file.path;
const storage_name = randomFileName(
path.extname(req.file.originalname).toLowerCase()
);
const target_path = path.join(__dirname, "../public/uploads/" + storage_name);
fs.rename(temp_path, target_path, (err) => {
if (err) buildResponse(res, err);
let url = `/media/${storage_name}::${path.basename(req.file.originalname)}`;
buildResponse(res, undefined, url);
});
};
exports.getMedia = (req, res) => {
let file_name = req.params.file_name;
if (image_formats.includes(path.extname(file_name).toLowerCase())) {
return res.sendFile(path.resolve("./public/uploads/" + file_name));
} else {
getFileName("/media/" + file_name).then((name) => {
return res.download(path.resolve("./public/uploads/" + file_name), name);
});
}
};
const getFileName = async (url) => {
let name = undefined;
await Message.findOne(
{
is_file: true,
content: {
$regex: `^${url}`,
},
},
(err, message) => {
if (err) {
console.error(err);
return;
}
if (message) {
name = message.content.split("::")[1];
}
}
);
return name;
};
const buildResponse = (res, err, fname) => {
let response;
if (err) {
response = {
timestamp: new Date().toISOString(),
status: 500,
path: "/media",
error: err.message,
};
} else {
response = {
timestamp: new Date().toISOString(),
status: 200,
path: fname,
};
}
res.json(response);
};
const fid_magnitude = 10;
const randomFileName = (ext) => {
let fname;
do {
let id = crypto.randomBytes(10).toString("hex");
fname =
('0'.repeat(fid_magnitude) + id).substr(-fid_magnitude) + fid_magnitude;
} while (fs.existsSync('/public/uploads/' + fname));
return fname + ext;
}