2023-08-16 11:45:22 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
don't cache archives CPU time is cheaper than storage space and it's not easy to automate cache clean-up
diff --git a/cogit/cogit.lua b/cogit/cogit.lua index 56a1afd..618f53f 100644 --- a/cogit/cogit.lua +++ b/cogit/cogit.lua @@ -104,24 +104,13 @@ function Cogit:is_admin(cookies) return self.sessions[session_id] end -function Cogit:archive(rname, rev, fmt) - local arc_path = "/tmp/cogit-archive/" - os.execute("mkdir -p "..arc_path) +function Cogit:archive(rname, rev, fname) local aname = rname.."-"..rev - local fname = aname.."."..fmt - -- check if file already exists - local fp = io.open(arc_path..fname) - if fp == nil then - local cmd = "GIT_DIR='"..self.path.."/"..rname..".git' " - cmd = cmd.."git archive --prefix="..aname.."/" - cmd = cmd.." -o "..arc_path..fname.." "..rev - os.execute(cmd) - self:log(LOG_INFO, "generated archive "..fname) - else - self:log(LOG_INFO, "using cached archive "..fname) - fp:close() - end - return arc_path, fname + local cmd = "GIT_DIR='"..self.path.."/"..rname..".git' " + cmd = cmd.."git archive --prefix="..aname.."/" + cmd = cmd.." --format=tar.gz -o '"..fname.."' "..rev + os.execute(cmd) + self:log(LOG_INFO, "generated archive for "..aname) end function Cogit:routes() @@ -190,7 +179,7 @@ function Cogit:routes() local tnames = repo:tags() local env = { title=self.title, is_admin=is_admin, repo=repo, - rname=rname, bnames=bnames, tnames=tnames, fmt=self.archive_fmt, + rname=rname, bnames=bnames, tnames=tnames, } return {fname="view/repo.html", env=env} end}, @@ -253,12 +242,18 @@ function Cogit:routes() if commit == nil then return "Page not found", 404, "Not found" end - local path, fname = self:archive(rname, cid, self.archive_fmt) + local fname = os.tmpname() + self:archive(rname, cid, fname) + local dname = rname.."-"..cid..".tar.gz" local headers = { ["Content-Type"] = 'application/gzip', - ["Content-Disposition"] = 'attachment; filename="'..fname..'"', + ["Content-Disposition"] = 'attachment; filename="'..dname..'"', } - return {fname=path..fname, headers=headers} + local fp = io.open(fname) + local data = fp:read("*a") + fp:close() + os.remove(fname) + return {data=data, headers=headers} end}, {"GET", "/repo/([%w_-]+)/commit/([%w_-]+)/tree/(.*)", function (req, rname, cid, path) @@ -288,7 +283,7 @@ function Cogit:routes() end local env = { title=self.title, is_admin=is_admin, rname=rname, cid=cid, - path=path, base=base, parts=parts, node=node, fmt=self.archive_fmt, + path=path, base=base, parts=parts, node=node, } if node.type_ == "dir" then return {fname="view/dir.html", env=env} @@ -308,7 +303,6 @@ local function new_cogit(path, port, title, log_level) limit=20, session_age=3*24*60*60, sessions={}, - archive_fmt="tar.gz", initialized=false, } self = setmetatable(self, Cogit) diff --git a/view/dir.html b/view/dir.html index 7256e56..494b9d0 100644 --- a/view/dir.html +++ b/view/dir.html @@ -17,7 +17,7 @@ <a href="/repo/{{$rname}}/commit/{{$cid}}">{{$cid}}</a> > % if #$parts == 0 then - % set archive_name = $rname.."-"..$cid.."."..$fmt + % set archive_name = $rname.."-"..$cid..".tar.gz" <a href="/repo/{{$rname}}/archive/{{$cid}}" download="{{$archive_name}}"> {{$archive_name}} </a> diff --git a/view/repo.html b/view/repo.html index 0fd82e9..9485beb 100644 --- a/view/repo.html +++ b/view/repo.html @@ -26,7 +26,7 @@ % for bname in $bnames do <tr> <td><a href="/repo/{{$rname}}/history/{{$bname}}">{{$bname}}</a></td> - % set archive_name = $rname.."-"..$bname.."."..$fmt + % set archive_name = $rname.."-"..$bname..".tar.gz" <td> <a href="/repo/{{$rname}}/archive/{{$bname}}" download="{{$archive_name}}"> {{$archive_name}} @@ -46,7 +46,7 @@ % set cid = $repo:commit($tname.."^{}"):id() <tr> <td><a href="/repo/{{$rname}}/commit/{{$cid}}">{{$tname}}</a></td> - % set archive_name = $rname.."-"..$tname.."."..$fmt + % set archive_name = $rname.."-"..$tname..".tar.gz" <td> <a href="/repo/{{$rname}}/archive/{{$tname}}" download="{{$archive_name}}"> {{$archive_name}}