alist/drivers/github/util.go
KirCute_ECT 779c293f04
fix(driver): implement canceling and updating progress for putting for some drivers (#7847)
* fix(driver): additionally implement canceling and updating progress for putting for some drivers

* refactor: add driver archive api into template

* fix(123): use built-in MD5 to avoid caching full

* .

* fix build failed
2025-02-01 17:29:55 +08:00

100 lines
2.0 KiB
Go

package github
import (
"context"
"errors"
"fmt"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
"strings"
"text/template"
)
type MessageTemplateVars struct {
UserName string
ObjName string
ObjPath string
ParentName string
ParentPath string
TargetName string
TargetPath string
}
func getMessage(tmpl *template.Template, vars *MessageTemplateVars, defaultOpStr string) (string, error) {
sb := strings.Builder{}
if err := tmpl.Execute(&sb, vars); err != nil {
return fmt.Sprintf("%s %s %s", vars.UserName, defaultOpStr, vars.ObjPath), err
}
return sb.String(), nil
}
func calculateBase64Length(inputLength int64) int64 {
return 4 * ((inputLength + 2) / 3)
}
func toErr(res *resty.Response) error {
var errMsg ErrResp
if err := utils.Json.Unmarshal(res.Body(), &errMsg); err != nil {
return errors.New(res.Status())
} else {
return fmt.Errorf("%s: %s", res.Status(), errMsg.Message)
}
}
// Example input:
// a = /aaa/bbb/ccc
// b = /aaa/b11/ddd/ccc
//
// Output:
// ancestor = /aaa
// aChildName = bbb
// bChildName = b11
// aRest = bbb/ccc
// bRest = b11/ddd/ccc
func getPathCommonAncestor(a, b string) (ancestor, aChildName, bChildName, aRest, bRest string) {
a = utils.FixAndCleanPath(a)
b = utils.FixAndCleanPath(b)
idx := 1
for idx < len(a) && idx < len(b) {
if a[idx] != b[idx] {
break
}
idx++
}
aNextIdx := idx
for aNextIdx < len(a) {
if a[aNextIdx] == '/' {
break
}
aNextIdx++
}
bNextIdx := idx
for bNextIdx < len(b) {
if b[bNextIdx] == '/' {
break
}
bNextIdx++
}
for idx > 0 {
if a[idx] == '/' {
break
}
idx--
}
ancestor = utils.FixAndCleanPath(a[:idx])
aChildName = a[idx+1 : aNextIdx]
bChildName = b[idx+1 : bNextIdx]
aRest = a[idx+1:]
bRest = b[idx+1:]
return ancestor, aChildName, bChildName, aRest, bRest
}
func getUsername(ctx context.Context) string {
user, ok := ctx.Value("user").(*model.User)
if !ok {
return "<system>"
}
return user.Username
}