2018-05-31 07:15:41 -04:00
|
|
|
'use strict'
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
|
|
|
|
|
|
|
const azure = require('azure-storage')
|
|
|
|
|
|
|
|
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-05-31 07:15:41 -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-05-31 07:15:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const azureBlobService = azure.createBlobService(config.azure.connectionString)
|
2018-05-31 07:15:41 -04:00
|
|
|
|
|
|
|
azureBlobService.createContainerIfNotExists(config.azure.container, { publicAccessLevel: 'blob' }, function (err, result, response) {
|
|
|
|
if (err) {
|
|
|
|
callback(new Error(err.message), null)
|
|
|
|
} else {
|
|
|
|
azureBlobService.createBlockBlobFromLocalFile(config.azure.container, path.basename(imagePath), imagePath, function (err, result, response) {
|
|
|
|
if (err) {
|
|
|
|
callback(new Error(err.message), null)
|
|
|
|
} else {
|
|
|
|
callback(null, azureBlobService.getUrl(config.azure.container, result.name))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|