package app import ( md "farga/app/middleware" "farga/db/types" "farga/lib/git" "fmt" "net/http" "github.com/gorilla/mux" ) func (a *App) RepoZipDownload(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) branch := vars["branch"] dbRepo, ok := r.Context().Value(md.RepoKey).(types.Repo) if !ok { http.Error(w, "Internal Server Error: Middleware did not provide repo data.", http.StatusInternalServerError) return } gitRepo, err := git.Repo(*dbRepo.UserID, dbRepo.ID, branch) if err != nil { http.Error(w, "Such a repo wasn't found.", http.StatusInternalServerError) return } repoPath := git.RepoPath(*dbRepo.UserID, dbRepo.ID) // don't waste resources if the reference is invalid. ref, err := git.ResolveHead(gitRepo.Git, branch) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-%s.zip\"", dbRepo.Name, ref.Name().Short())) // name the file err = git.ZipStream(r.Context(), repoPath, ref.Hash().String(), w) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } }