'use strict'
const URL = require('url').URL
const path = require('path')

const config = require('../../config')
const logger = require('../../logger')

exports.uploadImage = function (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
  }

  callback(null, (new URL(path.basename(imagePath), config.serverURL + '/uploads/')).href)
}

'use strict'
const URL = require('url').URL
const path = require('path')

const config = require('../../config')
const logger = require('../../logger')

exports.uploadImage = function (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
  }

  let url
  try {
    url = (new URL(path.basename(imagePath), config.serverURL + '/uploads/')).href
  } catch (e) {
    url = config.serverURL + '/uploads/' + path.basename(imagePath)
  }

  callback(null, url)
}

Fix avoiding crash if URL is invalid
Fix avoiding crash if URL is invalid

Signed-off-by: toshi0123 <661e25bb3f77d7010d4dcf7b0aff8b68fa2b1b9c@users.noreply.github.com>