简体   繁体   English

node.js检查是否已将新文件复制到磁盘

[英]node.js check if copying new file to disk has finished

I'm trying to write a tethered shooting app with electron / node.js. 我正在尝试使用electronic / node.js编写一个拴系射击应用程序。

The communication between camera and computer is managed by the software, which ships with the camera. 相机和计算机之间的通信由相机附带的软件管理。 The new pictures are directly copied to my local hard disk. 新图片直接复制到我的本地硬盘上。 After that my app should show the new picture as fast as possible. 之后,我的应用程序应尽快显示新图片。

I set up a chokidar watcher for the directory where my new pictures arrive. 我为新照片到达的目录设置了一个chokidar监视程序。 Unfortunately the 不幸的是

awaitWriteFinish: {stabilityThreshold: 500, pollInterval: 50}

option of chokidar acts very slowly and sometimes triggers the add-event too early. 选择chokidar的动作非常缓慢,有时会过早触发添加事件。

My idea to circumvent this was to write my own polling mechanism after the watcher (without awaitWriteFinish option) triggers an event. 我想避免此问题的想法是在观察者(没有awaitWriteFinish选项)触发事件之后编写我自己的轮询机制。

I tried fs.stats for polling but fs.stats doesn't recognize the current file size but the total file size. 我尝试使用fs.stats进行轮询,但fs.stats无法识别当前文件大小,但无法识别总文件大小。

Is there a easy node.js / javascript / jquery approach to detect if the new file is completely written to disk? 是否有一种简单的node.js / javascript / jquery方法来检测新文件是否已完全写入磁盘?

Any help apprechiated. 任何帮助表示赞赏。

I would use manual approach like this. 我会使用这样的手动方法。

fs.writeSync()

notifyWhateverEnpoint('writing is finished!') // depending on your app, you should choose how to notify it about finishing

I believe you could watch the directory for changes. 我相信您可以查看目录中的更改。 Perhaps scan it first to have a list of files. 也许先扫描一下即可获得文件列表。 For example if file you are on Windows and file is saved on C:\\temp 例如,如果文件在Windows上并且文件保存在C:\\ temp

 const fs = require('fs') const saveDir = 'C\\\\temp' let currentFiles = []; // Read all cuurent files in directory fs.readdirSync(saveDir, (error, files) => { currentFiles = files; }); fs.watch(saveDir, (eventType, fileName) => { if (currentFiles.includes(fileName)) { return; } // Handle rename event // Handle change event }); 

More info here: 更多信息在这里:

https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM