All files index.js

86.71% Statements 124/143
72.5% Branches 58/80
100% Functions 23/23
86.62% Lines 123/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 2633x 3x 3x 3x 3x   3x 3x   3x   3x                 3x 2x     3x 12x 1x 1x 1x     11x 11x 66x 10x       11x 1x 1x 1x     10x     3x 3x 3x                   3x 1x 1x 1x 1x 1x     1x           1x     1x   1x         3x 1x 1x 1x 1x 1x     1x 1x           1x         1x     3x 1x 1x     1x   1x       3x   3x 3x   1x 1x 1x     2x 2x 2x     2x 2x     2x                 2x       3x   2x 2x   1x 1x 1x 1x     1x 1x     1x 1x     1x                 1x       3x 1x         1x 1x     1x   1x     3x 12x 2x 2x     10x 2x 2x 2x     2x 1x 1x   1x     8x 1x 1x 1x 1x 1x       1x 1x 1x 1x   1x 1x     1x 1x       1x     7x 1x 6x 3x 3x 2x 1x 1x       3x         12x 12x        
require('dotenv').config()
const formidable = require('formidable')
const uuidv1 = require('uuid/v1')
const config = require('config')
const { PassThrough } = require('stream')
 
const minioClient = require('./minio-client.js')
const utils = require('./utils')
 
const logger = (config && config.logger) || console
 
const Ops = Object.freeze({
  post: 1,
  list: 2,
  get: 3,
  getStream: 4,
  delete: 5,
  postStream: 6
})
 
const extractFileExtension = filename => {
  return filename.split('.').pop()
}
 
const validityCheck = (req, options) => {
  if (!options) {
    const error = 'Options not provided'
    req.minio = { error }
    return false
  }
 
  let supported = false
  Object.keys(Ops).forEach(key => {
    if (Ops[key] === options.op) {
      supported = true
    }
  })
 
  if (!supported) {
    const error = 'Operation not supported'
    req.minio = { error }
    return false
  }
 
  return true
}
 
const getFileMetaData = stat => {
  Eif (stat && stat.metaData) {
    return {
      filename: Buffer.from(stat.metaData['file-name'], 'base64').toString(
        'utf8'
      ),
      contentType: stat.metaData['content-type']
    }
  }
  return {}
}
 
const handlePost = (req, next, fields, files) => {
  let filename = uuidv1()
  Eif (files.file && files.file.name) {
    const extension = extractFileExtension(files.file.name)
    Eif (extension) {
      filename += `.${extension}`
    }
  }
  minioClient.uploadFile(
    filename,
    (files.file && files.file.name) || '',
    (files.file && files.file.type) || '',
    (files.file && files.file.path) || '',
    (error, etag) => {
      Iif (error) {
        req.minio = { error }
      } else {
        req.minio = { post: { filename: `${filename}`, etag } }
      }
      next()
    }
  )
}
 
const handlePostStream = async (req, next, files, fileStream) => {
  let filename = uuidv1()
  Eif (files.file && files.file.name) {
    const extension = extractFileExtension(files.file.name)
    Eif (extension) {
      filename += `.${extension}`
    }
  }
  try {
    const etag = await minioClient.uploadFileSteam(
      filename,
      (files.file && files.file.name) || '',
      (files.file && files.file.type) || '',
      fileStream
    )
    req.minio = { post: { filename: `${filename}`, etag } }
  } catch (error) {
    console.log('error: ', error)
    req.minio = { error }
  }
  next()
}
 
const handleList = (req, next) => {
  minioClient.listFiles((error, list) => {
    Iif (error) {
      req.minio = { error }
    } else {
      req.minio = { list }
    }
    next()
  })
}
 
const handleGet = async (req, next) => {
  let stat
  try {
    stat = await minioClient.getFileStat(req.params.filename)
  } catch (error) {
    req.minio = { error }
    next()
    return
  }
 
  const tmpFile = utils.getTempPath(req.params.filename)
  minioClient.getFile(req.params.filename, tmpFile, error => {
    Iif (error) {
      req.minio = { error }
    } else {
      let { filename, contentType } = getFileMetaData(stat)
      Iif (!filename) {
        filename = req.params.filename
      }
      req.minio = {
        get: {
          path: tmpFile,
          originalName: filename,
          contentType,
          contentLength: stat.size
        }
      }
    }
    next()
  })
}
 
const handleGetStream = async (req, next) => {
  let stat
  try {
    stat = await minioClient.getFileStat(req.params.filename)
  } catch (error) {
    logger.error('minio handleGetSteam error: ', error)
    req.minio = { error }
    next()
    return
  }
 
  minioClient.getFileStream(req.params.filename, (error, stream) => {
    Iif (error) {
      req.minio = { error }
    } else {
      let { filename, contentType } = getFileMetaData(stat)
      Iif (!filename) {
        filename = req.params.filename
      }
      req.minio = {
        get: {
          stream,
          originalName: filename,
          contentLength: stream.headers['content-length'],
          contentType
        }
      }
    }
    next()
  })
}
 
const handleDelete = async (req, next) => {
  Iif (!req.params.filename || req.params.filename === 'undefined') {
    req.minio = { error: 'File name not specified' }
    next()
    return
  }
  const error = await minioClient.deleteFile(req.params.filename)
  Iif (error) {
    req.minio = { error }
  } else {
    req.minio = { delete: 'Success' }
  }
  next()
}
 
const handleRequests = (req, next, options) => {
  if (!validityCheck(req, options)) {
    next()
    return
  }
 
  if (options.op === Ops.post) {
    const form = new formidable.IncomingForm()
    form.parse(req, (err, fields, files) => {
      Iif (err) {
        req.minio = { error: err }
        next()
      } else if (!files.file) {
        req.minio = { error: 'No file attached to post' }
        next()
      } else {
        handlePost(req, next, fields, files)
      }
    })
  } else if (options.op === Ops.postStream) {
    const form = new formidable.IncomingForm()
    const pass = new PassThrough()
    const files = { file: {} }
    form.onPart = part => {
      Iif (!part.filename) {
        form.handlePart(part)
        return
      }
      files.file.name = part.filename
      files.file.type = part.mime
      part.on('data', function (buffer) {
        pass.write(buffer)
      })
      part.on('end', function () {
        pass.end()
      })
    }
    form.parse(req, err => {
      Iif (err) {
        req.minio = { error: err }
        next()
      } else {
        handlePostStream(req, next, files, pass)
      }
    })
  } else if (options.op === Ops.list) {
    handleList(req, next)
  } else if (options.op === Ops.get) {
    handleGet(req, next)
  } else if (options.op === Ops.getStream) {
    handleGetStream(req, next)
  } else Eif (options.op === Ops.delete) {
    handleDelete(req, next)
  }
}
 
module.exports = {
  Ops,
  utils,
  minioClient,
  middleware () {
    return options => (req, res, next) => {
      handleRequests(req, next, options)
    }
  }
}