]> Untitled Git - public.git/blob - lua/publish.lua
Hotfix 1.3.1
[public.git] / lua / publish.lua
1 require "apache2"
2
3 function publish(dir, pattern)
4     local cmd = ("%s/publish.sh '%s'"):format(dir, pattern)
5     local handle = io.popen(cmd)
6     local output = handle:read('*a')
7     handle:close()
8
9     return output
10 end
11
12 function get_form(dir)
13     local handle = io.open(dir .. '/submit.html')
14     local output = handle:read('*a')
15     handle:close()
16
17     return output;
18 end
19
20 function handle(r)
21     r.content_type = "text/html"
22
23     local args = r:parseargs()
24     local pattern = args.pattern
25
26     if pattern then
27         -- filename might have spaces that apache replaces with '+'
28         pattern = string.gsub(pattern, '+', ' ')
29
30         local nonce = publish(r.context_document_root, pattern)
31         if nonce == "" then
32             r:puts( "No links created" )
33         else
34             -- if FQDN override is set,
35             -- use override instead of my own server name and port number
36             local fqdn = os.getenv('PUBLIC_FQDN_OVERRIDE')
37             fqdn = fqdn or
38                 (r.is_https and "https" or "http") ..
39                 "://" .. r.server_name .. ":" ..r.port
40
41             local uri = ("%s/%s/"):format(fqdn, nonce)
42             r:puts( ([[<a href="%s">%s</a>]]):format(uri, uri) )
43         end
44     else
45         local template = get_form(r.context_document_root)
46         r:puts( template )
47     end
48
49     return apache2.OK
50 end