[Lazarus] Function to get the mimetype from a file extension

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[Lazarus] Function to get the mimetype from a file extension

Felipe Monteiro de Carvalho
Hello,

I need this now and after some Googling I couldn't find anything. So I
was wondering, maybe we already have this somewhere in FPC or Lazarus?
If not it might be something to consider adding to LazUtils.

--
Felipe Monteiro de Carvalho

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Žilvinas Ledas
Hi,

you should look at turbopower_ipro/iputils.pas - it has
function GetLocalContent(const TheFileName: string): string;
with valid windows implementation and array-lookup for other OS'es.

Also there is lmimewrapper.pp in LNet:
procedure TMimeFileSection.SetContentType(const aFileName: string);

it has a big IF statement for selecting type.


Regards,
Žilvinas

On 2012-05-09 14:39, Felipe Monteiro de Carvalho wrote:
> Hello,
>
> I need this now and after some Googling I couldn't find anything. So I
> was wondering, maybe we already have this somewhere in FPC or Lazarus?
> If not it might be something to consider adding to LazUtils.
>

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

leledumbo
Administrator
In reply to this post by Felipe Monteiro de Carvalho
No, we don't have a builtin one. However using any mime.types file (I forgot where I got mine, it's either from one of fpweb/lnet demo or my apache/php installation) you can do what you want:

uses
  fpmimetypes;
...
TheMimeType := MimeTypes.GetMimeType(ExtractFileExt(AFileName));
// TheMimeType now contains, e.g., 'application/octet-stream'
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

leledumbo
Administrator
Oh, I forgot that you need to:

MimeTypes.LoadFromFile(AMimeTypesFile);

first.
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Mark Morgan Lloyd
In reply to this post by Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote:
> Hello,
>
> I need this now and after some Googling I couldn't find anything. So I
> was wondering, maybe we already have this somewhere in FPC or Lazarus?
> If not it might be something to consider adding to LazUtils.

On what platform? On Windows you'd want something related to ftype/assoc
(i.e. possibly looking at the underlying registry), on some (but
possibly not all) unix desktops xdg-mime. In the latter cases it's very
much dependent on the distro providing the xdg- family of tools, and
also on there being an active desktop session (not just a remote shell).

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

wkitty42
In reply to this post by Felipe Monteiro de Carvalho
On 5/9/2012 07:39, Felipe Monteiro de Carvalho wrote:
> Hello,
>
> I need this now and after some Googling I couldn't find anything. So I
> was wondering, maybe we already have this somewhere in FPC or Lazarus?
> If not it might be something to consider adding to LazUtils.

wouldn't it be better to use mimemagic instead of relying on the file's extension?

i can easily name foo.gif as foo.txt and those systems that do properly look
into the file to determine its format/type will open it with the proper tool
instead of bringing it up in the app indicated by the extension...


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

michael.vancanneyt
In reply to this post by Felipe Monteiro de Carvalho


On Wed, 9 May 2012, Felipe Monteiro de Carvalho wrote:

> Hello,
>
> I need this now and after some Googling I couldn't find anything. So I
> was wondering, maybe we already have this somewhere in FPC or Lazarus?
> If not it might be something to consider adding to LazUtils.

FPC contains this unit:

fpmimetypes.pp

It is used by fcl-web. On linux, it loads /etc/mime.types.
On windows not yet, I must still look up how to do this.

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Felipe Monteiro de Carvalho
In reply to this post by wkitty42
On Wed, May 9, 2012 at 2:46 PM, waldo kitty <[hidden email]> wrote:
> wouldn't it be better to use mimemagic instead of relying on the file's
> extension?

You mean opening the file and reading the first bytes? For my use case
that's an over complication, and subject to multiple possible errors:
Permission errors for example. Not really nice.

I am implementing LCLIntf.OpenDocument and LCLIntf.OpenURL for
Android, and here it might be good to just let whatever app that is
choosen handle the file and never open it myself.

> i can easily name foo.gif as foo.txt and those systems that do properly look

Though luck of whoever changed the extension. That makes choosing the
file in dialogs also problematic. I don't care about this use case.

