2020-04-12 18:41:54 +00:00
|
|
|
import imgur from 'imgur'
|
|
|
|
|
|
|
|
import { config } from '../../config'
|
|
|
|
import { logger } from '../../logger'
|
|
|
|
import { UploadProvider } from './index'
|
|
|
|
|
|
|
|
const ImgurUploadProvider: UploadProvider = {
|
|
|
|
uploadImage: (imagePath, callback) => {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
logger.error('Callback has to be a function')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!imagePath) {
|
|
|
|
callback(new Error('Image path is missing or wrong'), undefined)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
imgur.setClientId(config.imgur.clientID)
|
|
|
|
imgur.uploadFile(imagePath)
|
|
|
|
.then(function (json) {
|
|
|
|
logger.debug(`SERVER uploadimage success: ${JSON.stringify(json)}`)
|
2020-04-12 18:43:38 +00:00
|
|
|
callback(undefined, json.data.link.replace(/^http:\/\//i, 'https://'))
|
2020-04-12 18:41:54 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
callback(new Error(err), undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { ImgurUploadProvider }
|