Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate an image file in Perl?

How would I validate that a jpg file is a valid image file. We are having files written to a directory using FTP, but we seem to be picking up the file before it has finished writing it, creating invalid images. I need to be able to identify when it is no longer being written to. Any ideas?

like image 528
Xetius Avatar asked Jan 23 '23 21:01

Xetius


2 Answers

Easiest way might just be to write the file to a temporary directory and then move it to the real directory after the write is finished.

Or you could check here.

JPEG::Error

[arguments: none] If the file reference remains undefined after a call to new, the file is to be considered not parseable by this module, and one should issue some error message and go to another file. An error message explaining the reason of the failure can be retrieved with the Error method:

EDIT:

Image::TestJPG might be even better.

like image 62
drby Avatar answered Jan 26 '23 10:01

drby


You're solving the wrong problem, I think.

What you should be doing is figuring out how to tell when whatever FTPd you're using is done writing the file - that way when you come to have the same problem for (say) GIFs, DOCs or MPEGs, you don't have to fix it again.

Precisely how you do that depends rather crucially on what FTPd on what OS you're running. Some do, I believe, have hooks you can set to trigger when an upload's done.

If you can run your own FTPd, Net::FTPServer or POE::Component::Server::FTP are customizable to do the right thing.

In the absence of that:

1) try tailing the logs with a Perl script that looks for 'upload complete' messages 2) use something like lsof or fuser to check whether anything is locking a file before you try and copy it.

like image 26
Penfold Avatar answered Jan 26 '23 11:01

Penfold