LINUX: how to detect that ftp file upload is finis

2019-02-04 07:50发布

In my project I have a file uploading feature. Files are uploaded via FTP. I need to configure a listener that will check for new files and invoke a script only when file uploading is finished. Because if I run this script immediately after detecting the new file, it can start to process file that is not completely uploaded, which will cause an error. Can anybody tell if this is possible on LINUX and how can I do this?

标签: linux upload ftp
5条回答
做个烂人
2楼-- · 2019-02-04 08:33

I have used the HiddenStores feature of Proftpd. It keeps in-transit files hidden by prefixing them with a .in.filename.ext until they have finished uploading. Your process can then safely list the directories for completed files.

http://www.proftpd.org/docs/directives/linked/config_ref_HiddenStores.html

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-04 08:34

Have a look at inotify

It doesn't automatically watch sub directories though, so if you need to monitor many ftp accounts (or the FTP client wants to create a sub dir and upload there) you'll need to handle this yourself.

查看更多
看我几分像从前
4楼-- · 2019-02-04 08:35

I was looking for the same thing and stumbled on pureftpd which has an upload script feature. Sounds like exactly what was needed. Found the details here: http://www.linuxbyexamples.net/2012/10/config-ftp-server-trigger-upload-file-to-call-external-script.html

查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-04 08:36

Apache "Mina" ftp server (java) might be able to do what you want, including detecting a failed upload, as mentioned here

Quote:

From Ftplet.afterCommand, you should be able to look at the reply. For those failed transfers that FtpServer can detect (that causes an SocketException or IOException) this should be something like 426 or 551.

Ftplet overview here, including response codes.

The afterCommand method signature:

FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply)

You'd check reply.getCode() in your overriden method. You should subclass DefaultFtplet rather than implementing Ftplet interface from scratch.

Note that DefaultFtplet::afterCommand shows how to detect what client command is being responded to. You can check for STOR or STOU and reply-code 426 or 551 to detect failed uploads.

However, this may not detect an upload intentionally terminated by the client, if the client app decides to treat the transfer as though the file was just shorter than it is. In the case of a unintentionally broken connection, I think the reply-code check will work. A test could be to kill the client app, or bring down the client machine's network interface.

To handle successful uploads (your original question), you can look for the success reply-code instead, ie 226.

查看更多
Root(大扎)
6楼-- · 2019-02-04 08:46

I'd try using inotify, event code IN_CLOSE_WRITE.

查看更多
登录 后发表回答