| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LWFileService.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LWService = require( "LWService" ) |
|---|
| 11 |
local LWResponse = require( "LWResponse" ) |
|---|
| 12 |
local LFSFile = require( "LFSFile" ) |
|---|
| 13 |
local MIMEType = require( "MIMEType" ) |
|---|
| 14 |
local LULog = require( "LULog" ) |
|---|
| 15 |
|
|---|
| 16 |
-- define the class |
|---|
| 17 |
local super = LWService |
|---|
| 18 |
local self = super() |
|---|
| 19 |
|
|---|
| 20 |
-- class variable(s) |
|---|
| 21 |
local _filter = nil |
|---|
| 22 |
|
|---|
| 23 |
-- constants |
|---|
| 24 |
local MaximumSize = 10000000 |
|---|
| 25 |
|
|---|
| 26 |
-- initialization method |
|---|
| 27 |
function self:init( aPrefix, aPath, anAuthenticator ) |
|---|
| 28 |
super.init( self, aPrefix, anAuthenticator ) |
|---|
| 29 |
|
|---|
| 30 |
self._path = aPath |
|---|
| 31 |
|
|---|
| 32 |
return self |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
-- method to access this service directory |
|---|
| 36 |
function self:directory() |
|---|
| 37 |
if self._directory == nil then |
|---|
| 38 |
self._directory = LFSFile( self:path() ) |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
return self._directory |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
-- method to access this service path |
|---|
| 45 |
function self:path() |
|---|
| 46 |
return self._path |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
-- method to handle a GET method |
|---|
| 50 |
function self:handleGet( aContext, hasContent ) |
|---|
| 51 |
local aDirectory = self:directory() |
|---|
| 52 |
local aName = self:pathWithContext( aContext ) |
|---|
| 53 |
local aFilter = self:filter() |
|---|
| 54 |
|
|---|
| 55 |
LULog:debug( aName ) |
|---|
| 56 |
|
|---|
| 57 |
if aFilter( aDirectory, aName ) == true then |
|---|
| 58 |
local aFile = LFSFile( self:path(), aName ) |
|---|
| 59 |
|
|---|
| 60 |
if aFile:size() < MaximumSize then |
|---|
| 61 |
local someHeaders = aContext:response():headers() |
|---|
| 62 |
local lastModified = aFile:lastModified() |
|---|
| 63 |
|
|---|
| 64 |
someHeaders:put( "content-type", MIMEType:typeWithExtension( aFile:extension() ) ) |
|---|
| 65 |
someHeaders:put( "etag", aFile:id() ) |
|---|
| 66 |
someHeaders:put( "last-modified", LWResponse:stringWithTime( lastModified ) ) |
|---|
| 67 |
|
|---|
| 68 |
aContext:response():writeContent( aFile:content(), hasContent ) |
|---|
| 69 |
else |
|---|
| 70 |
aContext:response():writeStatus( 413, hasContent ) |
|---|
| 71 |
end |
|---|
| 72 |
else |
|---|
| 73 |
aContext:response():writeStatus( 404, hasContent ) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
return self |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
-- method to access the service file filter for a given context |
|---|
| 80 |
function self:filter() |
|---|
| 81 |
if _filter == nil then |
|---|
| 82 |
_filter = function( aDirectory, aFileName ) |
|---|
| 83 |
if aFileName ~= nil |
|---|
| 84 |
and aFileName:sub( 1, 1 ) ~= "." |
|---|
| 85 |
and aFileName:find( "%.%." ) == nil |
|---|
| 86 |
then |
|---|
| 87 |
|
|---|
| 88 |
local aFile = LFSFile( aDirectory:path(), aFileName ) |
|---|
| 89 |
|
|---|
| 90 |
if aFile:isFile() == true then |
|---|
| 91 |
return true |
|---|
| 92 |
end |
|---|
| 93 |
end |
|---|
| 94 |
|
|---|
| 95 |
return false |
|---|
| 96 |
end |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
return _filter |
|---|
| 100 |
end |
|---|
| 101 |
|
|---|
| 102 |
return self |
|---|