|
|
|
@ -1,101 +1,54 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var path = require('path');
|
|
|
|
|
var winston = require('winston');
|
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
|
var mime = require('mime');
|
|
|
|
|
var graceful = require('graceful-fs');
|
|
|
|
|
|
|
|
|
|
var utils = require('./utils');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const nconf = require('nconf');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const winston = require('winston');
|
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
|
const mime = require('mime');
|
|
|
|
|
const graceful = require('graceful-fs');
|
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
|
|
const readdirAsync = util.promisify(fs.readdir);
|
|
|
|
|
const mkdirpAsync = util.promisify(mkdirp);
|
|
|
|
|
const copyFileAsync = util.promisify(fs.copyFile);
|
|
|
|
|
const writeFleAsync = util.promisify(fs.writeFile);
|
|
|
|
|
const statAsync = util.promisify(fs.stat);
|
|
|
|
|
const unlinkAsync = util.promisify(fs.unlink);
|
|
|
|
|
const linkAsync = util.promisify(fs.link);
|
|
|
|
|
const symlinkAsync = util.promisify(fs.symlink);
|
|
|
|
|
|
|
|
|
|
const utils = require('./utils');
|
|
|
|
|
|
|
|
|
|
graceful.gracefulify(fs);
|
|
|
|
|
|
|
|
|
|
var file = module.exports;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Asynchronously copies `src` to `dest`
|
|
|
|
|
* @param {string} src - source filename to copy
|
|
|
|
|
* @param {string} dest - destination filename of the copy operation
|
|
|
|
|
* @param {function(Error): void} callback
|
|
|
|
|
*/
|
|
|
|
|
function copyFile(src, dest, callback) {
|
|
|
|
|
var calledBack = false;
|
|
|
|
|
|
|
|
|
|
var read;
|
|
|
|
|
var write;
|
|
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
|
if (calledBack) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
calledBack = true;
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
if (read) {
|
|
|
|
|
read.destroy();
|
|
|
|
|
}
|
|
|
|
|
if (write) {
|
|
|
|
|
write.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read = fs.createReadStream(src);
|
|
|
|
|
read.on('error', done);
|
|
|
|
|
const file = module.exports;
|
|
|
|
|
|
|
|
|
|
write = fs.createWriteStream(dest);
|
|
|
|
|
write.on('error', done);
|
|
|
|
|
write.on('close', function () {
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
read.pipe(write);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.copyFile = (typeof fs.copyFile === 'function') ? fs.copyFile : copyFile;
|
|
|
|
|
|
|
|
|
|
file.saveFileToLocal = function (filename, folder, tempPath, callback) {
|
|
|
|
|
file.saveFileToLocal = async function (filename, folder, tempPath) {
|
|
|
|
|
/*
|
|
|
|
|
* remarkable doesn't allow spaces in hyperlinks, once that's fixed, remove this.
|
|
|
|
|
*/
|
|
|
|
|
filename = filename.split('.').map(function (name) {
|
|
|
|
|
return utils.slugify(name);
|
|
|
|
|
}).join('.');
|
|
|
|
|
filename = filename.split('.').map(name => utils.slugify(name)).join('.');
|
|
|
|
|
|
|
|
|
|
var uploadPath = path.join(nconf.get('upload_path'), folder, filename);
|
|
|
|
|
const uploadPath = path.join(nconf.get('upload_path'), folder, filename);
|
|
|
|
|
|
|
|
|
|
winston.verbose('Saving file ' + filename + ' to : ' + uploadPath);
|
|
|
|
|
mkdirp(path.dirname(uploadPath), function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.copyFile(tempPath, uploadPath, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, {
|
|
|
|
|
await mkdirpAsync(path.dirname(uploadPath));
|
|
|
|
|
await copyFileAsync(tempPath, uploadPath);
|
|
|
|
|
return {
|
|
|
|
|
url: '/assets/uploads/' + (folder ? folder + '/' : '') + filename,
|
|
|
|
|
path: uploadPath,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.base64ToLocal = function (imageData, uploadPath, callback) {
|
|
|
|
|
var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
|
|
|
|
file.base64ToLocal = async function (imageData, uploadPath) {
|
|
|
|
|
const buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
|
|
|
|
uploadPath = path.join(nconf.get('upload_path'), uploadPath);
|
|
|
|
|
|
|
|
|
|
fs.writeFile(uploadPath, buffer, {
|
|
|
|
|
await writeFleAsync(uploadPath, buffer, {
|
|
|
|
|
encoding: 'base64',
|
|
|
|
|
}, function (err) {
|
|
|
|
|
callback(err, uploadPath);
|
|
|
|
|
});
|
|
|
|
|
return uploadPath;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.isFileTypeAllowed = async function (path) {
|
|
|
|
@ -111,7 +64,7 @@ file.isFileTypeAllowed = async function (path) {
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/31205878/583363
|
|
|
|
|
file.appendToFileName = function (filename, string) {
|
|
|
|
|
var dotIndex = filename.lastIndexOf('.');
|
|
|
|
|
const dotIndex = filename.lastIndexOf('.');
|
|
|
|
|
if (dotIndex === -1) {
|
|
|
|
|
return filename + string;
|
|
|
|
|
}
|
|
|
|
@ -119,8 +72,8 @@ file.appendToFileName = function (filename, string) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.allowedExtensions = function () {
|
|
|
|
|
var meta = require('./meta');
|
|
|
|
|
var allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
|
|
|
|
|
const meta = require('./meta');
|
|
|
|
|
let allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
|
|
|
|
|
if (!allowedExtensions) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
@ -140,16 +93,16 @@ file.allowedExtensions = function () {
|
|
|
|
|
return allowedExtensions;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.exists = function (path, callback) {
|
|
|
|
|
fs.stat(path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
file.exists = async function (path) {
|
|
|
|
|
try {
|
|
|
|
|
await statAsync(path);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
|
return callback(null, false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return callback(err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
callback(null, true);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.existsSync = function (path) {
|
|
|
|
@ -165,52 +118,40 @@ file.existsSync = function (path) {
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.delete = function (path, callback) {
|
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
file.delete = async function (path) {
|
|
|
|
|
if (!path) {
|
|
|
|
|
return setImmediate(callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fs.unlink(path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
try {
|
|
|
|
|
await unlinkAsync(path);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
winston.warn(err);
|
|
|
|
|
}
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.link = function link(filePath, destPath, relative, callback) {
|
|
|
|
|
if (!callback) {
|
|
|
|
|
callback = relative;
|
|
|
|
|
relative = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.link = async function link(filePath, destPath, relative) {
|
|
|
|
|
if (relative && process.platform !== 'win32') {
|
|
|
|
|
filePath = path.relative(path.dirname(destPath), filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
fs.link(filePath, destPath, callback);
|
|
|
|
|
await linkAsync(filePath, destPath);
|
|
|
|
|
} else {
|
|
|
|
|
fs.symlink(filePath, destPath, 'file', callback);
|
|
|
|
|
await symlinkAsync(filePath, destPath, 'file');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.linkDirs = function linkDirs(sourceDir, destDir, relative, callback) {
|
|
|
|
|
if (!callback) {
|
|
|
|
|
callback = relative;
|
|
|
|
|
relative = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.linkDirs = async function linkDirs(sourceDir, destDir, relative) {
|
|
|
|
|
if (relative && process.platform !== 'win32') {
|
|
|
|
|
sourceDir = path.relative(path.dirname(destDir), sourceDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var type = (process.platform === 'win32') ? 'junction' : 'dir';
|
|
|
|
|
fs.symlink(sourceDir, destDir, type, callback);
|
|
|
|
|
const type = (process.platform === 'win32') ? 'junction' : 'dir';
|
|
|
|
|
await symlinkAsync(sourceDir, destDir, type);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
file.typeToExtension = function (type) {
|
|
|
|
|
var extension;
|
|
|
|
|
let extension = '';
|
|
|
|
|
if (type) {
|
|
|
|
|
extension = '.' + mime.getExtension(type);
|
|
|
|
|
}
|
|
|
|
@ -218,46 +159,14 @@ file.typeToExtension = function (type) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Adapted from http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
|
|
|
|
|
file.walk = function (dir, callback) {
|
|
|
|
|
var results = [];
|
|
|
|
|
|
|
|
|
|
fs.readdir(dir, function (err, list) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
var pending = list.length;
|
|
|
|
|
if (!pending) {
|
|
|
|
|
return callback(null, results);
|
|
|
|
|
}
|
|
|
|
|
list.forEach(function (filename) {
|
|
|
|
|
filename = dir + '/' + filename;
|
|
|
|
|
fs.stat(filename, function (err, stat) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stat && stat.isDirectory()) {
|
|
|
|
|
file.walk(filename, function (err, res) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results = results.concat(res);
|
|
|
|
|
pending -= 1;
|
|
|
|
|
if (!pending) {
|
|
|
|
|
callback(null, results);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
results.push(filename);
|
|
|
|
|
pending -= 1;
|
|
|
|
|
if (!pending) {
|
|
|
|
|
callback(null, results);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
file.walk = async function (dir) {
|
|
|
|
|
const subdirs = await readdirAsync(dir);
|
|
|
|
|
const files = await Promise.all(subdirs.map(async (subdir) => {
|
|
|
|
|
const res = path.resolve(dir, subdir);
|
|
|
|
|
return (await statAsync(res)).isDirectory() ? file.walk(res) : res;
|
|
|
|
|
}));
|
|
|
|
|
var result = files.reduce((a, f) => a.concat(f), []);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
require('./promisify')(file);
|
|
|
|
|