summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPhilip Molares2021-04-19 12:31:14 +0200
committerPhilip Molares2021-04-22 21:23:27 +0200
commitf34d927e8cec45cf65ccee3197b46f4482b2b273 (patch)
treeb4f9f164c3e856893da7f36b10c4f2d7fc16f810 /lib
parentda811aca097f5ed2dc0020dc9bee87aadec9db91 (diff)
ImageRouterImgur: Replace imgur library with note-fetch request
This kinda is a backport of https://github.com/hedgedoc/hedgedoc/pull/961 Signed-off-by: Philip Molares <philip.molares@udo.edu>
Diffstat (limited to '')
-rw-r--r--lib/web/imageRouter/imgur.js30
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/web/imageRouter/imgur.js b/lib/web/imageRouter/imgur.js
index dcb03a7a..ed0e1182 100644
--- a/lib/web/imageRouter/imgur.js
+++ b/lib/web/imageRouter/imgur.js
@@ -1,8 +1,8 @@
'use strict'
const config = require('../../config')
const logger = require('../../logger')
-
-const imgur = require('imgur')
+const fs = require('fs')
+const fetch = require('node-fetch')
exports.uploadImage = function (imagePath, callback) {
if (!callback || typeof callback !== 'function') {
@@ -15,12 +15,30 @@ exports.uploadImage = function (imagePath, callback) {
return
}
- imgur.setClientId(config.imgur.clientID)
- imgur.uploadFile(imagePath)
- .then(function (json) {
+ // The following client ID is for use with HedgeDoc only
+ const clientId = config.imgur.clientID || '032aa2f687790cd'
+
+ const buffer = fs.readFileSync(imagePath)
+
+ const params = new URLSearchParams()
+ params.append('image', buffer.toString('base64'))
+ params.append('type', 'base64')
+ fetch('https://api.imgur.com/3/image', {
+ method: 'POST',
+ body: params,
+ headers: { Authorization: `Client-ID ${clientId}` }
+ })
+ .then((res) => {
+ if (!res.ok) {
+ callback(new Error(res.statusText), null)
+ return
+ }
+ return res.json()
+ })
+ .then((json) => {
logger.debug(`SERVER uploadimage success: ${JSON.stringify(json)}`)
callback(null, json.data.link.replace(/^http:\/\//i, 'https://'))
- }).catch(function (err) {
+ }).catch((err) => {
callback(new Error(err), null)
})
}