pages-server/server/upstream/helper.go

49 lines
1.3 KiB
Go
Raw Normal View History

2021-12-05 13:47:33 +00:00
package upstream
import (
"mime"
"path"
"strings"
2021-12-05 13:47:33 +00:00
"codeberg.org/codeberg/pages/server/gitea"
2021-12-05 13:47:33 +00:00
)
// GetBranchTimestamp finds the default branch (if branch is "") and returns the last modification time of the branch
// (or nil if the branch doesn't exist)
2022-07-27 13:39:46 +00:00
func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string) *gitea.BranchTimestamp {
if len(branch) == 0 {
2021-12-05 13:47:33 +00:00
// Get default branch
defaultBranch, err := giteaClient.GiteaGetRepoDefaultBranch(owner, repo)
if err != nil {
2021-12-05 13:47:33 +00:00
return nil
}
2022-07-27 13:39:46 +00:00
branch = defaultBranch
2021-12-05 13:47:33 +00:00
}
2022-07-27 13:39:46 +00:00
timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(owner, repo, branch)
if err != nil {
2021-12-05 13:47:33 +00:00
return nil
}
2022-07-27 13:39:46 +00:00
return timestamp
2021-12-05 13:47:33 +00:00
}
func (o *Options) getMimeTypeByExtension() string {
if o.ForbiddenMimeTypes == nil {
o.ForbiddenMimeTypes = make(map[string]bool)
}
mimeType := mime.TypeByExtension(path.Ext(o.TargetPath))
mimeTypeSplit := strings.SplitN(mimeType, ";", 2)
if o.ForbiddenMimeTypes[mimeTypeSplit[0]] || mimeType == "" {
if o.DefaultMimeType != "" {
mimeType = o.DefaultMimeType
} else {
mimeType = "application/octet-stream"
}
}
return mimeType
}
2022-07-21 19:53:22 +00:00
func (o *Options) generateUriClientArgs() (targetOwner, targetRepo, ref, resource string) {
return o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath
}