Raccoon e97c2821fa
ts: lib/imageRouter/imgur.js
Signed-off-by: Raccoon <raccoon@hackmd.io>
2021-06-16 18:35:21 +08:00

29 lines
786 B
TypeScript

'use strict'
import * as imgur from "@hackmd/imgur";
import * as config from "../config";
import * as logger from "../logger";
export function uploadImage(imagePath, callback) {
if (!imagePath || typeof imagePath !== 'string') {
callback(new Error('Image path is missing or wrong'), null)
return
}
if (!callback || typeof callback !== 'function') {
logger.error('Callback has to be a function')
return
}
imgur.setClientId(config.imgur.clientID)
imgur.uploadFile(imagePath)
.then(function (json) {
if (config.debug) {
logger.info('SERVER uploadimage success: ' + JSON.stringify(json))
}
callback(null, json.data.link.replace(/^http:\/\//i, 'https://'))
}).catch(function (err) {
callback(new Error(err), null)
})
}