mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 03:06:31 -05:00
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>
This commit is contained in:
parent
da811aca09
commit
f34d927e8c
1 changed files with 24 additions and 6 deletions
|
@ -1,8 +1,8 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
const config = require('../../config')
|
const config = require('../../config')
|
||||||
const logger = require('../../logger')
|
const logger = require('../../logger')
|
||||||
|
const fs = require('fs')
|
||||||
const imgur = require('imgur')
|
const fetch = require('node-fetch')
|
||||||
|
|
||||||
exports.uploadImage = function (imagePath, callback) {
|
exports.uploadImage = function (imagePath, callback) {
|
||||||
if (!callback || typeof callback !== 'function') {
|
if (!callback || typeof callback !== 'function') {
|
||||||
|
@ -15,12 +15,30 @@ exports.uploadImage = function (imagePath, callback) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
imgur.setClientId(config.imgur.clientID)
|
// The following client ID is for use with HedgeDoc only
|
||||||
imgur.uploadFile(imagePath)
|
const clientId = config.imgur.clientID || '032aa2f687790cd'
|
||||||
.then(function (json) {
|
|
||||||
|
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)}`)
|
logger.debug(`SERVER uploadimage success: ${JSON.stringify(json)}`)
|
||||||
callback(null, json.data.link.replace(/^http:\/\//i, 'https://'))
|
callback(null, json.data.link.replace(/^http:\/\//i, 'https://'))
|
||||||
}).catch(function (err) {
|
}).catch((err) => {
|
||||||
callback(new Error(err), null)
|
callback(new Error(err), null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue