diff --git a/web/repo.go b/web/repo.go index 5e721db..6f6f199 100644 --- a/web/repo.go +++ b/web/repo.go @@ -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 + }) +} diff --git a/web/templates/map-version.html b/web/templates/map-version.html index 66bb683..fc59aac 100644 --- a/web/templates/map-version.html +++ b/web/templates/map-version.html @@ -22,6 +22,7 @@ {{ .HumanDate }} + zip {{ if .HasParent }} patch {{ end }} diff --git a/web/web.go b/web/web.go index 41cd928..799d0fa 100644 --- a/web/web.go +++ b/web/web.go @@ -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()