upd
This commit is contained in:
@@ -4,6 +4,7 @@ import express, { json } from "express";
|
||||
import cors from "cors";
|
||||
import mailRoute from "./routes/mail";
|
||||
import projectRoute from "./routes/projects";
|
||||
import uploadRoute from "./routes/upload";
|
||||
|
||||
connectDB();
|
||||
|
||||
@@ -15,6 +16,7 @@ app.use(cors({ origin: "*" }));
|
||||
|
||||
app.use("/mail", mailRoute);
|
||||
app.use("/projects", projectRoute);
|
||||
app.use("/upload", uploadRoute);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is listening on port ${port}`);
|
||||
|
||||
+56
-55
@@ -1,77 +1,78 @@
|
||||
import { Router } from "express";
|
||||
// import multer, { memoryStorage } from "multer";
|
||||
// import sharp from "sharp";
|
||||
// import fs from "fs";
|
||||
import multer, { memoryStorage } from "multer";
|
||||
import sharp from "sharp";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
// import { v4 as uuidv4 } from "uuid";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// const upload = multer({
|
||||
// dest: "uploads/",
|
||||
// storage: memoryStorage(),
|
||||
// fileFilter: (_req, file, cb) => {
|
||||
// if (file.mimetype.split("/")[0] === "image") {
|
||||
// cb(null, true);
|
||||
// } else {
|
||||
// cb(new Error("Only images are allowed!"));
|
||||
// }
|
||||
// },
|
||||
// });
|
||||
const upload = multer({
|
||||
dest: "uploads/",
|
||||
storage: memoryStorage(),
|
||||
fileFilter: (_req, file, cb) => {
|
||||
if (file.mimetype.split("/")[0] === "image") {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error("Only images are allowed!"));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
router.get("/", (_req, res) => {
|
||||
res.json({ ok: 1 });
|
||||
});
|
||||
|
||||
// router.get("/:filename", (req, res) => {
|
||||
// res.sendFile(`${path.resolve("uploads")}/${req.params.filename}`);
|
||||
// });
|
||||
router.get("/:filename", (req, res) => {
|
||||
res.sendFile(`${path.resolve("uploads")}/${req.params.filename}`);
|
||||
});
|
||||
|
||||
// router.get("/:file", (req, res) => {
|
||||
// const fileName = req.params.file;
|
||||
router.get("/:file", (req, res) => {
|
||||
const fileName = req.params.file;
|
||||
|
||||
// console.log(fileName);
|
||||
console.log(fileName);
|
||||
|
||||
// try {
|
||||
// const readStream = fs.createReadStream(
|
||||
// `${path.join(__dirname, "uploads")}/${fileName}`
|
||||
// );
|
||||
try {
|
||||
const readStream = fs.createReadStream(
|
||||
`${path.join(__dirname, "uploads")}/${fileName}`
|
||||
);
|
||||
|
||||
// console.log(readStream);
|
||||
console.log(readStream);
|
||||
|
||||
// readStream.pipe(res);
|
||||
// } catch (error) {
|
||||
// if (error instanceof Error) {
|
||||
// return res.json({ error: error.message });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
readStream.pipe(res);
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return res.json({ error: error.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// router.post("/", upload.single("file"), async (req, res) => {
|
||||
// if (!req.file) {
|
||||
// return res.json({ error: "req.file" });
|
||||
// }
|
||||
router.post("/", upload.single("file"), async (req, res) => {
|
||||
if (!req.file) {
|
||||
return res.json({ error: "req.file" });
|
||||
}
|
||||
|
||||
// try {
|
||||
// const filename = `${uuidv4()}.jpg`;
|
||||
try {
|
||||
const filename = `${uuidv4()}.jpg`;
|
||||
|
||||
// await sharp(req.file.buffer)
|
||||
// .resize({
|
||||
// width: 728,
|
||||
// height: 728,
|
||||
// fit: "inside",
|
||||
// withoutEnlargement: true,
|
||||
// })
|
||||
// .jpeg({ quality: 90 })
|
||||
// .toFile(`uploads/${filename}`);
|
||||
await sharp(req.file.buffer)
|
||||
.resize({
|
||||
width: 728,
|
||||
height: 728,
|
||||
fit: "inside",
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: 90 })
|
||||
.toFile(`uploads/${filename}`);
|
||||
|
||||
// return res.json({ file: `/upload/${filename}` });
|
||||
// } catch (error) {
|
||||
// if (error instanceof Error) {
|
||||
// return res.json({ error: error.message });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
return res.json({ file: filename });
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return res.json({ error: error.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const uploadRoute = router;
|
||||
|
||||
export default uploadRoute;
|
||||
|
||||
Reference in New Issue
Block a user