-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (115 loc) · 3.74 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require("express");
const multer = require("multer");
const cors = require("cors");
const fs = require("fs");
const PORT = 8080;
const app = express();
const { getAudioDurationInSeconds } = require("get-audio-duration");
app.use(cors());
let mapMemory = null;
app.listen(PORT, async () => {
// Create map of existing files already present with basic information for querying
fs.readFile("./storage/memory.json", "utf8", (err, data) => {
if (err) {
console.log(err);
}
mapMemory =
data === "" ? new Map() : new Map(Object.entries(JSON.parse(data)));
console.log("Running...");
});
});
const storage = multer.diskStorage({
// Save file to Storage folder
filename: (req, file, callback) => {
callback(null, file.originalname);
},
destination: (req, file, callback) => {
callback(null, "./storage");
},
});
const fileFilter = (req, file, callback) => {
// Check if file present already/ correct extension
const extenstion = file.originalname.split(".");
callback(
null,
mapMemory.get(file.originalname) === undefined &&
extenstion.length == 2 &&
extenstion[1] === "wav",
);
};
const upload = multer({
fileFilter: fileFilter,
storage: storage,
limits: { fileSize: 3000000 },
}).single("file");
app.post("/post", (req, res) => {
// check if file meets standards/is already present
upload(req, res, (err) => {
if (err || req.file === undefined) {
res
.status(400)
.json({
body: `File already exists, is to large, or is improperly formated`,
});
} else {
getAudioDurationInSeconds(req.file.path)
.then((dur) => {
const currentDay = new Date().toLocaleDateString();
const fileName = req.file.originalname;
mapMemory.set(fileName, {
name: fileName,
duration: dur,
dateAdded: currentDay,
path: req.file.path,
});
fs.writeFile(
"./storage/memory.json",
JSON.stringify(Object.fromEntries(mapMemory)),
(err) => {
res
.status(err ? 500 : 200)
.json({
body: err
? `Error storing data: ${err}`
: `${fileName} succesfully stored`,
});
},
);
})
.catch((err) =>
res.status(500).json({ body: `Error getting duration: ${err}` }),
);
}
});
});
app.get(`/list`, (req, res) => {
// Query map based on duration, return array of names
const maxDur = req.query.maxduration;
if (!isNaN(maxDur) && !isNaN(parseFloat(maxDur))) {
const files = Array.from(mapMemory.values())
.filter((file) => file.duration <= parseFloat(maxDur))
.map((file) => file.name);
const response =
files.length === 0 ? "No files found" : `Found ${files.length} files`;
res.status(200).json({ body: response, data: files });
} else {
res.status(400).json({ body: "Query is not properly formatted", data: [] });
}
});
app.get(`/info`, (req, res) => {
// Query map based on key name (file name), return file object
const fileName = req.query.name;
const noFile = mapMemory.get(fileName) === undefined;
const response = noFile ? `No file info found` : `File info retrieved`;
const fileInfo = noFile ? "" : mapMemory.get(fileName);
res.status(noFile ? 400 : 200).json({ body: response, data: fileInfo });
});
app.get(`/download`, (req, res) => {
// See if file present in map, if so download if not throw error.
const fileName = req.query.name;
if (mapMemory.get(fileName) !== undefined) {
res.status(200).download(`${mapMemory.get(fileName).path}`, fileName);
} else {
res.status(400).json({ body: "File not found" });
}
});