| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LWDirectoryService.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 LWFileService = require( "LWFileService" ) |
|---|
| 12 |
local LWLink = require( "LWLink" ) |
|---|
| 13 |
local LWResponse = require( "LWResponse" ) |
|---|
| 14 |
local LWString = require( "LWString" ) |
|---|
| 15 |
local LWTemplate = require( "LWTemplate" ) |
|---|
| 16 |
local MIMEType = require( "MIMEType" ) |
|---|
| 17 |
local LUBundle = require( "LUBundle" ) |
|---|
| 18 |
local LUFormat = require( "LUFormat" ) |
|---|
| 19 |
local LUTask = require( "LUTask" ) |
|---|
| 20 |
local LFSFile = require( "LFSFile" ) |
|---|
| 21 |
local LUURI = require( "LUURI" ) |
|---|
| 22 |
local os = require( "os" ) |
|---|
| 23 |
local math = require( "math" ) |
|---|
| 24 |
|
|---|
| 25 |
-- define the class |
|---|
| 26 |
local super = LWService |
|---|
| 27 |
local self = super() |
|---|
| 28 |
|
|---|
| 29 |
-- class variable(s) |
|---|
| 30 |
local _template = nil |
|---|
| 31 |
|
|---|
| 32 |
-- initialization method |
|---|
| 33 |
function self:init( aPrefix, aPath, anAuthenticator ) |
|---|
| 34 |
super.init( self, aPrefix, anAuthenticator ) |
|---|
| 35 |
|
|---|
| 36 |
self._path = aPath |
|---|
| 37 |
|
|---|
| 38 |
return self |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
-- method to access this service directory |
|---|
| 42 |
function self:directory() |
|---|
| 43 |
if self._directory == nil then |
|---|
| 44 |
self._directory = LFSFile( self:path() ) |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
return self._directory |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
-- method to access this file service |
|---|
| 51 |
function self:fileService() |
|---|
| 52 |
if self._fileService == nil then |
|---|
| 53 |
local aPrefix = self:prefix() |
|---|
| 54 |
local aPath = self:path() |
|---|
| 55 |
local anAuthenticator = self:authenticator() |
|---|
| 56 |
|
|---|
| 57 |
self._fileService = LWFileService( aPrefix, aPath, anAuthenticator ) |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
return self._fileService |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
-- method to access this service path |
|---|
| 64 |
function self:path() |
|---|
| 65 |
return self._path |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
-- method to handle a GET method |
|---|
| 69 |
function self:handleGet( aContext, hasContent ) |
|---|
| 70 |
local aPath = self:pathWithContext( aContext ) |
|---|
| 71 |
|
|---|
| 72 |
if aPath == nil then |
|---|
| 73 |
local aDirectory = self:directory() |
|---|
| 74 |
local aPath = aDirectory:path() |
|---|
| 75 |
local lastModified = aDirectory:lastModified() |
|---|
| 76 |
local aTemplate = self:template() |
|---|
| 77 |
local aCount = 0 |
|---|
| 78 |
|
|---|
| 79 |
aTemplate:put( "encoding", LWResponse:encoding() ) |
|---|
| 80 |
aTemplate:put( "name", LWString:encode( aDirectory:pathComponents():last() ) ) |
|---|
| 81 |
|
|---|
| 82 |
for aFileName in aDirectory:iterator( LWFileService:filter() ) do |
|---|
| 83 |
local aFileTemplate = aTemplate:get( "files" ) |
|---|
| 84 |
local aFile = LFSFile( aPath, aFileName ) |
|---|
| 85 |
local lastModified = aFile:lastModified() |
|---|
| 86 |
local aLink = self:prefix() .. aFileName |
|---|
| 87 |
|
|---|
| 88 |
aFileTemplate:put( "size", LUFormat:formatNumber( math.ceil( aFile:size() / 1024 ) ) ) |
|---|
| 89 |
aFileTemplate:put( "date", os.date( "%x", lastModified ) ) |
|---|
| 90 |
aFileTemplate:put( "link", LWLink:encode( aLink ) ) |
|---|
| 91 |
aFileTemplate:put( "type", MIMEType:typeWithExtension( aFile:extension() ) ) |
|---|
| 92 |
aFileTemplate:put( "name", LWString:encode( aFileName ) ) |
|---|
| 93 |
|
|---|
| 94 |
aTemplate:put( "files", aFileTemplate ) |
|---|
| 95 |
aCount = aCount + 1 |
|---|
| 96 |
|
|---|
| 97 |
LUTask:yield() |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
aTemplate:put( "size", LUFormat:formatNumber( aCount ) ) |
|---|
| 101 |
|
|---|
| 102 |
aContext:response():headers():put( "etag", aDirectory:id() ) |
|---|
| 103 |
aContext:response():headers():put( "last-modified", LWResponse:stringWithTime( lastModified ) ) |
|---|
| 104 |
aContext:response():writeContent( aTemplate:toString(), hasContent ) |
|---|
| 105 |
else |
|---|
| 106 |
self:fileService():handleGet( aContext, hasContent ) |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
return self |
|---|
| 110 |
end |
|---|
| 111 |
|
|---|
| 112 |
-- method to access the directory template |
|---|
| 113 |
function self:template() |
|---|
| 114 |
if _template == nil then |
|---|
| 115 |
local aBundle = LUBundle:bundleWithName( "LWDirectoryService" ) |
|---|
| 116 |
local aFile = aBundle:fileWithName( "LWDirectoryService", "txt" ) |
|---|
| 117 |
|
|---|
| 118 |
_template = LWTemplate( aFile:content() ) |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
return _template:copy() |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
return self |
|---|