--
Felipe Monteiro de Carvalho

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Felipe Monteiro de Carvalho
In reply to this post by michael.vancanneyt
On Wed, May 9, 2012 at 2:51 PM,  <[hidden email]> wrote:
> FPC contains this unit:
> fpmimetypes.pp
> It is used by fcl-web. On linux, it loads /etc/mime.types.
> On windows not yet, I must still look up how to do this.

I just opened "adb shell" and there is no such file in my HTC Wildfire.

I'd really like a standard list here. As less system-dependent as possible.

--
Felipe Monteiro de Carvalho

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

michael.vancanneyt


On Wed, 9 May 2012, Felipe Monteiro de Carvalho wrote:

> On Wed, May 9, 2012 at 2:51 PM,  <[hidden email]> wrote:
>> FPC contains this unit:
>> fpmimetypes.pp
>> It is used by fcl-web. On linux, it loads /etc/mime.types.
>> On windows not yet, I must still look up how to do this.
>
> I just opened "adb shell" and there is no such file in my HTC Wildfire.
>
> I'd really like a standard list here. As less system-dependent as possible.

Distribute a mime.types from a unix box with your app
and load this in the fpmimetypes unit using the provided function.

That's what I do on Windows. By far the easiest solution :)

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Felipe Monteiro de Carvalho
On Wed, May 9, 2012 at 4:15 PM,  <[hidden email]> wrote:
> Distribute a mime.types from a unix box with your app and load this in the
> fpmimetypes unit using the provided function.
> That's what I do on Windows. By far the easiest solution :)

My code is the LCL, so I cannot do that without forcing all
LCL-CustomDrawn-Android users to deploy this file. I think I will
steal the code from TurboPower iPro and put it somewhere in LazUtils.

--
Felipe Monteiro de Carvalho

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Graeme Geldenhuys
In reply to this post by wkitty42
On 9 May 2012 14:46, waldo kitty <[hidden email]> wrote:
>
> wouldn't it be better to use mimemagic instead of relying on the file's
> extension?
>
> i can easily name foo.gif as foo.txt and those systems that do properly look

+1

Windows and Mac OS X fails that test. Linux rules in that regards. :)

Side note:
Sad thing is, I believe Mac OS 9 and earlier used to use mimemagic,
but with OS X they simply look at the file extension - dumb move.

--
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://fpgui.sourceforge.net

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Graeme Geldenhuys
In reply to this post by michael.vancanneyt
On 9 May 2012 14:51,  <[hidden email]> wrote:
>
> It is used by fcl-web. On linux, it loads /etc/mime.types.

I don't know the exact format of that file, I guessed "mime type name"
then "file extension list"

But on my Ubuntu 10.04 that file has many lines only showing the "mime
type name" with no extension mentioned. So how reliable is that file
really?

I know Linux (at least Gnome) uses magic numbers (first 2-4 bytes of a
file) to detect the real file type without looking at the file
extension. These magic numbers information is stored somewhere in
linux (can't remember where now), and it includes the magic number and
mime type mapping. I'll see if I can find the file I am talking about.
But as with everything Linux, this is probably highly distro specific
etc.


--
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://fpgui.sourceforge.net

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Graeme Geldenhuys
In reply to this post by michael.vancanneyt
On 9 May 2012 16:15,  <[hidden email]> wrote:
>
> That's what I do on Windows. By far the easiest solution :)

Ingenious solution. :-)


--
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://fpgui.sourceforge.net

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

