Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some old lighttpd lua files
-- jpeg quality
quality = "85"
-- docroot of the images
docroot = "/var/www"
-- list of all sizes used
map_sizes = "sml"
-- map of prefixes : sizes to width/height - if the size doesn't exist, an error will thrown
map = {
["user:s"] = {60, 60},
["user:m"] = {150, 0},
["album:s"] = {60, 60},
["album:m"] = {160, 120},
["album:l"] = {480, 0}
}
function check_header(st)
last_modified = os.date("%a, %d %b %Y %H:%M:%S GMT", st["st_mtime"])
lighty.header["Last-Modified"] = last_modified
lighty.header["ETag"] = st["etag"]
ts = lighty.env["request.time"] + 60 * 60 * 24 * 30
-- HTTP 1.0
lighty.header["Expires"] = os.date("%a, %d %b %Y %H:%M:%S GMT", ts)
-- HTTP 1.1
lighty.header["Cache-Control"] = "max-age=" .. ts
if nil == lighty.request["If-None-Match"] and nil == lighty.request["If-Modified-Since"] then
return 0
end
if nil ~= lighty.request["If-None-Match"] and lighty.request["If-None-Match"] == st["etag"] then
return 304
end
if nil ~= lighty.request["If-Modified-Since"] and lighty.request["If-Modified-Since"] == last_modified then
return 304
end
return 0
end
st = lighty.stat(lighty.env["physical.path"])
if nil ~= st then
x = check_header(st)
if x ~= 0 then
return x
end
else
original = string.gsub(lighty.env["physical.path"], "([%w/]+)-[" .. map_sizes .. "]\.", "%1.")
if original == lighty.env["physical.path"] or nil == lighty.stat(original) then
return 404
else
key = string.gsub(lighty.env["physical.path"], "^" .. docroot .. "/(%a+)/.*-([" .. map_sizes .. "])\.jpg$", "%1:%2")
if nil == map[key] then
return 404
end
-- Generate the thumbnail using imagemagick
if map[key][2] > 0 then
os.execute("convert " .. original .. "[0] -quality " .. quality .. " -resize x" .. (map[key][2] * 2) .. " -resize '" .. (map[key][1] * 2) .. "x<' -resize 50% -gravity center -crop " .. map[key][1] .. "x" .. map[key][2] .. "+0+0 +repage " .. lighty.env["physical.path"])
else
os.execute("convert " .. original .. "[0] -resize " .. map[key][1] .. "x " .. lighty.env["physical.path"])
end
st = lighty.stat(lighty.env["physical.path"])
if nil == st then
return 501
end
x = check_header(st)
end
end
etag = hashme(lighty.env["request.uri"])
if nil ~= lighty.request["If-None-Match"] and lighty.request["If-None-Match"] == lighty.header["Etag"] then
return 304
end
-- Initialize some variables
prefix = "/var/www/"
cache = "/var/cache/" .. etag
types = { scripts = "text/javascript", style = "text/css"}
extensions = { scripts = "js", style = "css" }
-- Set Etag Header - yes we use Etags.
lighty.header["Etag"] = "\"" .. etag .. "\""
-- Check gzip version
gzip = false
if nil ~= lighty.request["Accept-Encoding"] then
gzip = nil ~= string.find(lighty.request["Accept-Encoding"], "gzip")
end
-- Retrieve type and send the appropriate header
type = string.match(lighty.env["request.uri"], "^/(%w+)/.*")
lighty.header["Content-Type"] = types[type]
-- If file does not exist
if nil == lighty.stat(cache) then
exec = ""
files = string.gsub(lighty.env["uri.path"], "^/" .. type, "")
for a in string.gmatch(files, "(.-[.]" .. extensions[type] .. "),?") do
if nil == lighty.stat(prefix .. type .. a) then
print("NOT FOUND: " .. prefix .. type .. a)
return 404
end
exec = exec .. prefix .. type .. a .. " "
end
if "" == exec then
print("NO VALID FILE: " .. lighty.env["uri.path"])
return 404
end
-- Use cat to merge files (fastest method so far)
os.execute("/bin/cat " .. exec .. ">" .. cache)
-- Optimize with third party applications
if type == "style" then
os.execute("/usr/bin/csstidy " .. cache .. " --remove_last_\\;=true --silent=true --template=high " .. cache)
else
os.execute("/usr/local/java/bin/java -jar /usr/local/java/yuicompressor.jar --charset utf-8 --type js " .. cache .. " -o " .. cache)
end
-- Generate gzip version
os.execute("/bin/gzip -c " .. cache .. " > " .. cache .. ".z")
end
-- Get request time using magnet patch (or use general way of lua...this is optimized to save the time(NULL)
ts = lighty.env["request.time"] + 60 * 60 * 24 * 30
-- HTTP 1.0
lighty.header["Expires"] = os.date("%a, %d %b %Y %H:%M:%S GMT", ts)
-- HTTP 1.1
lighty.header["Cache-Control"] = "max-age=" .. ts
-- Decide what version we send to the client
if gzip then
lighty.header["Content-Encoding"] = "gzip"
lighty.env["physical.path"] = cache .. ".z"
else
lighty.env["physical.path"] = cache
end
@HemnSala

This comment has been minimized.

Copy link

HemnSala commented Oct 30, 2017

Hi
how can i use this project after download all source program ?
please tell us step by step
thank you for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.