Re: [Koha] How to check a file's MIME type in KOHA
Hi All, I have checked the MIME information of a specific file using its extension. But actually I need details of exact file. Say, I have a file like test.jpg , somebody renamed its extension as test.pdf. If we checked the MIME information of test.pdf, it should display the output as image/jpeg. For implementing this , I am trying to integrate *File::MMagic::XS* into KOHA with the following steps. 1. Download *File::MMagic::XS* from http://search.cpan.org/~dmaki/ File-MMagic-XS-0.09008/lib/File/MMagic/XS.pm <http://search.cpan.org/%7Edmaki/File-MMagic-XS-0.09008/lib/File/MMagic/XS.pm> . 2. Extract the tar file and put the extracted file into usr/share/koha/lib/C4. 3. Take command prompt and Use command : cd /usr/share/koha/lib/C4/File- MMagic/ 4. Use command: perl Makefile.PL 5. Use command: make 6. Use command: make install After that , call *use C4::File-MMagic::lib::File::MMagic::XS;* *my $m = File::MMagic::XS->new();$m = File::MMagic::XS->new('/etc/magic'); # use external magic filemy $mime = $m->get_mime($source);* into the specific page and upload a jpeg file.But I got the MIME type as *text/plain* Can someone help on this issue? Regards, Rejeesh K.Nair
On Wed, Feb 1, 2017 at 2:51 PM, Indranil Das Gupta <indradg@gmail.com> wrote:
Hi,
You need to look into /usr/share/koha/lib/Koha/Upload.pm and /usr/share/koha/intranet/cgi-bin/tools/upload.pl files.
The perl module file is place where you need to write up the new function to handle the mimetype and then call that function from upload.pl script.
hope this helps -idg
-- Indranil Das Gupta L2C2 Technologies
Phone : +91-98300-20971 <098300%2020971> WWW : http://www.l2c2.co.in Blog : http://blog.l2c2.co.in IRC : indradg on irc://irc.freenode.net Twitter : indradg
On Tue, Jan 31, 2017 at 10:48 AM, Rejeesh K.Nair <rejeeshknair@gmail.com> wrote:
Hi Indranil,
Thank you for your quick reply.
Could you please help me, how to integrate the code to check the MIME type in KOHA?
Regards, Rejeesh K Nair
On Mon, Jan 30, 2017 at 3:14 PM, Indranil Das Gupta <indradg@gmail.com> wrote:
Hi Rajeesh,
On Mon, Jan 30, 2017 at 10:52 AM, Rejeesh K.Nair < rejeeshknair@gmail.com> wrote:
I need to check the MIME type of each uploaded file for security purposes.
Some search results from Google suggest that we can do it with the help of the File::MimeInfo <https://metacpan.org/pod/File::MimeInfo>Perl module.
How can we install the File::MimeInfo module in KOHA and check the MIME types, or is there some better way?
IIRC, that sort of generic mimetype detection functionality does not exist in Koha. From the top of my head, the only place where we actively check for mimetype is the image upload option using GD which is basically restricted to a few image formats like PNG, JPEG, XPM etc.
Short answer: To implement this on the server side, you have to write the necessary code. As a stop gap, you *could* use the Javascript FileReader object to check the mimetype on the client side. but that is not a fool proof solution.
hope this helps. -- Indranil Das Gupta L2C2 Technologies
Phone : +91-98300-20971 <098300%2020971> WWW : http://www.l2c2.co.in Blog : http://blog.l2c2.co.in IRC : indradg on irc://irc.freenode.net Twitter : indradg
-- Thanks & Regards,
Rejeesh K.Nair m: +91 (0) 9495316906 <+91%2094953%2016906> e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/>
-- Thanks & Regards,
Rejeesh K.Nair m: +91 (0) 9495316906 <094953%2016906> e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/>
-- Thanks & Regards, Rejeesh K.Nair m: +91 (0) 9495316906 e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/>
Excerpts from Rejeesh K.Nair's message of 2017-02-07 15:49:42 +0530:
I have checked the MIME information of a specific file using its extension. But actually I need details of exact file.
I'm a newbie with the Koha source code. But I'm wondering if there is some way you could use the external "file" program from within Koha to determine the file type. This program is not fooled by file name extensions; it examines the file contents to determine the type. Here is an example of a test I ran where I renamed a .pdf file with .jpg extension in an attempt to fool file: $ cp koha-notes.pdf junk.jpg $ file -i junk.jpg junk.jpg: application/pdf; charset=binary
Hi Mark, Your sample code meets my requirement. But I need to implement this for each uploading file in KOHA code. Regards, Rejeesh K.Nair On Tue, Feb 7, 2017 at 7:36 PM, Mark Alexander <marka@pobox.com> wrote:
Excerpts from Rejeesh K.Nair's message of 2017-02-07 15:49:42 +0530:
I have checked the MIME information of a specific file using its extension. But actually I need details of exact file.
I'm a newbie with the Koha source code. But I'm wondering if there is some way you could use the external "file" program from within Koha to determine the file type. This program is not fooled by file name extensions; it examines the file contents to determine the type. Here is an example of a test I ran where I renamed a .pdf file with .jpg extension in an attempt to fool file:
$ cp koha-notes.pdf junk.jpg $ file -i junk.jpg junk.jpg: application/pdf; charset=binary
-- Thanks & Regards, Rejeesh K.Nair m: +91 (0) 9495316906 e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/>
Hi Rejeesh, there are a few ways to execute a system command from perl code. You can use http://perldoc.perl.org/functions/system.html Not sure if that's the best solution. Regards Holger Rejeesh K.Nair wrote:
Hi Mark,
Your sample code meets my requirement. But I need to implement this for each uploading file in KOHA code.
Regards, Rejeesh K.Nair
Hi All, Many thanks for all your timely support , We got a solution for checking MIME type. Here I am sharing the code snippet I have used to solve it . Please see the code and share your comments. my $file = $query->param("id_list_file"); my $tmpfilename = $query->tmpFileName($file); my $mimeinfo = `file -i -b $tmpfilename`; my @fields = split /;/, $mimeinfo; my $mimetype = $fields[0]; #print $mimetype; if($mimetype eq 'application/marc' ){ //Required action }else{ //Required action } Regards, Rejeesh On Wed, Feb 8, 2017 at 2:03 PM, Holger Meissner < Holger.Meissner@hs-gesundheit.de> wrote:
Hi Rejeesh,
there are a few ways to execute a system command from perl code. You can use http://perldoc.perl.org/functions/system.html
Not sure if that's the best solution.
Regards Holger
Rejeesh K.Nair wrote:
Hi Mark,
Your sample code meets my requirement. But I need to implement this for each uploading file in KOHA code.
Regards, Rejeesh K.Nair
Koha mailing list http://koha-community.org Koha@lists.katipo.co.nz https://lists.katipo.co.nz/mailman/listinfo/koha
-- Thanks & Regards, Rejeesh K.Nair m: +91 (0) 9495316906 e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/>
Hi Rejeesh, I have just written a patch using CGI->uploadInfo. I think it does what you want. See bug 18087, patch 'Handle invalid filetypes'. Regards, Jonathan On Tue, 14 Feb 2017 at 07:11 Rejeesh K.Nair <rejeeshknair@gmail.com> wrote:
Hi All,
Many thanks for all your timely support , We got a solution for checking MIME type. Here I am sharing the code snippet I have used to solve it . Please see the code and share your comments.
my $file = $query->param("id_list_file"); my $tmpfilename = $query->tmpFileName($file); my $mimeinfo = `file -i -b $tmpfilename`; my @fields = split /;/, $mimeinfo; my $mimetype = $fields[0]; #print $mimetype; if($mimetype eq 'application/marc' ){ //Required action }else{ //Required action }
Regards, Rejeesh
On Wed, Feb 8, 2017 at 2:03 PM, Holger Meissner < Holger.Meissner@hs-gesundheit.de> wrote:
Hi Rejeesh,
there are a few ways to execute a system command from perl code. You can use http://perldoc.perl.org/functions/system.html
Not sure if that's the best solution.
Regards Holger
Rejeesh K.Nair wrote:
Hi Mark,
Your sample code meets my requirement. But I need to implement this for each uploading file in KOHA code.
Regards, Rejeesh K.Nair
Koha mailing list http://koha-community.org Koha@lists.katipo.co.nz https://lists.katipo.co.nz/mailman/listinfo/koha
-- Thanks & Regards,
Rejeesh K.Nair m: +91 (0) 9495316906 <+91%2094953%2016906> e: rejeeshknair@gmail.com <https://www.facebook.com/rejeeshnairk> <https://twitter.com/rejeeshknair> <http://in.linkedin.com/in/rejeeshknair> <https://www.instagram.com/rejeeshknair/> _______________________________________________ Koha mailing list http://koha-community.org Koha@lists.katipo.co.nz https://lists.katipo.co.nz/mailman/listinfo/koha
participants (4)
-
Holger Meissner -
Jonathan Druart -
Mark Alexander -
Rejeesh K.Nair