fix #10 choose upload path in config.js

This commit is contained in:
lesion
2019-06-11 17:44:11 +02:00
parent a557348b21
commit 5778c64108
16 changed files with 99 additions and 95 deletions

View File

@@ -48,7 +48,7 @@ ${event.description.length > 200 ? event.description.substr(0, 200) + '...' : ev
let media
if (event.image_path) {
const file = path.join(__dirname, '..', '..', '..', 'uploads', event.image_path)
const file = path.join(config.upload_path, event.image_path)
if (fs.statSync(file)) {
media = await bot.post('media', { file: fs.createReadStream(file) })
}

View File

@@ -53,8 +53,8 @@ const userController = {
// check if event is mine (or user is admin)
if (event && (req.user.is_admin || req.user.id === event.userId)) {
if (event.image_path) {
const old_path = path.resolve(__dirname, '..', '..', 'uploads', event.image_path)
const old_thumb_path = path.resolve(__dirname, '..', '..', 'uploads', 'thumb', event.image_path)
const old_path = path.join(config.upload_path, event.image_path)
const old_thumb_path = path.join(config.upload_path, 'thumb', event.image_path)
try {
await fs.unlink(old_path)
await fs.unlink(old_thumb_path)
@@ -126,8 +126,8 @@ const userController = {
if (req.file) {
if (event.image_path) {
const old_path = path.resolve(__dirname, '..', '..', 'uploads', event.image_path)
const old_thumb_path = path.resolve(__dirname, '..', '..', 'uploads', 'thumb', event.image_path)
const old_path = path.resolve(config.upload_path, event.image_path)
const old_thumb_path = path.resolve(config.upload_path, 'thumb', event.image_path)
await fs.unlink(old_path, e => console.error(e))
await fs.unlink(old_thumb_path, e => console.error(e))
}

View File

@@ -11,9 +11,7 @@ const exportController = require('./controller/export')
const userController = require('./controller/user')
const settingsController = require('./controller/settings')
const storage = require('./storage')({
destination: 'uploads/'
})
const storage = require('./storage')
const upload = multer({ storage })
const api = express.Router()

View File

@@ -1,62 +1,43 @@
const fs = require('fs')
const os = require('os')
const path = require('path')
const crypto = require('crypto')
const mkdirp = require('mkdirp')
const sharp = require('sharp')
const consola = require('consola')
const config = require('../config')
function getDestination(req, file, cb) {
cb(null, os.tmpdir())
}
function DiskStorage(opts) {
if (typeof opts.destination === 'string') {
mkdirp.sync(opts.destination)
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
} else {
this.getDestination = (opts.destination || getDestination)
}
}
DiskStorage.prototype._handleFile = function _handleFile(req, file, cb) {
const that = this
that.getDestination(req, file, function (err, destination) {
if (err) return cb(err)
mkdirp.sync(config.upload_path + '/thumb')
const DiskStorage = {
_handleFile(req, file, cb) {
const filename = crypto.randomBytes(16).toString('hex') + '.jpg'
const finalPath = path.join(destination, filename)
const thumbPath = path.join(destination, 'thumb', filename)
const finalPath = path.resolve(config.upload_path, filename)
const thumbPath = path.resolve(config.upload_path, 'thumb', filename)
const outStream = fs.createWriteStream(finalPath)
const thumbStream = fs.createWriteStream(thumbPath)
const resizer = sharp().resize(800).jpeg({ quality: 90 })
const thumbnailer = sharp().resize(400).jpeg({ quality: 90 })
file.stream.pipe(thumbnailer).pipe(thumbStream)
thumbStream.on('error', e => console.log('thumbStream error ', e))
thumbStream.on('error', e => consola.error('thumbStream error ', e))
file.stream.pipe(resizer).pipe(outStream)
outStream.on('error', cb)
outStream.on('finish', function () {
cb(null, {
destination,
destination: config.upload_path,
filename,
path: finalPath,
size: outStream.bytesWritten
})
})
})
},
_removeFile(req, file, cb) {
delete file.destination
delete file.filename
delete file.path
fs.unlink(path, cb)
}
}
DiskStorage.prototype._removeFile = function _removeFile(req, file, cb) {
let path = file.path
delete file.destination
delete file.filename
delete file.path
fs.unlink(path, cb)
}
module.exports = function (opts) {
return new DiskStorage(opts)
}
module.exports = DiskStorage