-- -*- Coding: utf-8 -*- module Rakka.Resource.Object ( resObject ) where import Network.HTTP.Lucu import Rakka.Environment import Rakka.Page import Rakka.Storage import Rakka.SystemConfig import System.FilePath.Posix resObject :: Environment -> ResourceDef resObject env = ResourceDef { resUsesNativeThread = False , resIsGreedy = True , resGet = Just $ getPathInfo >>= handleGet env . toPageName , resHead = Nothing , resPost = Nothing , resPut = Nothing , resDelete = Nothing } where toPageName :: [String] -> PageName toPageName = UTF8.decodeString . joinPath handleGet :: Environment -> PageName -> Resource () handleGet env name = do pageM <- getPage (envStorage env) name Nothing case pageM of Nothing -> foundNoEntity Nothing Just page -> if isEntity page then handleGetEntity page else handleRedirect env page {- HTTP/1.1 302 Found Location: http://example.org/object/Destination -} handleRedirect :: Environment -> Page -> Resource () handleRedirect env redir = do BaseURI baseURI <- getSysConf (envSysConf env) redirect Found (mkObjectURI baseURI $ redirName redir) {- HTTP/1.1 200 OK Content-Type: image/png ... -} handleGetEntity :: Page -> Resource () handleGetEntity page = do case entityRevision page of 0 -> foundTimeStamp (entityLastMod page) -- 0 はデフォルトページ rev -> foundEntity (strongETag $ show rev) (entityLastMod page) setContentType (entityType page) outputLBS (entityContent page)