Skip to content

Commit

Permalink
feat: add post image endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ditramadia committed Nov 15, 2023
1 parent c26a887 commit 8da65f3
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 2 deletions.
175 changes: 174 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"googleapis": "^126.0.1",
"jsonwebtoken": "^9.0.1"
"jsonwebtoken": "^9.0.1",
"multer": "^1.4.5-lts.1",
"shortid": "^2.2.16"
},
"devDependencies": {
"@types/bcrypt": "^5.0.1",
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.2",
"@types/multer": "^1.4.10",
"@types/node": "^20.5.3",
"@types/shortid": "^0.0.32",
"nodemon": "^3.0.1",
"prisma": "^5.2.0",
"ts-node": "^10.9.1",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/images/messageImage_1699968506525.jpg
Binary file not shown.
26 changes: 26 additions & 0 deletions src/routes/image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import express from 'express';
import path from 'path';
import multer from 'multer';
import shortid from 'shortid';
import accessValidation from '../middleware/accessValidation';

const router = express.Router();

const storage = multer.diskStorage({
destination: function (req, file, cb) {
const destinationPath = path.join(__dirname, '../images');
cb(null, destinationPath);
},
filename: function (req, file, cb) {
const uniqueFilename = `${shortid.generate()}-${file.originalname}`
cb(null, uniqueFilename);
}
});
const upload = multer({ storage });

// Get image
router.get('/', (req, res) => {
const { filename } = req.query;

Expand All @@ -20,4 +36,14 @@ router.get('/', (req, res) => {
});
});

// Post image
router.post('/upload', accessValidation, upload.single('image'), (req, res) => {
console.log("here:");
console.log(req.file);

res.json({
message: 'File uploaded successfully'
});
});

export default router;

0 comments on commit 8da65f3

Please sign in to comment.