-
Notifications
You must be signed in to change notification settings - Fork 36
/
serve_file.moon
80 lines (63 loc) · 2.03 KB
/
serve_file.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
uri = ngx.var.request_uri
-- manifests are served by the app
return ngx.exec "/" if uri\match "/manifest[^/]*/?$"
import Users, Modules, Versions, Rocks, Manifests from require "models"
assert = (thing) ->
ngx.exit 404 unless thing
thing
should_increment = ->
if agent = ngx.var.http_user_agent
agent = agent\lower!
if agent\match"luarocks" or agent\match"luasocket" or agent\match"wget" or agent\match"curl"
true
is_rockspec = ->
(uri\match "%.rockspec$")
object = if uri\match "^/manifests"
slug = ngx.var[1]
file = ngx.var[2]
user = assert Users\find(:slug)
if is_rockspec!
unpack Versions\select [[
INNER JOIN modules
ON (modules.id = module_id and modules.user_id = ?)
WHERE rockspec_fname = ?
]], user.id, file
else
unpack Rocks\select [[
INNER JOIN versions
ON (versions.id = version_id)
INNER JOIN modules
ON (modules.id = versions.module_id and modules.user_id = ?)
WHERE rock_fname = ?
]], user.id, file
else
file = ngx.var[1]
manifest = Manifests\root!
-- TODO: do this with less complex query
if is_rockspec!
unpack Versions\select [[
INNER JOIN manifest_modules
ON (manifest_modules.module_id = versions.module_id and manifest_modules.manifest_id = ?)
WHERE rockspec_fname = ?
]], manifest.id, file
else
unpack Rocks\select [[
INNER JOIN versions
ON (versions.id = rocks.version_id)
INNER JOIN manifest_modules
ON (manifest_modules.module_id = versions.module_id and manifest_modules.manifest_id = ?)
WHERE rock_fname = ?
]], manifest.id, file
assert object
if object.increment_download and should_increment!
object\increment_download!
url, untrusted = object\url!
if untrusted
-- trailing slash required for domain urls
unless url\match "//.-/"
url ..= "/"
assert object.content_type, "external url must provide content type"
if object.content_type
ngx.header.content_type = object\content_type!
ngx.header["x-object_url"] = url
ngx.var._url = url