forked from hiteshchoudhary/chai-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add multer and cloudinary files for file upload
- Loading branch information
1 parent
ac62010
commit 5f45529
Showing
5 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import multer from "multer"; | ||
|
||
const storage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
cb(null, "./public/temp") | ||
}, | ||
filename: function (req, file, cb) { | ||
|
||
cb(null, file.originalname) | ||
} | ||
}) | ||
|
||
export const upload = multer({ | ||
storage, | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {v2 as cloudinary} from "cloudinary" | ||
import fs from "fs" | ||
|
||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUDINARY_CLOUD_NAME, | ||
api_key: process.env.CLOUDINARY_API_KEY, | ||
api_secret: process.env.CLOUDINARY_API_SECRET | ||
}); | ||
|
||
const uploadOnCloudinary = async (localFilePath) => { | ||
try { | ||
if (!localFilePath) return null | ||
//upload the file on cloudinary | ||
const response = await cloudinary.uploader.upload(localFilePath, { | ||
resource_type: "auto" | ||
}) | ||
// file has been uploaded successfull | ||
console.log("file is uploaded on cloudinary ", response.url); | ||
return response; | ||
|
||
} catch (error) { | ||
fs.unlinkSync(localFilePath) // remove the locally saved temporary file as the upload operation got failed | ||
return null; | ||
} | ||
} | ||
|
||
|
||
|
||
export {uploadOnCloudinary} |