-
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.
1. Utils : Imgur
- Loading branch information
Showing
3 changed files
with
65 additions
and
80 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
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 |
---|---|---|
@@ -1,5 +1,30 @@ | ||
function imgurUpload(file) { | ||
const imgurURL = process.env.IMGUR_URL; | ||
const imgurClientId = process.env.IMGUR_CLIENT_ID; | ||
|
||
async function imgurUpload(file) { | ||
// Format to formData | ||
const formData = new FormData(); | ||
|
||
// Format to Blob object | ||
const fileBlob = new Blob([file.buffer], { type: file.mimetype }); | ||
formData.append('image', fileBlob, { | ||
filename: file.originalname | ||
}); | ||
|
||
const imgurResponse = await fetch(`${imgurURL}`, { | ||
method: `POST`, | ||
headers: { | ||
"Authorization": `Client-ID ${imgurClientId}`, | ||
}, | ||
body: formData | ||
}); | ||
if (!imgurResponse.ok) { | ||
// Returns Error Message | ||
throw `Imgur Upload Error : ${imgurResponse.statusText}` | ||
} | ||
// Retrieve Imgur Link | ||
const imgur = await imgurResponse.json(); | ||
return imgur.data.link; | ||
} | ||
|
||
module.exports = imgurUpload; |