Skip to content
Permalink
40e55693aa
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (42 sloc) 1.33 KB
'use strict'
const fs = require('fs')
const util = require('util')
const mime = require('mime-types')
const copy = util.promisify(fs.copyFile)
const unlink = util.promisify(fs.unlink)
const exists = util.promisify(fs.exists)
const uploadPicture = async(path, mimeType, fileName, newPath) => {
try{
if(path.length === 0) throw new Error('path is blank')
if(mimeType.length === 0) throw new Error('mimeType is blank')
const extension = mime.extension(mimeType)
const acceptedExtension = ['jpg', 'png', 'webp', 'tiff', 'gif', 'svg']
if(acceptedExtension.includes(extension) === false) throw new Error('file type is not suitable for upload')
const newFile = `${newPath}${fileName}.${extension}`
await copy(path, newFile)
return true
} catch(err) {
throw err
}
}
const deletePicture = async(targetFile) => {
try{
if(await exists(`${targetFile}`) === false) throw new Error('image file does not exist')
await unlink(`${targetFile}`)
return true
} catch(err) {
throw err
}
}
const renamePicture = async(targetFile, newName) => {
try{
if(await exists(`${targetFile}`) === false) throw new Error('file does not exist')
const name = String(newName)
await copy(`${targetFile}`, `${name}`)
await unlink(`${targetFile}`)
return true
} catch(err) {
throw err
}
}
module.exports = {uploadPicture, deletePicture, renamePicture}