--- get stat information on a path -- @param path to stat -- @param the ftype to return -- @return false if ftype or path does not exist function file_info(path, ftype) local attr = lighty.stat(path) if attr and attr[ftype] then return attr[ftype] end return false end --- Wrapper for reading a full file into a string -- @param filename Full path to the file -- @return a string with the content of the file function read_file(filename) local content = "" if file_info(filename, "is_file") then local file = io.open(filename, "r") content = file:read("*a") io.close(file) end return content end --- Wrapper for writeing content to a file -- @param filename Full path to the destionation file -- @param content The string to write function write_cache(filename, content) local file = io.open(filename, "w") file:write(content) io.close(file) end --- Concat multiple files into one file -- @param lighty lighty global variable passed to the method -- @param match The files that will be concat into a file -- @param fileExtension Do !NOT! include the dot ( . ) function combine_files(lighty, files, fileExtension) require "md5" local charset = "; charset=utf-8" local prefix = "cache-" local rootPath = lighty.env["physical.doc-root"] .. fileExtension .. "/" local concatRoot = "/tmp/cache/" local lastModified = 0 for file in string.gmatch(files, "(.-\." .. fileExtension .. "),?") do local fullPath = rootPath .. file modTime = file_info(fullPath, "st_mtime") if type(modTime) == "number" then lastModified = math.max(lastModified, modTime) end end local hash = lastModified .. "-" .. md5.sumhexa(files) lighty.header["Etag"] = '"' .. hash ..'"' local cacheFile = prefix .. hash .. '.' .. fileExtension if not file_info(concatRoot .. cacheFile, "is_file") then local content = "" for file in string.gmatch(match, "(.-\." .. fileExtension .. "),?") do content = content .. "\n" content = content .. "/**\n" content = content .. " *\n" content = content .. " * " .. file .. "\n" content = content .. " *\n" content = content .. " */\n" content = content .. "\n" content = content .. read_file(rootPath .. file) end write_cache(concatRoot .. cacheFile, content ) end lighty.env["physical.path"] = concatRoot .. cacheFile end if (not file_info(lighty.env["physical.path"], "is_file")) then css = string.match(lighty.env["physical.path"], "css/(.*\.css)") if css then return combine_files(lighty, css, "css") end js = string.match(lighty.env["physical.path"], "js/(.*\.js)") if js then return combine_files(lighty, js, "js") end end