blob: ed0e11829b76231730b3f2c08c261b3af575cba1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
'use strict'
const config = require('../../config')
const logger = require('../../logger')
const fs = require('fs')
const fetch = require('node-fetch')
exports.uploadImage = function (imagePath, callback) {
if (!callback || typeof callback !== 'function') {
logger.error('Callback has to be a function')
return
}
if (!imagePath || typeof imagePath !== 'string') {
callback(new Error('Image path is missing or wrong'), null)
return
}
// 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((err) => {
callback(new Error(err), null)
})
}
|