mime.anubis 2.33 KB

 *Project*                             The Anubis Project
   
 *Title*                                 MIME Types definition.
   
 *Copyright*                     Copyright (c) Alain Prouté 2005. 


 *Authors*  Alain Prouté
            David René
   

read tools/base64.anubis
read tools/basis.anubis   
read system/string.anubis
   
public type MIME:
   mime(String        type,
        String        sybtype,
        List(String)  file_extensions). 


public define Bool
  MIME x = MIME y
   =
  if x is mime(x_type, x_subtype, _) then
  if y is mime(y_type, y_subtype, _) then
  insensitive_equal(x_type, y_type) & insensitive_equal(x_subtype, y_subtype). 
   
public define List(MIME)
   known_mime_types
     = 
   [
     mime("application", "octet-stream",            [".exe"]), 
     mime("application", "x-pdf",                   [".pdf"]), 
     mime("image", "bmp",                           [".bmp"]), 
     mime("image", "gif",                           [".gif"]), 
     mime("image", "jpeg",                          [".jpg", ".jpeg"]), 
     mime("image", "png",                           [".png"]), 
     mime("image", "x-icon",                        [".ico"]), 
     mime("text", "html",                           [".html", ".htm"]), 
     mime("text", "css",                            [".css"]), 
     mime("text", "javascript",                     [".js"]), 
     mime("text", "plain",                          [".txt", ".anubis", ".c", ".h", ".y", "/Makefile"]),
     mime("text", "comma-separated-values",         [".csv"]), 
     mime("application", "msword",                  [".doc"]), 
     mime("application", "octet-stream",            [".emz", ".xml", ".mso", ".wmf", ".gz", ".rar", ".zip", ".card", ".ankh", ".adm", ".swf", ".downloaded"]), 
     mime("audio", "x-mpeg",                        [".mp3"]),
     mime("video", "x-msvideo",                     [".avi"]),
     mime("message", "rfc822",                      [".eml"]),
   ]. 

public define String
  to_String
  (
    MIME  mime_type
  ) =
  if mime_type is mime(type, subtype, _) then
  type + "/" + subtype.
   
public define String
  to_MIME_text
  (
    String  charset,
    String  text
  )=
  if length(text) > 0 then 
    with text2 = "=?"+charset+"?B?"+to_string(base64_encode(to_byte_array(text)))+"?=",
    find_and_replace(text2, implode([13,10]), implode([13,10,32]))
  else "".