MIME functions
chm.GetMimeType
This function returns MIME type of a given filename.
CREATE FUNCTION [chm].[GetMimeType]
(
@filename NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
@filename
can be both a filename or a filename together with its path.
Example:
select chm.GetMimeType('flower.jpg') -- image/jpeg
select chm.GetMimeType('/docs/readme.txt') -- text/plain
select chm.GetMimeType('c:\projects\my-app\index.js') -- application/javascript
chm.GetExtension
This function returns extension of a given filename (without a dot character).
CREATE FUNCTION [chm].[GetExtension]
(
@filenameOrExtension NVARCHAR(MAX)
)
RETURNS NVARCHAR (MAX)
@filename
can be both a filename or a filename together with its path.
Example:
select chm.GetExtension('flower.jpg') -- jpg
select chm.GetExtension('/docs/readme.txt') -- txt
select chm.GetExtension('c:\projects\my-app\index.js') -- js
chm.GetExtension
This function returns extension of a given filename (without a dot character).
CREATE FUNCTION [chm].[GetExtension]
(
@filenameOrExtension NVARCHAR(MAX)
)
RETURNS NVARCHAR (MAX)
@filename
can be both a filename or a filename together with its path.
Example:
select chm.GetExtension('flower.jpg') -- jpg
select chm.GetExtension('/docs/readme.txt') -- txt
select chm.GetExtension('c:\projects\my-app\index.js') -- js
Mime Tables
In addition to chm.GetMimeType()
that refers to an array of MIME types located in the Chameleon
assemblies which are not editable (unless by editing Chameleon
source code, recompiling and reinstalling the SQLCLR assemblies, which is cumbersome), list of MIME values are also provided as tables in the database, so that user can add/edit them if he desires so.
chameleon.Mimes
This table holds MIME groups. Each group can span multiple file extensions.
Column | Type | Description |
---|---|---|
id
|
smallint
|
PK |
value
|
varchar(85)
|
MIME value |
source
|
varchar(15)
|
Source |
compressible
|
bit
|
Whether or not related content is compressible |
charset
|
varchar(10)
|
Character-set |
extensions
|
varchar(150)
|
Related file extensions |
chameleon.MimeTypes
This table holds file extensions of MIME values.
Column | Type | Description |
---|---|---|
id
|
smallint
|
PK |
mimeId
|
smallint
|
FK to chameleon.Mimes(id) |
extension
|
varchar(20)
|
Fle extension |
isDefault
|
bit
|
Is default file extension of MIME or not |
chameleon.GetMimeType
This is the non-CLR version of chm.GetMimeType
that introduced earlier and performs the same thing.
CREATE FUNCTION [chameleon].[GetMimeType]
(
@filename NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
It refers to chameleon.Mimes
and chameleon.MimeTypes
tables in order to find MIME of given file extension, whereas chm.GetMimeType()
refers to the in-memory array of Mime objects in the Chameleon
s compiled assemblies.
Example:
select [chameleon].[GetMimeType]('.jpg')
Output:
image/jpeg
The benefit of using chameleon.GetMimeType()
over chm.GetMimeType()
is that, MIME tables could be easily manipulated (more MIMEs added to them), but MIME array inside Chameleon
assemblies could not be easily manipulated.