You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/file.js

87 lines
2.0 KiB
JavaScript

11 years ago
"use strict";
var fs = require('fs'),
nconf = require('nconf'),
path = require('path'),
10 years ago
winston = require('winston'),
jimp = require('jimp'),
10 years ago
utils = require('../public/src/utils');
var file = {};
10 years ago
file.saveFileToLocal = function(filename, folder, tempPath, callback) {
/*
* remarkable doesn't allow spaces in hyperlinks, once that's fixed, remove this.
*/
filename = filename.split('.');
filename.forEach(function(name, idx) {
filename[idx] = utils.slugify(name);
});
filename = filename.join('.');
10 years ago
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), folder, filename);
10 years ago
winston.verbose('Saving file '+ filename +' to : ' + uploadPath);
var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath);
is.on('end', function () {
callback(null, {
url: nconf.get('upload_url') + folder + '/' + filename
});
});
os.on('error', callback);
is.pipe(os);
11 years ago
};
file.isFileTypeAllowed = function(path, callback) {
// Attempt to read the file, if it passes, file type is allowed
jimp.read(path, function(err) {
callback(err);
});
10 years ago
};
file.allowedExtensions = function() {
var meta = require('./meta');
var allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
if (!allowedExtensions) {
return [];
}
allowedExtensions = allowedExtensions.split(',');
allowedExtensions = allowedExtensions.filter(Boolean).map(function(extension) {
extension = extension.trim();
if (!extension.startsWith('.')) {
extension = '.' + extension;
}
return extension;
});
if (allowedExtensions.indexOf('.jpg') !== -1 && allowedExtensions.indexOf('.jpeg') === -1) {
allowedExtensions.push('.jpeg');
}
return allowedExtensions;
};
9 years ago
file.exists = function(path, callback) {
fs.stat(path, function(err, stat) {
callback(!err && stat);
});
};
file.existsSync = function(path) {
var exists = false;
try {
exists = fs.statSync(path);
} catch(err) {
exists = false;
}
return !!exists;
};
module.exports = file;