func GetFileContents(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get(":name") path := r.URL.Query().Get("path") ref := r.URL.Query().Get("ref") if ref == "" { ref = "master" } if path == "" || repo == "" { err := fmt.Errorf("Error when trying to obtain file %s on ref %s of repository %s (repository and path are required).", path, ref, repo) http.Error(w, err.Error(), http.StatusBadRequest) return } contents, err := repository.GetFileContents(repo, ref, path) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } extension := filepath.Ext(path) mimeType := mime.TypeByExtension(extension) if mimeType == "" { mimeType = "text/plain; charset=utf-8" } w.Header().Set("Content-Type", mimeType) w.Header().Set("Content-Length", strconv.Itoa(len(contents))) w.Write(contents) }
func getFileContents(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get(":name") path := r.URL.Query().Get("path") ref := r.URL.Query().Get("ref") if ref == "" { ref = "master" } if path == "" { err := fmt.Errorf("Error when trying to obtain an uknown file on ref %s of repository %s (path is required).", ref, repo) http.Error(w, err.Error(), http.StatusBadRequest) return } contents, err := repository.GetFileContents(repo, ref, path) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } w.Header().Set("Content-Type", getMimeType(path, contents)) w.Header().Set("Content-Length", strconv.Itoa(len(contents))) w.Write(contents) }