download a zip of any particular version

This commit is contained in:
Michael Zhang 2020-10-14 16:49:59 -05:00
parent 6dddd54e07
commit c7271d2d33
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 38 additions and 3 deletions

View file

@ -1,7 +1,9 @@
package web
import (
"archive/zip"
"errors"
"fmt"
"io"
"net/http"
"path"
@ -76,3 +78,37 @@ func (web *Web) mapPatch(c *gin.Context) {
c.String(http.StatusOK, "text/plain", patch.String())
}
func (web *Web) mapZip(c *gin.Context) {
userId := c.Param("userId")
mapId := c.Param("mapId")
hash := c.Param("hash")
repoDir := path.Join(web.config.Repos, userId, mapId)
repo, _ := git.PlainOpen(repoDir)
hashObj := plumbing.NewHash(hash)
commit, _ := repo.CommitObject(hashObj)
tree, _ := commit.Tree()
files := tree.Files()
c.Writer.Header().Set("Content-type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.zip", mapId))
c.Stream(func(w io.Writer) bool {
ar := zip.NewWriter(w)
for {
file, err := files.Next()
if err == io.EOF {
break
}
reader, _ := file.Reader()
fdest, _ := ar.Create(file.Name)
io.Copy(fdest, reader)
}
ar.Close()
return false
})
}

View file

@ -22,6 +22,7 @@
<tr>
<td><span title="{{ .Date }}">{{ .HumanDate }}</span></td>
<td>
<a href="zip/{{ .Hash }}" target="_blank">zip</a>
{{ if .HasParent }}
<a href="patch/{{ .Hash }}" target="_blank">patch</a>
{{ end }}

View file

@ -68,14 +68,12 @@ func (web *Web) Run() {
})
r.GET("/logout", web.logout)
r.GET("/login", web.login)
r.GET("/login/callback", web.loginCallback)
r.GET("/map/:userId/:mapId/versions", web.mapVersions)
r.GET("/map/:userId/:mapId/patch/:hash", web.mapPatch)
r.GET("/map/:userId/:mapId/zip/:hash", web.mapZip)
r.GET("/", func(c *gin.Context) {
beatmapSets := web.listRepos()