aboutsummaryrefslogtreecommitdiff
path: root/http_handle_repo_raw.go
blob: b714397e5200867003dea84c6f8f60cf1ed0716b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package forge

import (
	"fmt"
	"html/template"
	"net/http"
	"strings"

	"go.lindenii.runxiyu.org/forge/internal/git2c"
	"go.lindenii.runxiyu.org/forge/internal/misc"
)

// httpHandleRepoRaw serves raw files, or directory listings that point to raw
// files.
func (s *Server) httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params map[string]any) {
	repoName := params["repo_name"].(string)
	groupPath := params["group_path"].([]string)
	rawPathSpec := params["rest"].(string)
	pathSpec := strings.TrimSuffix(rawPathSpec, "/")
	params["path_spec"] = pathSpec

	_, repoPath, _, _, _, _, _ := s.getRepoInfo(request.Context(), groupPath, repoName, "")

	client, err := git2c.NewClient(s.Config.Git.Socket)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}
	defer client.Close()

	files, content, err := client.Cmd2(repoPath, pathSpec)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}

	switch {
	case files != nil:
		params["files"] = files
		params["readme_filename"] = "README.md"
		params["readme"] = template.HTML("<p>README rendering here is WIP again</p>") // TODO
		renderTemplate(writer, "repo_raw_dir", params)
	case content != "":
		if misc.RedirectNoDir(writer, request) {
			return
		}
		writer.Header().Set("Content-Type", "application/octet-stream")
		fmt.Fprint(writer, content)
	default:
		errorPage500(writer, params, "Unknown error fetching repo raw data")
	}
}