2018-03-17 21:14:50 -04:00
|
|
|
'use strict'
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
|
|
|
|
const imgur = require('imgur')
|
|
|
|
|
|
|
|
exports.uploadImage = function (imagePath, callback) {
|
2019-06-08 14:51:24 -04:00
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
logger.error('Callback has to be a function')
|
2018-03-17 21:14:50 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:51:24 -04:00
|
|
|
if (!imagePath || typeof imagePath !== 'string') {
|
|
|
|
callback(new Error('Image path is missing or wrong'), null)
|
2018-03-17 21:14:50 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
imgur.setClientId(config.imgur.clientID)
|
|
|
|
imgur.uploadFile(imagePath)
|
2019-05-30 18:27:56 -04:00
|
|
|
.then(function (json) {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`SERVER uploadimage success: ${JSON.stringify(json)}`)
|
2019-05-30 18:27:56 -04:00
|
|
|
callback(null, json.data.link.replace(/^http:\/\//i, 'https://'))
|
|
|
|
}).catch(function (err) {
|
|
|
|
callback(new Error(err), null)
|
|
|
|
})
|
2018-03-17 21:14:50 -04:00
|
|
|
}
|