11module Hyper.Node.FileServer (fileServer ) where
22
33import Prelude
4- import Node.Buffer as Buffer
5- import Node.Path as Path
4+
65import Control.IxMonad (ibind , (:>>=))
76import Control.Monad.Aff.Class (liftAff , class MonadAff )
87import Control.Monad.Eff.Class (liftEff )
8+ import Data.Array (last )
9+ import Data.Map (Map , fromFoldable , lookup )
10+ import Data.Maybe (maybe )
11+ import Data.String (Pattern (..), split )
912import Data.Tuple (Tuple (Tuple))
1013import Hyper.Conn (Conn )
1114import Hyper.Middleware (Middleware , lift' )
@@ -14,10 +17,101 @@ import Hyper.Request (class Request, getRequestData)
1417import Hyper.Response (class ResponseWritable , class Response , ResponseEnded , StatusLineOpen , end , headers , send , toResponse , writeStatus )
1518import Hyper.Status (statusOK )
1619import Node.Buffer (BUFFER , Buffer )
20+ import Node.Buffer as Buffer
1721import Node.FS (FS )
1822import Node.FS.Aff (readFile , stat , exists )
1923import Node.FS.Stats (isDirectory , isFile )
2024import Node.Path (FilePath )
25+ import Node.Path as Path
26+
27+
28+ htaccess :: Map String String
29+ htaccess = fromFoldable $
30+ [ Tuple " aab" " application/x-authorware-bin"
31+ , Tuple " aam" " application/x-authorware-map"
32+ , Tuple " aas" " application/x-authorware-seg"
33+ , Tuple " asc" " text/plain"
34+ , Tuple " asf" " video/x-ms-asf"
35+ , Tuple " asp" " text/html"
36+ , Tuple " asx" " video/x-ms-asf"
37+ , Tuple " avi" " application/octet-stream"
38+ , Tuple " awk" " text/plain"
39+ , Tuple " bash" " text/plain"
40+ , Tuple " bsh" " text/plain"
41+ , Tuple " bz2" " application/octet-stream"
42+ , Tuple " c" " text/plain"
43+ , Tuple " cgi" " text/plain"
44+ , Tuple " chm" " application/octet-stream"
45+ , Tuple " class" " application/x-java-applet"
46+ , Tuple " csh" " text/plain"
47+ , Tuple " css" " text/css"
48+ , Tuple " csv" " application/vnd.ms-excel"
49+ , Tuple " dcr" " application/x-director"
50+ , Tuple " dir" " application/x-director"
51+ , Tuple " dmg" " application/octet-stream"
52+ , Tuple " dxr" " application/x-director"
53+ , Tuple " exe" " application/octet-stream"
54+ , Tuple " fgd" " application/x-director"
55+ , Tuple " fh" " image/x-freehand"
56+ , Tuple " fh4" " image/x-freehand"
57+ , Tuple " fh5" " image/x-freehand"
58+ , Tuple " fh7" " image/x-freehand"
59+ , Tuple " fhc" " image/x-freehand"
60+ , Tuple " flv" " video/x-flv"
61+ , Tuple " gawk" " text/plain"
62+ , Tuple " gtar" " application/x-gtar"
63+ , Tuple " gz" " application/x-gzip"
64+ , Tuple " h" " text/plain"
65+ , Tuple " ico" " image/vnd.microsoft.icon"
66+ , Tuple " in" " text/plain"
67+ , Tuple " ini" " text/plain"
68+ , Tuple " m3u" " audio/x-mpegurl"
69+ , Tuple " md5" " text/plain"
70+ , Tuple " mov" " application/octet-stream"
71+ , Tuple " mov" " video/quicktime"
72+ , Tuple " mp4" " application/octet-stream"
73+ , Tuple " mpg" " application/octet-stream"
74+ , Tuple " msi" " application/octet-stream"
75+ , Tuple " nawk" " text/plain"
76+ , Tuple " pdb" " application/x-pilot"
77+ , Tuple " pdf" " application/pdf"
78+ , Tuple " phps" " application/x-httpd-php-source"
79+ , Tuple " pl" " text/plain"
80+ , Tuple " prc" " application/x-pilot"
81+ , Tuple " py" " text/plain"
82+ , Tuple " qt" " video/quicktime"
83+ , Tuple " ra" " audio/vnd.rn-realaudio"
84+ , Tuple " ram" " audio/vnd.rn-realaudio"
85+ , Tuple " rar" " application/x-rar-compressed"
86+ , Tuple " rm" " application/vnd.rn-realmedia"
87+ , Tuple " rpm" " audio/x-pn-realaudio-plugin"
88+ , Tuple " rv" " video/vnd.rn-realvideo"
89+ , Tuple " sh" " text/plain"
90+ , Tuple " sha" " text/plain"
91+ , Tuple " sha1" " text/plain"
92+ , Tuple " shtml" " text/html"
93+ , Tuple " svg" " image/svg+xml"
94+ , Tuple " svgz" " image/svg+xml"
95+ , Tuple " swf" " application/x-shockwave-flash"
96+ , Tuple " tgz" " application/octet-stream"
97+ , Tuple " torrent" " application/x-bittorrent"
98+ , Tuple " var" " text/plain"
99+ , Tuple " wav" " audio/x-wav"
100+ , Tuple " wax" " audio/x-ms-wax"
101+ , Tuple " wm" " video/x-ms-wm"
102+ , Tuple " wma" " audio/x-ms-wma"
103+ , Tuple " wmd" " application/x-ms-wmd"
104+ , Tuple " wmv" " video/x-ms-wmv"
105+ , Tuple " wmx" " video/x-ms-wmx"
106+ , Tuple " wmz" " application/x-ms-wmz"
107+ , Tuple " wvx" " video/x-ms-wvx"
108+ , Tuple " xbm" " image/x-xbitmap"
109+ , Tuple " xhtml" " application/xhtml+xml"
110+ , Tuple " xls" " application/octet-stream"
111+ , Tuple " xml" " text/xml"
112+ , Tuple " xrdf" " application/xrds+xml"
113+ , Tuple " zip" " application/zip"
114+ ]
21115
22116serveFile
23117 :: forall m e req res c b
@@ -32,10 +126,13 @@ serveFile
32126 (Conn req (res ResponseEnded ) c )
33127 Unit
34128serveFile path = do
129+ let
130+ ext = last $ split (Pattern " ." ) path
131+ contentType = maybe " */*" id (ext >>= flip lookup htaccess)
35132 buf <- lift' (liftAff (readFile path))
36133 contentLength <- liftEff (Buffer .size buf)
37134 _ <- writeStatus statusOK
38- _ <- headers [ Tuple " Content-Type" " */* ; charset=utf-8"
135+ _ <- headers [ Tuple " Content-Type" (contentType <> " ; charset=utf-8" )
39136 , Tuple " Content-Length" (show contentLength)
40137 ]
41138 response <- toResponse buf
0 commit comments