Henry Vermaak
In reply to this post by Graeme Geldenhuys
On 09/05/12 15:47, Graeme Geldenhuys wrote:
> I know Linux (at least Gnome) uses magic numbers (first 2-4 bytes of a
> file) to detect the real file type without looking at the file
> extension. These magic numbers information is stored somewhere in
> linux (can't remember where now), and it includes the magic number and
> mime type mapping. I'll see if I can find the file I am talking about.
> But as with everything Linux, this is probably highly distro specific
> etc.

/usr/share/misc/magic.mgc (which on my system is a symlink to somewhere
else).  It's a big file: 1.8MB.

Linking to libmagic will get you most the functionality of the `file`
command, afaik.

Henry

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

michael.vancanneyt
In reply to this post by Graeme Geldenhuys


On Wed, 9 May 2012, Graeme Geldenhuys wrote:

> On 9 May 2012 14:51,  <[hidden email]> wrote:
>>
>> It is used by fcl-web. On linux, it loads /etc/mime.types.
>
> I don't know the exact format of that file, I guessed "mime type name"
> then "file extension list"

Correct.

>
> But on my Ubuntu 10.04 that file has many lines only showing the "mime
> type name" with no extension mentioned. So how reliable is that file
> really?
>

Based on my personal experience: very.
I've had this file in use since 18 years at least, since the first NCSA
webserver installations.

> I know Linux (at least Gnome) uses magic numbers (first 2-4 bytes of a
> file) to detect the real file type without looking at the file
> extension.

A webserver does not do that. It uses the mime file.

the 'file' command looks at the magic numbers, but for example KDE looks at
the extension.

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

michael.vancanneyt
In reply to this post by Felipe Monteiro de Carvalho


On Wed, 9 May 2012, Felipe Monteiro de Carvalho wrote:

> On Wed, May 9, 2012 at 4:15 PM,  <[hidden email]> wrote:
>> Distribute a mime.types from a unix box with your app and load this in the
>> fpmimetypes unit using the provided function.
>> That's what I do on Windows. By far the easiest solution :)
>
> My code is the LCL, so I cannot do that without forcing all
> LCL-CustomDrawn-Android users to deploy this file. I think I will
> steal the code from TurboPower iPro and put it somewhere in LazUtils.

On windows it looks in the registry. Good luck with that on Android :-)

For other OSes there are about 4 or 5 mime types defined. Not really useful.

So my suggestion is probably still the best. I don't see why you'd need to
force this on all users, only the ones that need mime types need the file ?

Or you could convert it to an include file that just registers all more or
less known types in fpmimetypes.pp. Then people can include this include
file and be done with it.

Michael.

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

wkitty42
In reply to this post by Graeme Geldenhuys
On 5/9/2012 10:37, Graeme Geldenhuys wrote:

> On 9 May 2012 14:46, waldo kitty<[hidden email]>  wrote:
>>
>> wouldn't it be better to use mimemagic instead of relying on the file's
>> extension?
>>
>> i can easily name foo.gif as foo.txt and those systems that do properly look
>
> +1
>
> Windows and Mac OS X fails that test. Linux rules in that regards. :)

yeah, i remember when we were playing with this back when fidonet was in its
heyday... many in the programming echos were looking at this for their tools and
apps they were writing :)

> Side note:
> Sad thing is, I believe Mac OS 9 and earlier used to use mimemagic,
> but with OS X they simply look at the file extension - dumb move.

definitely a dumb move... especially when tricks like using the wrong extension
are used in malware, trojan and virus attacks...

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

wkitty42
In reply to this post by Felipe Monteiro de Carvalho
On 5/9/2012 09:59, Felipe Monteiro de Carvalho wrote:
> On Wed, May 9, 2012 at 2:46 PM, waldo kitty<[hidden email]>  wrote:
>> wouldn't it be better to use mimemagic instead of relying on the file's
>> extension?
>
> You mean opening the file and reading the first bytes?

yes, it is the best way... but not all are at the beginning, either...

> For my use case that's an over complication, and subject to multiple
> possible errors:
> Permission errors for example. Not really nice.

true... but then the user wouldn't have access to those files, either... correct?

> I am implementing LCLIntf.OpenDocument and LCLIntf.OpenURL for
> Android, and here it might be good to just let whatever app that is
> choosen handle the file and never open it myself.

ahhh, yes... that would be the easiest...

>> i can easily name foo.gif as foo.txt and those systems that do properly look
>
> Though luck of whoever changed the extension. That makes choosing the
> file in dialogs also problematic. I don't care about this use case.

understood... it has been used in exploits, though... especially in the
winwhatever world and is still used quite regularly... those of us in the
security world have network traffic sniffers specifically looking for traffic
that says it is one mime type yet the file extension says it is another and
finally the magic numbers say it is something else altogether... this traffic is
definitely red flag traffic and warrants a deeper look...


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Function to get the mimetype from a file extension

silvioprog
Please see "MimeTypeFromExt" function in Synapse project:

https://synalist.svn.sourceforge.net/svnroot/synalist/trunk/mimepart.pas

--
Silvio Clécio
============================================
Site - <silvioprog.com.br>
LazSolutions - <code.google.com/p/lazsolutions>
LazWebSolutions - <github.com/silvioprog/lazwebsolutions>
============================================
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
12