From 42115a4ceee5e324f6b2004aac9d17605c7d147d Mon Sep 17 00:00:00 2001 From: veypi Date: Tue, 22 Oct 2024 22:50:26 +0800 Subject: [PATCH] feat: update webda --- oa/Makefile | 16 ++++++++++ oa/api/user/user.go | 2 +- oa/builtin/fs/app.go | 32 +++++++++++++++++++ oa/builtin/fs/fs.go | 22 +++++++++++++ oa/builtin/fs/user.go | 32 +++++++++++++++++++ oa/builtin/init.go | 12 +++++-- oa/cfg/cfg.go | 7 +--- oa/{builtin => libs}/webdav/dir.html | 7 +++- oa/{builtin => libs}/webdav/dir_render.go | 0 oa/{builtin => libs}/webdav/file.go | 0 oa/{builtin => libs}/webdav/file_test.go | 0 oa/{builtin => libs}/webdav/if.go | 0 oa/{builtin => libs}/webdav/if_test.go | 0 .../webdav/internal/xml/README | 0 .../webdav/internal/xml/atom_test.go | 0 .../webdav/internal/xml/example_test.go | 0 .../webdav/internal/xml/marshal.go | 0 .../webdav/internal/xml/marshal_test.go | 0 .../webdav/internal/xml/read.go | 0 .../webdav/internal/xml/read_test.go | 0 .../webdav/internal/xml/typeinfo.go | 0 .../webdav/internal/xml/xml.go | 0 .../webdav/internal/xml/xml_test.go | 0 .../webdav/litmus_test_server.go | 0 oa/{builtin => libs}/webdav/lock.go | 0 oa/{builtin => libs}/webdav/lock_test.go | 0 oa/{builtin => libs}/webdav/prop.go | 0 oa/{builtin => libs}/webdav/prop_test.go | 0 oa/{builtin => libs}/webdav/webdav.go | 4 +-- oa/{builtin => libs}/webdav/webdav_test.go | 0 oa/{builtin => libs}/webdav/xml.go | 2 +- oa/{builtin => libs}/webdav/xml_test.go | 2 +- oa/models/app.go | 4 +-- oa/models/init.go | 3 ++ 34 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 oa/Makefile create mode 100644 oa/builtin/fs/app.go create mode 100644 oa/builtin/fs/fs.go create mode 100644 oa/builtin/fs/user.go rename oa/{builtin => libs}/webdav/dir.html (97%) rename oa/{builtin => libs}/webdav/dir_render.go (100%) rename oa/{builtin => libs}/webdav/file.go (100%) rename oa/{builtin => libs}/webdav/file_test.go (100%) rename oa/{builtin => libs}/webdav/if.go (100%) rename oa/{builtin => libs}/webdav/if_test.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/README (100%) rename oa/{builtin => libs}/webdav/internal/xml/atom_test.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/example_test.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/marshal.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/marshal_test.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/read.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/read_test.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/typeinfo.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/xml.go (100%) rename oa/{builtin => libs}/webdav/internal/xml/xml_test.go (100%) rename oa/{builtin => libs}/webdav/litmus_test_server.go (100%) rename oa/{builtin => libs}/webdav/lock.go (100%) rename oa/{builtin => libs}/webdav/lock_test.go (100%) rename oa/{builtin => libs}/webdav/prop.go (100%) rename oa/{builtin => libs}/webdav/prop_test.go (100%) rename oa/{builtin => libs}/webdav/webdav.go (99%) rename oa/{builtin => libs}/webdav/webdav_test.go (100%) rename oa/{builtin => libs}/webdav/xml.go (99%) rename oa/{builtin => libs}/webdav/xml_test.go (99%) diff --git a/oa/Makefile b/oa/Makefile new file mode 100644 index 0000000..0d8f860 --- /dev/null +++ b/oa/Makefile @@ -0,0 +1,16 @@ +# +# Makefile +# Copyright (C) 2024 veypi +# 2024-10-22 16:36 +# Distributed under terms of the GPL license. +# +cfg=~/.config/oa.yaml + +run: + @go run main.go -f ${cfg} + +dbrest: + @go run main.go -f ${cfg} db drop + @go run main.go -f ${cfg} db migrate + @go run main.go -f ${cfg} db init + diff --git a/oa/api/user/user.go b/oa/api/user/user.go index 603718e..d55b4b3 100644 --- a/oa/api/user/user.go +++ b/oa/api/user/user.go @@ -131,7 +131,7 @@ func userPost(x *rest.X) (any, error) { if opts.Icon != nil { data.Icon = *opts.Icon } else { - data.Icon = fmt.Sprintf("/media/icon/%d.jpg", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(230)) + data.Icon = fmt.Sprintf("https://public.veypi.com/img/avatar/%04d.jpg", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(220)) } if opts.Email != nil { data.Email = *opts.Email diff --git a/oa/builtin/fs/app.go b/oa/builtin/fs/app.go new file mode 100644 index 0000000..29e988d --- /dev/null +++ b/oa/builtin/fs/app.go @@ -0,0 +1,32 @@ +// +// app.go +// Copyright (C) 2024 veypi +// 2024-10-22 15:42 +// Distributed under terms of the GPL license. +// + +package fs + +import ( + "net/http" + "oa/cfg" + "oa/libs/webdav" + "os" + + "github.com/veypi/utils" + "github.com/veypi/utils/logv" +) + +func NewAppFs(prefix string) func(http.ResponseWriter, *http.Request) { + apPath := utils.PathJoin(cfg.Config.FsPath, "app") + if !utils.FileExists(apPath) { + logv.AssertError(os.MkdirAll(apPath, 0744)) + } + + client := webdav.NewWebdav(apPath) + client.Prefix = prefix + client.GenSubPathFunc = func(r *http.Request) string { + return "" + } + return client.ServeHTTP +} diff --git a/oa/builtin/fs/fs.go b/oa/builtin/fs/fs.go new file mode 100644 index 0000000..1a84627 --- /dev/null +++ b/oa/builtin/fs/fs.go @@ -0,0 +1,22 @@ +// +// fs.go +// Copyright (C) 2024 veypi +// 2024-10-22 15:51 +// Distributed under terms of the GPL license. +// + +package fs + +import ( + "net/http" + "oa/libs/webdav" +) + +func NewFs(dir_path, prefix string) *webdav.Handler { + client := webdav.NewWebdav(dir_path) + client.Prefix = prefix + client.GenSubPathFunc = func(r *http.Request) string { + return "" + } + return client +} diff --git a/oa/builtin/fs/user.go b/oa/builtin/fs/user.go new file mode 100644 index 0000000..de8bb21 --- /dev/null +++ b/oa/builtin/fs/user.go @@ -0,0 +1,32 @@ +// +// user.go +// Copyright (C) 2024 veypi +// 2024-10-22 15:49 +// Distributed under terms of the GPL license. +// + +package fs + +import ( + "net/http" + "oa/cfg" + "oa/libs/webdav" + "os" + + "github.com/veypi/utils" + "github.com/veypi/utils/logv" +) + +func NewUserFs(prefix string) func(http.ResponseWriter, *http.Request) { + tmp := utils.PathJoin(cfg.Config.FsPath, "u") + if !utils.FileExists(tmp) { + logv.AssertError(os.MkdirAll(tmp, 0744)) + } + + client := webdav.NewWebdav(tmp) + client.Prefix = prefix + client.GenSubPathFunc = func(r *http.Request) string { + return "" + } + return client.ServeHTTP +} diff --git a/oa/builtin/init.go b/oa/builtin/init.go index 0d5d5a4..3e70812 100644 --- a/oa/builtin/init.go +++ b/oa/builtin/init.go @@ -11,21 +11,27 @@ import ( "net/http" "net/http/httputil" "net/url" - "oa/builtin/webdav" + "oa/builtin/fs" + "oa/cfg" "github.com/veypi/OneBD/rest" "github.com/veypi/utils/logv" ) func Enable(app *rest.Application) { + if cfg.Config.FsPath != "" { + r := app.Router().SubRouter("fs") + r.Any("/app/*", fs.NewAppFs("/fs/app")) + r.Any("/u/*", fs.NewUserFs("/fs/u")) + } tsPorxy := httputil.NewSingleHostReverseProxy(logv.AssertFuncErr(url.Parse("http://v.v:8428"))) - fsProxy := webdav.NewWebdav("/home/v/cache/") + fsProxy := fs.NewFs("/home/v/cache/", "") app.SetMux(func(w http.ResponseWriter, r *http.Request) func(http.ResponseWriter, *http.Request) { if r.Host == "ts.oa.v" || r.Header.Get("mux") == "ts" { return tsPorxy.ServeHTTP } else if r.Host == "fs.oa.v" { - return fsProxy + return fsProxy.ServeHTTP } return nil }) diff --git a/oa/cfg/cfg.go b/oa/cfg/cfg.go index 5bf5615..48d0858 100644 --- a/oa/cfg/cfg.go +++ b/oa/cfg/cfg.go @@ -19,13 +19,8 @@ type config struct { DSN string `json:"dsn"` ID string `json:"id"` Key string `json:"key"` - MediaPath string `json:"media"` + FsPath string `json:"fs"` AccessUrl string `json:"access_url"` - Influxdb struct { - Host string `json:"host"` - Port string `json:"port"` - Token string `json:"token"` - } `json:"influxdb"` } var Config = &config{} diff --git a/oa/builtin/webdav/dir.html b/oa/libs/webdav/dir.html similarity index 97% rename from oa/builtin/webdav/dir.html rename to oa/libs/webdav/dir.html index 6e79dbb..9d2f242 100644 --- a/oa/builtin/webdav/dir.html +++ b/oa/libs/webdav/dir.html @@ -135,7 +135,12 @@ - /{{ range .path }}{{if .}}{{.}}/{{end}}{{end}} + +  / + {{ range .path }}{{if .}} + {{.}} + / + {{end}}{{end}}
diff --git a/oa/builtin/webdav/dir_render.go b/oa/libs/webdav/dir_render.go similarity index 100% rename from oa/builtin/webdav/dir_render.go rename to oa/libs/webdav/dir_render.go diff --git a/oa/builtin/webdav/file.go b/oa/libs/webdav/file.go similarity index 100% rename from oa/builtin/webdav/file.go rename to oa/libs/webdav/file.go diff --git a/oa/builtin/webdav/file_test.go b/oa/libs/webdav/file_test.go similarity index 100% rename from oa/builtin/webdav/file_test.go rename to oa/libs/webdav/file_test.go diff --git a/oa/builtin/webdav/if.go b/oa/libs/webdav/if.go similarity index 100% rename from oa/builtin/webdav/if.go rename to oa/libs/webdav/if.go diff --git a/oa/builtin/webdav/if_test.go b/oa/libs/webdav/if_test.go similarity index 100% rename from oa/builtin/webdav/if_test.go rename to oa/libs/webdav/if_test.go diff --git a/oa/builtin/webdav/internal/xml/README b/oa/libs/webdav/internal/xml/README similarity index 100% rename from oa/builtin/webdav/internal/xml/README rename to oa/libs/webdav/internal/xml/README diff --git a/oa/builtin/webdav/internal/xml/atom_test.go b/oa/libs/webdav/internal/xml/atom_test.go similarity index 100% rename from oa/builtin/webdav/internal/xml/atom_test.go rename to oa/libs/webdav/internal/xml/atom_test.go diff --git a/oa/builtin/webdav/internal/xml/example_test.go b/oa/libs/webdav/internal/xml/example_test.go similarity index 100% rename from oa/builtin/webdav/internal/xml/example_test.go rename to oa/libs/webdav/internal/xml/example_test.go diff --git a/oa/builtin/webdav/internal/xml/marshal.go b/oa/libs/webdav/internal/xml/marshal.go similarity index 100% rename from oa/builtin/webdav/internal/xml/marshal.go rename to oa/libs/webdav/internal/xml/marshal.go diff --git a/oa/builtin/webdav/internal/xml/marshal_test.go b/oa/libs/webdav/internal/xml/marshal_test.go similarity index 100% rename from oa/builtin/webdav/internal/xml/marshal_test.go rename to oa/libs/webdav/internal/xml/marshal_test.go diff --git a/oa/builtin/webdav/internal/xml/read.go b/oa/libs/webdav/internal/xml/read.go similarity index 100% rename from oa/builtin/webdav/internal/xml/read.go rename to oa/libs/webdav/internal/xml/read.go diff --git a/oa/builtin/webdav/internal/xml/read_test.go b/oa/libs/webdav/internal/xml/read_test.go similarity index 100% rename from oa/builtin/webdav/internal/xml/read_test.go rename to oa/libs/webdav/internal/xml/read_test.go diff --git a/oa/builtin/webdav/internal/xml/typeinfo.go b/oa/libs/webdav/internal/xml/typeinfo.go similarity index 100% rename from oa/builtin/webdav/internal/xml/typeinfo.go rename to oa/libs/webdav/internal/xml/typeinfo.go diff --git a/oa/builtin/webdav/internal/xml/xml.go b/oa/libs/webdav/internal/xml/xml.go similarity index 100% rename from oa/builtin/webdav/internal/xml/xml.go rename to oa/libs/webdav/internal/xml/xml.go diff --git a/oa/builtin/webdav/internal/xml/xml_test.go b/oa/libs/webdav/internal/xml/xml_test.go similarity index 100% rename from oa/builtin/webdav/internal/xml/xml_test.go rename to oa/libs/webdav/internal/xml/xml_test.go diff --git a/oa/builtin/webdav/litmus_test_server.go b/oa/libs/webdav/litmus_test_server.go similarity index 100% rename from oa/builtin/webdav/litmus_test_server.go rename to oa/libs/webdav/litmus_test_server.go diff --git a/oa/builtin/webdav/lock.go b/oa/libs/webdav/lock.go similarity index 100% rename from oa/builtin/webdav/lock.go rename to oa/libs/webdav/lock.go diff --git a/oa/builtin/webdav/lock_test.go b/oa/libs/webdav/lock_test.go similarity index 100% rename from oa/builtin/webdav/lock_test.go rename to oa/libs/webdav/lock_test.go diff --git a/oa/builtin/webdav/prop.go b/oa/libs/webdav/prop.go similarity index 100% rename from oa/builtin/webdav/prop.go rename to oa/libs/webdav/prop.go diff --git a/oa/builtin/webdav/prop_test.go b/oa/libs/webdav/prop_test.go similarity index 100% rename from oa/builtin/webdav/prop_test.go rename to oa/libs/webdav/prop_test.go diff --git a/oa/builtin/webdav/webdav.go b/oa/libs/webdav/webdav.go similarity index 99% rename from oa/builtin/webdav/webdav.go rename to oa/libs/webdav/webdav.go index 7555c45..ee52e32 100644 --- a/oa/builtin/webdav/webdav.go +++ b/oa/libs/webdav/webdav.go @@ -18,7 +18,7 @@ import ( "time" ) -func NewWebdav(p string) func(http.ResponseWriter, *http.Request) { +func NewWebdav(p string) *Handler { fs := &Handler{ FileSystem: Dir(p), LockSystem: NewMemLS(), @@ -27,7 +27,7 @@ func NewWebdav(p string) func(http.ResponseWriter, *http.Request) { return "" }, } - return fs.ServeHTTP + return fs } type Handler struct { diff --git a/oa/builtin/webdav/webdav_test.go b/oa/libs/webdav/webdav_test.go similarity index 100% rename from oa/builtin/webdav/webdav_test.go rename to oa/libs/webdav/webdav_test.go diff --git a/oa/builtin/webdav/xml.go b/oa/libs/webdav/xml.go similarity index 99% rename from oa/builtin/webdav/xml.go rename to oa/libs/webdav/xml.go index 75ce4db..b4923f7 100644 --- a/oa/builtin/webdav/xml.go +++ b/oa/libs/webdav/xml.go @@ -32,7 +32,7 @@ import ( // In the long term, this package should use the standard library's version // only, and the internal fork deleted, once // https://github.com/golang/go/issues/13400 is resolved. - ixml "oa/builtin/webdav/internal/xml" + ixml "oa/libs/webdav/internal/xml" ) // http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo diff --git a/oa/builtin/webdav/xml_test.go b/oa/libs/webdav/xml_test.go similarity index 99% rename from oa/builtin/webdav/xml_test.go rename to oa/libs/webdav/xml_test.go index 5f38242..811ac16 100644 --- a/oa/builtin/webdav/xml_test.go +++ b/oa/libs/webdav/xml_test.go @@ -16,7 +16,7 @@ import ( "strings" "testing" - ixml "oa/builtin/webdav/internal/xml" + ixml "oa/libs/webdav/internal/xml" ) func TestReadLockInfo(t *testing.T) { diff --git a/oa/models/app.go b/oa/models/app.go index 6178156..c29e303 100644 --- a/oa/models/app.go +++ b/oa/models/app.go @@ -21,10 +21,10 @@ type App struct { type AppUser struct { BaseModel AppID string `json:"app_id" methods:"get,list,post,patch,delete" parse:"path"` - App *App `json:"-" gorm:"foreignKey:AppID;references:ID"` + App *App `json:"app" gorm:"foreignKey:AppID;references:ID"` UserID string `json:"user_id" methods:"get,*list,post" parse:"json"` - User *User `json:"-" gorm:"foreignKey:UserID;references:ID"` + User *User `json:"user" gorm:"foreignKey:UserID;references:ID"` Status string `json:"status" methods:"post,*patch,*list" parse:"json"` } diff --git a/oa/models/init.go b/oa/models/init.go index e7fefa9..a67f214 100644 --- a/oa/models/init.go +++ b/oa/models/init.go @@ -50,6 +50,9 @@ func init() { func InitDBData() error { app := &App{} app.ID = cfg.Config.ID + app.Name = "OA" + app.Icon = "/favicon.ico" + app.Key = cfg.Config.Key logv.AssertError(cfg.DB().Where("id = ?", app.ID).Attrs(app).FirstOrCreate(app).Error) initRole := map[string]map[string]uint{ "user": {"admin": 5, "normal": 1},