Commit 430611e9 authored by Senk Ju's avatar Senk Ju

await instead of callback functions

parent 08459e97
......@@ -6,13 +6,20 @@ const fileUpload = require("express-fileupload");
const mime = require("mime-types");
const crypto = require("crypto");
const uidGenerator = require("uid-generator");
const util = require("util");
const config = require(path.join(__dirname, "config.json"));
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const readdir = util.promisify(fs.readdir);
const existsFile = util.promisify(fs.exists);
const fileStats = util.promisify(fs.stat);
const app = express();
const uidGen = new uidGenerator(uidGenerator.BASE64, 8);
const imgFolder = path.join(__dirname, "images");
const imageFolder = path.join(__dirname, "images");
var userRateLimits = {};
......@@ -30,17 +37,17 @@ function endRequest(code, message, res) {
res.end(`${code}: ${message}`);
}
function encrypt(buffer, password) {
async function encryptBuffer(buffer, password) {
var cipher = crypto.createCipher(config.encryption.algorithm, password)
var encrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
return encrypted;
}
function decrypt(buffer, password) {
async function decryptFile(filePath, password) {
var fileBuffer = await readFile(filePath);
var decipher = crypto.createDecipher(config.encryption.algorithm, password)
var decrypted = Buffer.concat([decipher.update(buffer), decipher.final()]);
var decrypted = Buffer.concat([decipher.update(fileBuffer), decipher.final()]);
return decrypted;
}
......@@ -48,24 +55,19 @@ setInterval(() => {
userRateLimits = {};
}, 1000 * 60 * config.rateLimit.delay);
app.get("/:filename", (req, res) => {
app.get("/:filename", async (req, res) => {
var filename = req.params.filename;
var fileMime = mime.lookup(filename);
var filePath = path.join(imgFolder, filename);
var filePath = path.join(imageFolder, filename);
var password = req.query.key;
if (!fs.existsSync(filePath) || filename.includes("/") || filename.includes("\\")) {
if (await !existsFile(filePath) || filename.includes("/") || filename.includes("\\")) {
endRequest(404, "File not found!", res);
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
endRequest(500, "Failed to decrypt file!", res);
return;
}
var decryptedFile = decrypt(data, password);
try {
var decryptedFile = await decryptFile(filePath, password);
res.writeHead(200, {
"Content-Type": fileMime,
......@@ -73,10 +75,12 @@ app.get("/:filename", (req, res) => {
});
res.end(decryptedFile);
})
} catch (err) {
endRequest(500, "Failed to decrypt file!", res);
}
});
app.post("/upload", (req, res) => {
app.post("/upload", async (req, res) => {
var file = req.files.file;
var userKey = req.body.userKey;
var password = req.body.password;
......@@ -111,50 +115,40 @@ app.post("/upload", (req, res) => {
}
do {
var filename = uidGen.generateSync() + fileExtension;
var imgFolderFile = path.join(imgFolder, filename);
} while (fs.existsSync(imgFolderFile));
var filename = await uidGen.generate() + fileExtension;
var imageFolderFile = path.join(imageFolder, filename);
} while (await existsFile(imageFolderFile));
var encryptedFile = encrypt(file.data, password);
fs.writeFile(imgFolderFile, encryptedFile, (err) => {
if (err) {
endRequest(500, "Failed to encrypt file.", res);
return;
}
try {
var encryptedFile = await encryptBuffer(file.data, password);
await writeFile(imageFolderFile, encryptedFile);
res.end(`https://axolotl.club/${filename}?key=${password}`);
});
} catch (err) {
endRequest(500, "Failed to encrypt file.", res);
}
});
app.get("/", (req, res) => {
fs.readdir(imgFolder, (err, files) => {
if (err) {
endRequest(500, "Internal server error.", res);
return;
}
app.get("/", async (req, res) => {
var uploadedImages = await readdir(imageFolder);
var fileCount = uploadedImages.length.toLocaleString();
var usersCount = config.userKeys.length.toLocaleString();
var count = files.length.toLocaleString();
var users = config.userKeys.length.toLocaleString();
var size = 0;
files.forEach(file => {
size += fs.statSync(path.join(imgFolder, file)).size;
});
size = (size / 1000000).toFixed(2);
var statistics = {
count: count,
users: users,
size: size
var totalSize = 0;
for (image of uploadedImages) {
var fileStat = await fileStats(path.join(imageFolder, image));
totalSize += fileStat.size;
}
totalSize = (totalSize / 1000000).toFixed(2);
res.render("index", {
statistics: {
count: fileCount,
users: usersCount,
size: totalSize
}
res.render("index", {
statistics: statistics
});
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment