download a zip of any particular version
This commit is contained in:
parent
6dddd54e07
commit
c7271d2d33
3 changed files with 38 additions and 3 deletions
36
web/repo.go
36
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
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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 }}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue