Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/repo/raw.go (93 lines, 2.2 KiB) -rw-r--r-- file download

c2c5250 Jakob Wakeling 2023-12-12 09:13:37
0
// Copyright (C) 2023, Jakob Wakeling
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
1
// All rights reserved.
c2c5250 Jakob Wakeling 2023-12-12 09:13:37
2
d314664 Jakob Wakeling 2023-11-03 23:00:26
3
package repo
d314664 Jakob Wakeling 2023-11-03 23:00:26
4
d314664 Jakob Wakeling 2023-11-03 23:00:26
5
import (
d314664 Jakob Wakeling 2023-11-03 23:00:26
6
	"errors"
d314664 Jakob Wakeling 2023-11-03 23:00:26
7
	"io"
d314664 Jakob Wakeling 2023-11-03 23:00:26
8
	"log"
d314664 Jakob Wakeling 2023-11-03 23:00:26
9
	"net/http"
d314664 Jakob Wakeling 2023-11-03 23:00:26
10
945fd71 Jakob Wakeling 2023-11-05 15:39:31
11
	"github.com/Jamozed/Goit/src/goit"
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
12
	"github.com/go-chi/chi/v5"
d314664 Jakob Wakeling 2023-11-03 23:00:26
13
	"github.com/go-git/go-git/v5"
d314664 Jakob Wakeling 2023-11-03 23:00:26
14
	"github.com/go-git/go-git/v5/plumbing"
d314664 Jakob Wakeling 2023-11-03 23:00:26
15
	"github.com/go-git/go-git/v5/plumbing/object"
d314664 Jakob Wakeling 2023-11-03 23:00:26
16
)
d314664 Jakob Wakeling 2023-11-03 23:00:26
17
d314664 Jakob Wakeling 2023-11-03 23:00:26
18
func HandleRaw(w http.ResponseWriter, r *http.Request) {
d314664 Jakob Wakeling 2023-11-03 23:00:26
19
	auth, user, err := goit.Auth(w, r, true)
d314664 Jakob Wakeling 2023-11-03 23:00:26
20
	if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
21
		log.Println("[repo/raw]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
22
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
23
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
24
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
25
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
26
	tpath := chi.URLParam(r, "*")
d314664 Jakob Wakeling 2023-11-03 23:00:26
27
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
28
	repo, err := goit.GetRepoByName(chi.URLParam(r, "repo"))
d314664 Jakob Wakeling 2023-11-03 23:00:26
29
	if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
30
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
31
		return
778d467 Jakob Wakeling 2024-01-17 18:29:55
32
	} else if repo == nil || !goit.IsVisible(repo, auth, user) {
d314664 Jakob Wakeling 2023-11-03 23:00:26
33
		goit.HttpError(w, http.StatusNotFound)
d314664 Jakob Wakeling 2023-11-03 23:00:26
34
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
35
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
36
08dff9a Jakob Wakeling 2023-11-07 21:03:04
37
	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
d314664 Jakob Wakeling 2023-11-03 23:00:26
38
	if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
39
		log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
40
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
41
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
42
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
43
d314664 Jakob Wakeling 2023-11-03 23:00:26
44
	ref, err := gr.Head()
d314664 Jakob Wakeling 2023-11-03 23:00:26
45
	if errors.Is(err, plumbing.ErrReferenceNotFound) {
d314664 Jakob Wakeling 2023-11-03 23:00:26
46
		goit.HttpError(w, http.StatusNotFound)
d314664 Jakob Wakeling 2023-11-03 23:00:26
47
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
48
	} else if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
49
		log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
50
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
51
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
52
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
53
d314664 Jakob Wakeling 2023-11-03 23:00:26
54
	commit, err := gr.CommitObject(ref.Hash())
d314664 Jakob Wakeling 2023-11-03 23:00:26
55
	if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
56
		log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
57
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
58
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
59
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
60
3f4c3f4 Jakob Wakeling 2023-12-01 23:26:03
61
	file, err := commit.File(tpath)
d314664 Jakob Wakeling 2023-11-03 23:00:26
62
	if errors.Is(err, object.ErrFileNotFound) {
d314664 Jakob Wakeling 2023-11-03 23:00:26
63
		goit.HttpError(w, http.StatusNotFound)
d314664 Jakob Wakeling 2023-11-03 23:00:26
64
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
65
	} else if err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
66
		log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
67
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
68
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
69
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
70
d314664 Jakob Wakeling 2023-11-03 23:00:26
71
	if rc, err := file.Blob.Reader(); err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
72
		log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
73
		goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
74
		return
d314664 Jakob Wakeling 2023-11-03 23:00:26
75
	} else {
d314664 Jakob Wakeling 2023-11-03 23:00:26
76
		buf := make([]byte, min(file.Size, (10*1024*1024)))
d314664 Jakob Wakeling 2023-11-03 23:00:26
77
		if _, err := rc.Read(buf); err != nil && !errors.Is(err, io.EOF) {
d314664 Jakob Wakeling 2023-11-03 23:00:26
78
			log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
79
			goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
80
			return
d314664 Jakob Wakeling 2023-11-03 23:00:26
81
		}
d314664 Jakob Wakeling 2023-11-03 23:00:26
82
d314664 Jakob Wakeling 2023-11-03 23:00:26
83
		if _, err := w.Write(buf); err != nil {
d314664 Jakob Wakeling 2023-11-03 23:00:26
84
			log.Println("[/repo/file]", err.Error())
d314664 Jakob Wakeling 2023-11-03 23:00:26
85
			goit.HttpError(w, http.StatusInternalServerError)
d314664 Jakob Wakeling 2023-11-03 23:00:26
86
			return
d314664 Jakob Wakeling 2023-11-03 23:00:26
87
		}
b4de626 Jakob Wakeling 2023-11-16 23:06:56
88
b4de626 Jakob Wakeling 2023-11-16 23:06:56
89
		rc.Close()
d314664 Jakob Wakeling 2023-11-03 23:00:26
90
	}
d314664 Jakob Wakeling 2023-11-03 23:00:26
91
}
92