cleaning
This commit is contained in:
parent
d0a17d5471
commit
ef3231efd2
|
@ -18,64 +18,47 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var ERR = require("async-stacktrace");
|
var ERR = require("async-stacktrace")
|
||||||
var padManager = require("../db/PadManager");
|
, padManager = require("../db/PadManager")
|
||||||
var padMessageHandler = require("./PadMessageHandler");
|
, padMessageHandler = require("./PadMessageHandler")
|
||||||
var async = require("async");
|
, async = require("async")
|
||||||
var fs = require("fs");
|
, fs = require("fs")
|
||||||
var settings = require('../utils/Settings');
|
, settings = require('../utils/Settings')
|
||||||
var formidable = require('formidable');
|
, formidable = require('formidable')
|
||||||
var os = require("os");
|
, os = require("os")
|
||||||
|
, importHtml = require("../utils/ImportHtml");
|
||||||
// TESTING importing in HTML
|
|
||||||
var importHtml = require("../utils/ImportHtml");
|
|
||||||
|
|
||||||
//load abiword only if its enabled
|
//load abiword only if its enabled
|
||||||
if(settings.abiword != null)
|
if(settings.abiword != null)
|
||||||
var abiword = require("../utils/Abiword");
|
var abiword = require("../utils/Abiword");
|
||||||
|
|
||||||
//Patched in formidable since v1.0.4:
|
|
||||||
// The dafault temp directory is well detected in order to OS
|
|
||||||
//var tempDirectory = "/tmp/";
|
|
||||||
|
|
||||||
//tempDirectory changes if the operating system is windows
|
|
||||||
/*if(os.type().indexOf("Windows") > -1)
|
|
||||||
{
|
|
||||||
tempDirectory = process.env.TEMP;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* do a requested import
|
* do a requested import
|
||||||
*/
|
*/
|
||||||
exports.doImport = function(req, res, padId)
|
exports.doImport = function(req, res, padId)
|
||||||
{
|
{
|
||||||
//pipe to a file
|
//pipe to a file
|
||||||
//convert file to text via abiword
|
//convert file to html via abiword
|
||||||
//set text in the pad
|
//set html in the pad
|
||||||
|
|
||||||
var srcFile, destFile;
|
var srcFile, destFile
|
||||||
var pad;
|
, pad;
|
||||||
var text;
|
, text;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
//save the uploaded file to /tmp
|
//save the uploaded file to /tmp
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
|
||||||
var form = new formidable.IncomingForm();
|
var form = new formidable.IncomingForm();
|
||||||
form.keepExtensions = true;
|
form.keepExtensions = true;
|
||||||
//form.uploadDir = tempDirectory;
|
|
||||||
|
|
||||||
form.parse(req, function(err, fields, files)
|
form.parse(req, function(err, fields, files) {
|
||||||
{
|
|
||||||
//the upload failed, stop at this point
|
//the upload failed, stop at this point
|
||||||
if(err || files.file === undefined)
|
if(err || files.file === undefined) {
|
||||||
{
|
|
||||||
console.warn("Uploading Error: " + err.stack);
|
console.warn("Uploading Error: " + err.stack);
|
||||||
callback("uploadFailed");
|
callback("uploadFailed");
|
||||||
}
|
}
|
||||||
//everything ok, continue
|
//everything ok, continue
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
//save the path of the uploaded file
|
//save the path of the uploaded file
|
||||||
srcFile = files.file.path;
|
srcFile = files.file.path;
|
||||||
callback();
|
callback();
|
||||||
|
@ -85,45 +68,35 @@ exports.doImport = function(req, res, padId)
|
||||||
|
|
||||||
//ensure this is a file ending we know, else we change the file ending to .txt
|
//ensure this is a file ending we know, else we change the file ending to .txt
|
||||||
//this allows us to accept source code files like .c or .java
|
//this allows us to accept source code files like .c or .java
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
|
||||||
var fileEnding = (srcFile.split(".")[1] || "").toLowerCase();
|
var fileEnding = (srcFile.split(".")[1] || "").toLowerCase();
|
||||||
var knownFileEndings = ["txt", "doc", "docx", "pdf", "odt", "html", "htm"];
|
var knownFileEndings = ["txt", "doc", "docx", "pdf", "odt", "html", "htm"];
|
||||||
|
|
||||||
//find out if this is a known file ending
|
//find out if this is a known file ending
|
||||||
var fileEndingKnown = false;
|
var fileEndingKnown = false;
|
||||||
for(var i in knownFileEndings)
|
for(var i in knownFileEndings) {
|
||||||
{
|
if(fileEnding == knownFileEndings[i]){
|
||||||
if(fileEnding == knownFileEndings[i])
|
|
||||||
{
|
|
||||||
fileEndingKnown = true;
|
fileEndingKnown = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if the file ending is known, continue as normal
|
//if the file ending is known, continue as normal
|
||||||
if(fileEndingKnown)
|
if(fileEndingKnown) {
|
||||||
{
|
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
//we need to rename this file with a .txt ending
|
//we need to rename this file with a .txt ending
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
var oldSrcFile = srcFile;
|
var oldSrcFile = srcFile;
|
||||||
srcFile = srcFile.split(".")[0] + ".txt";
|
srcFile = srcFile.split(".")[0] + ".txt";
|
||||||
//srcFile = srcFile.split(".")[0] + ".htm";
|
|
||||||
|
|
||||||
fs.rename(oldSrcFile, srcFile, callback);
|
fs.rename(oldSrcFile, srcFile, callback);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
//convert file to text
|
//convert file to text
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
|
||||||
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
|
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
|
||||||
//destFile = tempDirectory + "eplite_import_" + randNum + ".txt";
|
|
||||||
//destFile = tempDirectory + "eplite_import_" + randNum + ".htm";
|
|
||||||
destFile = os.tmpDir() + "/eplite_import_" + randNum + ".htm";
|
destFile = os.tmpDir() + "/eplite_import_" + randNum + ".htm";
|
||||||
//abiword.convertFile(srcFile, destFile, "txt", function(err){
|
|
||||||
abiword.convertFile(srcFile, destFile, "htm", function(err) {
|
abiword.convertFile(srcFile, destFile, "htm", function(err) {
|
||||||
//catch convert errors
|
//catch convert errors
|
||||||
if(err) {
|
if(err) {
|
||||||
|
@ -136,10 +109,8 @@ exports.doImport = function(req, res, padId)
|
||||||
},
|
},
|
||||||
|
|
||||||
//get the pad object
|
//get the pad object
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
padManager.getPad(padId, function(err, _pad){
|
||||||
padManager.getPad(padId, function(err, _pad)
|
|
||||||
{
|
|
||||||
if(ERR(err, callback)) return;
|
if(ERR(err, callback)) return;
|
||||||
pad = _pad;
|
pad = _pad;
|
||||||
callback();
|
callback();
|
||||||
|
@ -147,54 +118,40 @@ exports.doImport = function(req, res, padId)
|
||||||
},
|
},
|
||||||
|
|
||||||
//read the text
|
//read the text
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
fs.readFile(destFile, "utf8", function(err, _text){
|
||||||
fs.readFile(destFile, "utf8", function(err, _text)
|
|
||||||
{
|
|
||||||
if(ERR(err, callback)) return;
|
if(ERR(err, callback)) return;
|
||||||
text = _text;
|
text = _text;
|
||||||
|
|
||||||
//node on windows has a delay on releasing of the file lock.
|
//node on windows has a delay on releasing of the file lock.
|
||||||
//We add a 100ms delay to work around this
|
//We add a 100ms delay to work around this
|
||||||
if(os.type().indexOf("Windows") > -1)
|
if(os.type().indexOf("Windows") > -1){
|
||||||
{
|
setTimeout(function(){callback();}, 100);
|
||||||
setTimeout(function()
|
} else {
|
||||||
{
|
|
||||||
callback();
|
|
||||||
}, 100);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//change text of the pad and broadcast the changeset
|
//change text of the pad and broadcast the changeset
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
|
||||||
//pad.setText(text);
|
|
||||||
//prueba
|
|
||||||
importHtml.setPadHTML(pad, text);
|
importHtml.setPadHTML(pad, text);
|
||||||
padMessageHandler.updatePadClients(pad, callback);
|
padMessageHandler.updatePadClients(pad, callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
//clean up temporary files
|
//clean up temporary files
|
||||||
function(callback)
|
function(callback) {
|
||||||
{
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(callback)
|
function(callback){
|
||||||
{
|
|
||||||
fs.unlink(srcFile, callback);
|
fs.unlink(srcFile, callback);
|
||||||
},
|
},
|
||||||
function(callback)
|
function(callback){
|
||||||
{
|
|
||||||
fs.unlink(destFile, callback);
|
fs.unlink(destFile, callback);
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
], function(err)
|
], function(err) {
|
||||||
{
|
|
||||||
var status = "ok";
|
var status = "ok";
|
||||||
|
|
||||||
//check for known errors and replace the status
|
//check for known errors and replace the status
|
||||||
|
|
Loading…
Reference in New Issue