Goit

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

Goit/src/util/util.go (102 lines, 1.8 KiB) -rw-r--r-- file download

ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
0
// Copyright (C) 2023, Jakob Wakeling
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
1
// All rights reserved.
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
2
d631c5e Jakob Wakeling 2023-07-20 23:13:39
3
package util
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
4
1828e5f Jakob Wakeling 2023-07-19 19:53:59
5
import (
b4b291e Jakob Wakeling 2023-12-23 17:05:01
6
	"cmp"
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
7
	"errors"
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
8
	"io/fs"
1828e5f Jakob Wakeling 2023-07-19 19:53:59
9
	"net/http"
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
10
	"os"
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
11
	"path/filepath"
1828e5f Jakob Wakeling 2023-07-19 19:53:59
12
)
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
13
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
14
const ModeNotRegular = os.ModeSymlink | os.ModeDevice | os.ModeNamedPipe | os.ModeSocket | os.ModeCharDevice |
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
15
	os.ModeIrregular
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
16
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
17
func If[T any](cond bool, a, b T) T {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
18
	if cond {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
19
		return a
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
20
	}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
21
	return b
b4b291e Jakob Wakeling 2023-12-23 17:05:01
22
}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
23
b4b291e Jakob Wakeling 2023-12-23 17:05:01
24
func Min[T cmp.Ordered](a, b T) T {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
25
	if a < b {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
26
		return a
b4b291e Jakob Wakeling 2023-12-23 17:05:01
27
	}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
28
	return b
b4b291e Jakob Wakeling 2023-12-23 17:05:01
29
}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
30
b4b291e Jakob Wakeling 2023-12-23 17:05:01
31
func Max[T cmp.Ordered](a, b T) T {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
32
	if a > b {
b4b291e Jakob Wakeling 2023-12-23 17:05:01
33
		return a
b4b291e Jakob Wakeling 2023-12-23 17:05:01
34
	}
b4b291e Jakob Wakeling 2023-12-23 17:05:01
35
	return b
e08e053 Jakob Wakeling 2023-07-18 21:31:14
36
}
e08e053 Jakob Wakeling 2023-07-18 21:31:14
37
0893c1e Jakob Wakeling 2023-07-21 17:11:15
38
/* Return the named cookie or nil if not found or invalid. */
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
39
func Cookie(r *http.Request, name string) *http.Cookie {
0893c1e Jakob Wakeling 2023-07-21 17:11:15
40
	c, err := r.Cookie(name)
0893c1e Jakob Wakeling 2023-07-21 17:11:15
41
	if err == nil && c.Valid() == nil {
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
42
		return c
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
43
	}
0893c1e Jakob Wakeling 2023-07-21 17:11:15
44
0893c1e Jakob Wakeling 2023-07-21 17:11:15
45
	return nil
ae5fc19 Jakob Wakeling 2023-07-17 21:54:54
46
}
8e4646b Jakob Wakeling 2023-07-26 00:53:25
47
2954edd Jakob Wakeling 2024-06-16 22:57:19
48
/* Return a slice of form values with the specified key. */
2954edd Jakob Wakeling 2024-06-16 22:57:19
49
func FormValues(r *http.Request, key string) []string {
2954edd Jakob Wakeling 2024-06-16 22:57:19
50
	if r.Form == nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
51
		r.ParseMultipartForm(32 << 20 /* 32 MB */)
2954edd Jakob Wakeling 2024-06-16 22:57:19
52
	}
2954edd Jakob Wakeling 2024-06-16 22:57:19
53
2954edd Jakob Wakeling 2024-06-16 22:57:19
54
	return r.Form[key]
2954edd Jakob Wakeling 2024-06-16 22:57:19
55
}
2954edd Jakob Wakeling 2024-06-16 22:57:19
56
8e4646b Jakob Wakeling 2023-07-26 00:53:25
57
func ModeString(mode uint32) string {
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
58
	s := If((mode&0o40000) != 0, "d", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
59
	s += If((mode&0o400) != 0, "r", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
60
	s += If((mode&0o200) != 0, "w", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
61
	s += If((mode&0o100) != 0, "x", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
62
	s += If((mode&0o040) != 0, "r", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
63
	s += If((mode&0o020) != 0, "w", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
64
	s += If((mode&0o010) != 0, "x", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
65
	s += If((mode&0o004) != 0, "r", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
66
	s += If((mode&0o002) != 0, "w", "-")
6f00d17 Jakob Wakeling 2023-07-26 12:39:54
67
	s += If((mode&0o001) != 0, "x", "-")
8e4646b Jakob Wakeling 2023-07-26 00:53:25
68
	return s
8e4646b Jakob Wakeling 2023-07-26 00:53:25
69
}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
70
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
71
func DirSize(path string) (uint64, error) {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
72
	var size int64
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
73
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
74
	err := filepath.WalkDir(path, func(_ string, d fs.DirEntry, err error) error {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
75
		if err != nil {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
76
			if errors.Is(err, os.ErrNotExist) {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
77
				return nil
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
78
			}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
79
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
80
			return err
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
81
		}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
82
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
83
		if d.IsDir() {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
84
			return nil
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
85
		}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
86
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
87
		f, err := d.Info()
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
88
		if err != nil {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
89
			return err
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
90
		}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
91
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
92
		if (f.Mode() & ModeNotRegular) == 0 {
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
93
			size += f.Size()
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
94
		}
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
95
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
96
		return nil
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
97
	})
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
98
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
99
	return uint64(size), err
cfc2b79 Jakob Wakeling 2023-07-27 13:12:30
100
}
101