Last active
January 12, 2024 14:26
-
-
Save Somtuzy/53cc0b7bba5ddd0f9b49b77c34b8ba36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const uploadRouter = require('./upload.route') | |
app.use(uploadRouter) | |
app.listen(3000, () => { | |
console.log('Server is running on port 3000') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cloudinary = require("cloudinary"); | |
cloudinary.config({ | |
cloud_name: process.env.CLOUD_NAME, | |
api_key: process.env.API_KEY, | |
api_secret: process.env.API_SECRET, | |
}); | |
module.exports = async (file) => { | |
return await cloudinary.uploader.upload(file.path); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const multer = require("multer"); | |
const storage = multer.diskStorage({ | |
destination: function (req, file, cb) { | |
cb(null, "./uploads"); | |
}, | |
filename: function (req, file, cb) { | |
cb( | |
null, | |
${file.fieldname}-${Date.now()}${path.extname(file.originalname)} | |
); | |
}, | |
}); | |
function checkFileType(file, cb) { | |
const filetypes = /jpeg|jpg|png|gif/; | |
const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); | |
const mimetype = filetypes.test(file.mimetype); | |
if (extname && mimetype) { | |
return cb(null, true); | |
} else { | |
cb("Error: Images only!"); | |
} | |
} | |
module.exports = multer({ | |
storage: storage, | |
fileFilter: function (req, file, cb) { | |
checkFileType(file, cb); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = async function changeProfilePicture(req, res) { | |
const { id } = req.params | |
const profile_picture = req.body.profile_picture[0] | |
const existingUser = await User.find({_id: id}) | |
if(!existingUser) { | |
//tell them user does not wxist | |
} | |
const updatedUser = await User.update(id, { | |
profile_picture: profile_picture | |
} | |
//return response to the frontend after | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uploadToCloudinary = require( './cloudinary') | |
module.exports = (fieldToAccessUploadedFiles) => async (req, res, next) => { | |
const uploads = [] | |
if (req.files && req.files.length > 0) { | |
for (const file of req.files) { | |
const result = await uploadToCloudinary(file) | |
console.log(result); | |
uploads.push({ ...file, ...result }) | |
} | |
} | |
req.body[fieldToAccessUploadedFiles] = uploads | |
console.log(`File(s) uploaded successfully. Field to access uploaded file(s): req.body.${fieldToAccessUploadedFiles}`); | |
console.log('Uploaded File(s):', req.body[fieldToAccessUploadedFiles]); | |
next() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uploadFiles = require('./upload.middleware') | |
const { upload } = require('./multer') | |
const changeProfilePicture = require('upload.controller') | |
const uploadRouter = Router() | |
uploadRouter.post('/users/:id/upload_profile_picture', [upload.array('profile_picture', 1), uploadFiles('profile_picture')], changeProfilePicture) | |
module.exports = uploadRouter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment