debian-mirror-gitlab/workhorse/internal/helper/helpers.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
858 B
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package helper
import (
"errors"
"mime"
"net/url"
"os"
2023-03-04 22:38:38 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/log"
2021-02-22 17:27:13 +05:30
)
func OpenFile(path string) (file *os.File, fi os.FileInfo, err error) {
file, err = os.Open(path)
if err != nil {
return
}
defer func() {
if err != nil {
file.Close()
}
}()
fi, err = file.Stat()
if err != nil {
return
}
// The os.Open can also open directories
if fi.IsDir() {
err = &os.PathError{
Op: "open",
Path: path,
Err: errors.New("path is directory"),
}
return
}
return
}
func URLMustParse(s string) *url.URL {
u, err := url.Parse(s)
if err != nil {
2023-03-04 22:38:38 +05:30
log.WithError(err).WithFields(log.Fields{"url": s}).Fatal("urlMustParse")
2021-02-22 17:27:13 +05:30
}
return u
}
func IsContentType(expected, actual string) bool {
parsed, _, err := mime.ParseMediaType(actual)
return err == nil && parsed == expected
}