- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 Node.js 构建一个 Web 应用程序,现在我需要生成一个 PDF 目录的 tar 存档。该应用程序在运行 Ubuntu 14.04 服务器的 VM 上运行。我执行此操作的代码如下所示:
function tarDirectory(path, token, callback) {
var exec = require('child_process').exec;
var cmd = 'cd ' + path + ' && tar -cvf genericName-' + token + '.tar' + ' ' + token;
exec(cmd, function(error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
if (error) {
console.error(error);
}
if(callback) callback();
});
}
此 tarDirectory 函数由以下代码调用:
router.post('/files/generate', function(req, res, next) {
IDList = req.body['IDs[]'];
token = req.body['token'];
// if just a single file being generated
if (typeof req.body['IDs[]'] === "string"){
filehelper.generateFile(IDList[0], req.app.locals.site.basedir + "temp/", token);
}
// if multiple files being generated
else {
IDList.forEach(function(id) {
filehelper.generateFile(id, req.app.locals.site.basedir + "temp/", token);
});
}
filehelper.tarDirectory(req.app.locals.site.basedir + "temp/", token, res.end);
});
该代码需要一个带有动态数据的发布请求,该动态数据是通过在我的网络应用程序中单击按钮生成的,然后将根据数据创建文件并将其压缩到一个目录中。这一切都很好……第一次。当我一段时间内第一次单击该按钮时,会生成 tar,当我打开它时,客户端 PDF 与服务器上的相同。但是,当我在大约一个小时内再次单击时,我收到了一个 tar 文件,但是当我打开存档并将其解压缩时,PDF 全部损坏并且大约是预期字节大小的一半。我在这里不知所措......我怀疑这可能与流关闭处理不当有关,但我不确定。
这是将 PDF 生成到一个目录中的代码,生成后将其打包:
function generateFile(id, path, token) {
var dirPath = path + token;
var filePath = path + token + "/file" + id + ".pdf";
console.log("creating file for: " + id);
try{
fs.statSync(dirPath).isDirectory();
} catch (err) {
fs.mkdirSync(dirPath);
}
// start the file pdf generation
file = new PDFDocument();
output = fs.createWriteStream(filePath);
output.on('close', function(){
return;
});
file.pipe(output);
// handle the intricacies of the file generation
file.text("file" + id + ".pdf");
// end the file
file.end();
}
最佳答案
generateFile
函数中,您有 WriteStream,它是异步的。但是,您将此函数调用为 sync.,并且在 pdf 生成完成之前没有等待就开始 .tar 压缩,这可能会导致此问题。generateFile
,或者迭代 async.,并且仅在所有文件生成完成后才开始压缩。bluebird 示例:
var Promise = require('bluebird');
function generateFile(id, path, token) {
return new Promise(function(resolve, reject) {
var dirPath = path + token;
var filePath = path + token + "/file" + id + ".pdf";
console.log("creating file for: " + id);
try{
fs.statSync(dirPath).isDirectory();
} catch (err) {
fs.mkdirSync(dirPath);
}
// start the file pdf generation
file = new PDFDocument();
output = fs.createWriteStream(filePath);
output.on('close', function(){
return resolve();
});
output.on('error', function(error) {
return reject(error);
});
file.pipe(output);
// handle the intricacies of the file generation
file.text("file" + id + ".pdf");
// end the file
file.end();
});
}
Pdfs 生成和压缩。
var Promise = require('bluebird');
....
//IDList.forEach(function(id) {
// filehelper.generateFile(id, req.app.locals.site.basedir + "temp/", //token);});
//replace with
Promise.map(IDList, function(id) {
return filehelper.generateFile(id, req.app.locals.site.basedir + "temp/", token);
})
.then(function() {
//all files are ready, start compressing
})
.catch(function(error) {
//we have error
});
关于Node.js - tar 文件的 exec 命令第一次工作正常,但在后续执行时产生损坏的 tar 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38826925/
@ 。 目录 集成Flink 编程示例 打包运行 CDC入湖 概述
当我通过多个模式匹配发送一个变量时,这样做的命名约定是什么? val somethingBetweenOriginalAndResult = original match { case ...
this post 的后续工作:(根据评论中的要求进行更新) 我从实际(伪装的)示例数据以及在该数据上运行脚本的结果开始。 原始数据样本: 目录中前两个文件的标题和前两行。它们是相同的数据,这不是错误
当我通过多个模式匹配发送一个变量时,这样做的命名约定是什么? val somethingBetweenOriginalAndResult = original match { case ...
this post 的后续工作:(根据评论中的要求进行更新) 我从实际(伪装的)示例数据以及在该数据上运行脚本的结果开始。 原始数据样本: 目录中前两个文件的标题和前两行。它们是相同的数据,这不是错误
我just asked a very similar question here两个答案都有效。但是,当我在每个字典中有 > 2 个字典键时,第 3 个键的值始终为 0。例如: myList = [{
此问题是 Trapping Frame Entrance in tkinter 的后续问题其中@jasonharper 提供了非常有用的答案。 在下面的代码中,我将入口绑定(bind)到 Canvas
使用 subsequent from 子句和query continuation 有什么区别? 后面的 from 子句是指使用 ...from...in...from...in...select 模式
我的原题可以查到here ,为此我得到了一些很好的答案、idas 和提示。 作为可行性和性能研究的一部分,我已经开始转换我的模式,以便使用这些想法对我的数据进行版本控制。在这样做的过程中,我想出了一些
圆 A 沿 x 轴向右移动。圆 B 沿 y 轴向上移动。我想知道他们是否会发生碰撞。 (不是何时,只是如果。) 半径相同,恒速度不同。 This answer似乎解决了这个问题,我的问题最好应该是这个
在需要定期轮询服务器的 AJAX 应用程序(如聊天应用程序)中,建议两次调用之间的最短时间是多少,以便尽快完成更新?什么时间被认为是服务器和客户端的 hogs? 最佳答案 这个问题的答案很大程度上取决
这是 a previous question on Stack Overflow 的后续行动(参见引用链接)。考虑以下代码(来自 W3Schools): /* unvisited link */ a:
昨天,我问了a question关于如何在 .NET 中动态呈现某些内容:具体来说,我询问了如何创建用于“绘图”/渲染的白色“ Canvas ”,使用什么框架等。但是,许多答案建议我问更具体的答案,所
这是上一个问题的后续: Java compiler fails to recognise static inner class accepted answer给出了一个解决方法,如果找不到更好的解决方
这个问题跟在问题之后:How to read two lines from a file and create dynamics keys in a for-loop? 但是,问题的性质已经发展到我想
好的跟进 this thread ,这就是我想出的…… public class SharweAuthorizeAttribute : AuthorizeAttribute { private
Using nested loops 我仍在努力实现上述问题中所述的相同目标。基本上我的代码现在看起来像这样: for(int ac = 0; ac < gravityObject.length; a
一直在寻找这个没有结果。我有一个片段,我想将文本文件读入 python 中的变量中,以便稍后可以引用它(特别是杀死正在运行的进程)。 文件生成如下: os.system('wmic process w
我创建了 2 个自定义跟进 Intent ,对于这 2 个自定义跟进 Intent ,还有 3 个跟进 Intent 。一切安好。我从这 2 个阶段获取信息,然后显示结果。该过程大约是 2 个后续问题
这个问题在这里已经有了答案: Iterate through parameters skipping the first (5 个答案) 关闭 5 年前。 我有一个脚本,它的第一个参数是一个对象的标
我是一名优秀的程序员,十分优秀!