This commit is contained in:
MishaBagger
2023-11-08 13:41:29 +03:00
parent 797df1e4fa
commit 0956275ec4
24 changed files with 4184 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
const fs = require("fs");
const path = require("path");
const directoryPath = path.join(__dirname, "..", "uploads");
const displayImages = (req, res) => {
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
const imageUrls = files.map((file) => `/uploads/${file}`);
res.status(200).json(imageUrls);
});
};
module.exports = { displayImages };

View File

@@ -0,0 +1,29 @@
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
return cb(null, "./uploads");
},
filename: function (req, file, cb) {
return cb(null, `${Date.now()}-${file.originalname}`);
},
});
const upload = multer({ storage: storage });
const uploadImage = (req, res, next) => {
if (!req.file)
return res.status(400).send(`No file Uploaded <br>
<button onclick="window.location.href='/images'">Show Images</button>
<button onclick="window.location.href='/'">Back to Home</button>
`);
console.log(req.file);
res.send(
`Uploaded <br>
<button onclick="window.location.href='/images'">Show Images</button>
<button onclick="window.location.href='/'">Back to Home</button>
`
);
};
module.exports = { upload, uploadImage };