2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
2016-10-15 03:31:15 +00:00
|
|
|
|
2017-07-23 03:56:46 +00:00
|
|
|
/* global wiki */
|
2017-05-13 19:29:00 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
const path = require('path')
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
|
|
|
const multer = require('multer')
|
|
|
|
const os = require('os')
|
|
|
|
const _ = require('lodash')
|
2016-10-15 03:31:15 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-23 03:56:46 +00:00
|
|
|
* Local Disk Storage
|
2016-10-15 03:31:15 +00:00
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
_uploadsPath: './repo/uploads',
|
|
|
|
_uploadsThumbsPath: './data/thumbs',
|
|
|
|
|
|
|
|
uploadImgHandler: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Local Data Storage model
|
|
|
|
*/
|
|
|
|
init () {
|
2017-07-23 03:56:46 +00:00
|
|
|
this._uploadsPath = path.resolve(wiki.ROOTPATH, wiki.config.paths.repo, 'uploads')
|
|
|
|
this._uploadsThumbsPath = path.resolve(wiki.ROOTPATH, wiki.config.paths.data, 'thumbs')
|
2017-02-09 01:52:37 +00:00
|
|
|
|
2017-07-23 03:56:46 +00:00
|
|
|
this.createBaseDirectories()
|
2017-07-29 21:33:08 +00:00
|
|
|
// this.initMulter()
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init Multer upload handlers
|
|
|
|
*/
|
2017-07-23 03:56:46 +00:00
|
|
|
initMulter () {
|
2017-02-09 01:52:37 +00:00
|
|
|
let maxFileSizes = {
|
2017-07-29 21:33:08 +00:00
|
|
|
// img: wiki.config.uploads.maxImageFileSize * 1024 * 1024,
|
|
|
|
// file: wiki.config.uploads.maxOtherFileSize * 1024 * 1024
|
|
|
|
img: 3 * 1024 * 1024,
|
|
|
|
file: 10 * 1024 * 1024
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -> IMAGES
|
|
|
|
|
|
|
|
this.uploadImgHandler = multer({
|
|
|
|
storage: multer.diskStorage({
|
|
|
|
destination: (req, f, cb) => {
|
2017-07-23 03:56:46 +00:00
|
|
|
cb(null, path.resolve(wiki.ROOTPATH, wiki.config.paths.data, 'temp-upload'))
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
fileFilter: (req, f, cb) => {
|
|
|
|
// -> Check filesize
|
|
|
|
|
|
|
|
if (f.size > maxFileSizes.img) {
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check MIME type (quick check only)
|
|
|
|
|
|
|
|
if (!_.includes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'], f.mimetype)) {
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, true)
|
|
|
|
}
|
|
|
|
}).array('imgfile', 20)
|
|
|
|
|
|
|
|
// -> FILES
|
|
|
|
|
|
|
|
this.uploadFileHandler = multer({
|
|
|
|
storage: multer.diskStorage({
|
|
|
|
destination: (req, f, cb) => {
|
2017-07-23 03:56:46 +00:00
|
|
|
cb(null, path.resolve(wiki.ROOTPATH, wiki.config.paths.data, 'temp-upload'))
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
fileFilter: (req, f, cb) => {
|
|
|
|
// -> Check filesize
|
|
|
|
|
|
|
|
if (f.size > maxFileSizes.file) {
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, true)
|
|
|
|
}
|
|
|
|
}).array('binfile', 20)
|
|
|
|
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a base directories (Synchronous).
|
|
|
|
*/
|
2017-07-23 03:56:46 +00:00
|
|
|
createBaseDirectories () {
|
2017-02-09 01:52:37 +00:00
|
|
|
try {
|
2017-07-23 03:56:46 +00:00
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data))
|
|
|
|
fs.emptyDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data))
|
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data, './cache'))
|
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data, './thumbs'))
|
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data, './temp-upload'))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
if (os.type() !== 'Windows_NT') {
|
2017-07-23 03:56:46 +00:00
|
|
|
fs.chmodSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.data, './temp-upload'), '755')
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 03:56:46 +00:00
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.repo))
|
|
|
|
fs.ensureDirSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.repo, './uploads'))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
if (os.type() !== 'Windows_NT') {
|
2017-07-23 03:56:46 +00:00
|
|
|
fs.chmodSync(path.resolve(wiki.ROOTPATH, wiki.config.paths.repo, './uploads'), '755')
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2017-07-23 03:56:46 +00:00
|
|
|
wiki.logger.error(err)
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 05:34:47 +00:00
|
|
|
wiki.logger.info('Disk Data Paths: [ OK ]')
|
2017-02-09 01:52:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the uploads path.
|
|
|
|
*
|
|
|
|
* @return {String} The uploads path.
|
|
|
|
*/
|
|
|
|
getUploadsPath () {
|
|
|
|
return this._uploadsPath
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the thumbnails folder path.
|
|
|
|
*
|
|
|
|
* @return {String} The thumbs path.
|
|
|
|
*/
|
|
|
|
getThumbsPath () {
|
|
|
|
return this._uploadsThumbsPath
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if filename is valid and unique
|
|
|
|
*
|
|
|
|
* @param {String} f The filename
|
|
|
|
* @param {String} fld The containing folder
|
|
|
|
* @param {boolean} isImage Indicates if image
|
|
|
|
* @return {Promise<String>} Promise of the accepted filename
|
|
|
|
*/
|
|
|
|
validateUploadsFilename (f, fld, isImage) {
|
|
|
|
let fObj = path.parse(f)
|
2017-07-23 03:56:46 +00:00
|
|
|
let fname = _.chain(fObj.name).trim().toLower().kebabCase().value().replace(new RegExp('[^a-z0-9-' + wiki.data.regex.cjk + wiki.data.regex.arabic + ']', 'g'), '')
|
2017-02-09 01:52:37 +00:00
|
|
|
let fext = _.toLower(fObj.ext)
|
|
|
|
|
|
|
|
if (isImage && !_.includes(['.jpg', '.jpeg', '.png', '.gif', '.webp'], fext)) {
|
|
|
|
fext = '.png'
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fname + fext
|
|
|
|
let fpath = path.resolve(this._uploadsPath, fld, f)
|
|
|
|
|
|
|
|
return fs.statAsync(fpath).then((s) => {
|
2017-07-23 03:56:46 +00:00
|
|
|
throw new Error(wiki.lang.t('errors:fileexists', { path: f }))
|
2017-02-09 01:52:37 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|