first commit
This commit is contained in:
72
controller/config.go
Normal file
72
controller/config.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func getHomeProfile() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return os.Getenv("USERPROFILE") + "\\.filer"
|
||||
}
|
||||
return os.Getenv("HOME") + "/.filer"
|
||||
}
|
||||
|
||||
func init() {
|
||||
NewConfig(getHomeProfile())
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
ConfigFile string `yaml:"-"`
|
||||
Environment string `yaml:"environment" json:"environment"`
|
||||
Upyun UpyunConfig `yaml:"upyun" json:"upyun"`
|
||||
}
|
||||
|
||||
type UpyunConfig struct {
|
||||
Bucket string `yaml:"bucket" json:"bucket"`
|
||||
Operator string `yaml:"operator" json:"operator"`
|
||||
Password string `yaml:"password" json:"password"`
|
||||
Domain string `yaml:"domain" json:"domain"`
|
||||
}
|
||||
|
||||
var conf *ServerConfig
|
||||
|
||||
func NewConfig(fileName string) {
|
||||
conf = new(ServerConfig)
|
||||
conf.ConfigFile = fileName
|
||||
if err := conf.Load(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetConfig() *ServerConfig {
|
||||
if conf == nil {
|
||||
panic(fmt.Errorf("config not init"))
|
||||
}
|
||||
return conf
|
||||
}
|
||||
|
||||
func (receiver *ServerConfig) Load() error {
|
||||
data, err := os.ReadFile(receiver.ConfigFile)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if err := yaml.Unmarshal(data, receiver); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (receiver *ServerConfig) Rewrite() error {
|
||||
data, err := yaml.Marshal(receiver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(receiver.ConfigFile, data, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
175
controller/controller.go
Normal file
175
controller/controller.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/upyun/go-sdk/v3/upyun"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
ctx context.Context
|
||||
uper *upyun.UpYun
|
||||
}
|
||||
|
||||
func (s *Storage) Startup(ctx context.Context) {
|
||||
s.ctx = ctx
|
||||
}
|
||||
|
||||
func NewStorage() *Storage {
|
||||
return &Storage{}
|
||||
}
|
||||
|
||||
// Quit 退出程序
|
||||
func (s *Storage) Quit() {
|
||||
runtime.Quit(s.ctx)
|
||||
}
|
||||
|
||||
// GetConfig 获取所有配置
|
||||
func (s *Storage) GetConfig() (resp *ServerConfig, err error) {
|
||||
resp = new(ServerConfig)
|
||||
cfg := GetConfig()
|
||||
resp.Upyun = UpyunConfig{
|
||||
Bucket: cfg.Upyun.Bucket,
|
||||
Operator: cfg.Upyun.Operator,
|
||||
Password: cfg.Upyun.Password,
|
||||
Domain: cfg.Upyun.Domain,
|
||||
}
|
||||
logrus.Infof("get config: %v", resp.Upyun)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// SetUpyunConfig 更新又拍云的配置
|
||||
func (s *Storage) SetUpyunConfig(req *UpyunConfig) (err error) {
|
||||
cfg := GetConfig()
|
||||
logrus.Infof("event receive: %+v", req)
|
||||
cfg.Upyun.Bucket = req.Bucket
|
||||
cfg.Upyun.Operator = req.Operator
|
||||
cfg.Upyun.Password = req.Password
|
||||
cfg.Upyun.Domain = req.Domain
|
||||
if err := cfg.Rewrite(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.refreshDriver(cfg.Upyun)
|
||||
return nil
|
||||
}
|
||||
|
||||
// refreshDriver 刷新驱动句柄
|
||||
func (s *Storage) refreshDriver(cfg UpyunConfig) {
|
||||
s.uper = upyun.NewUpYun(&upyun.UpYunConfig{
|
||||
Bucket: cfg.Bucket,
|
||||
Operator: cfg.Operator,
|
||||
Password: cfg.Password,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Storage) isUperNil() error {
|
||||
if s.uper == nil {
|
||||
cfg := GetConfig().Upyun
|
||||
if cfg.Bucket == "" || cfg.Operator == "" || cfg.Password == "" {
|
||||
return fmt.Errorf("[upyun] please initial config")
|
||||
}
|
||||
s.refreshDriver(cfg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UperCreateDirectory 创建目录
|
||||
func (s *Storage) UperCreateDirectory(dirname string) (err error) {
|
||||
if err := s.isUperNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.uper.Mkdir(dirname); err != nil {
|
||||
logrus.Errorf("[upyun] mkdir %s failed: %v", dirname, err)
|
||||
return fmt.Errorf("[upyun] mkdir %s failed: %v", dirname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UperList 获取文件列表
|
||||
func (s *Storage) UperList(prefix string) (list []*FileInfo, err error) {
|
||||
|
||||
if err := s.isUperNil(); err != nil {
|
||||
return list, err
|
||||
}
|
||||
|
||||
objsChan := make(chan *upyun.FileInfo, 25)
|
||||
go func() {
|
||||
if err := s.uper.List(&upyun.GetObjectsConfig{
|
||||
Path: prefix,
|
||||
ObjectsChan: objsChan,
|
||||
MaxListTries: 0,
|
||||
MaxListLevel: 0,
|
||||
DescOrder: true,
|
||||
}); err != nil {
|
||||
logrus.Errorf("[upyun] read list %s failed: %v", prefix, err)
|
||||
}
|
||||
}()
|
||||
|
||||
for file := range objsChan {
|
||||
list = append(list, &FileInfo{
|
||||
Filename: file.Name,
|
||||
Prefix: prefix,
|
||||
CreatedAt: file.Time.Format(time.DateTime),
|
||||
Size: file.Size,
|
||||
IsDir: file.IsDir,
|
||||
})
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// UperUpload 上传文件
|
||||
func (s *Storage) UperUpload(prefix string) (err error) {
|
||||
if err := s.isUperNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
filename, err := runtime.OpenFileDialog(s.ctx, runtime.OpenDialogOptions{})
|
||||
|
||||
basefile := filepath.Base(filename)
|
||||
|
||||
if prefix != "/" {
|
||||
prefix += "/"
|
||||
}
|
||||
basefile = prefix + basefile
|
||||
|
||||
logrus.Infof("filename: %s, upload filename: %s", filename, basefile)
|
||||
file, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.uper.Put(&upyun.PutObjectConfig{
|
||||
Path: basefile,
|
||||
Reader: file,
|
||||
}); err != nil {
|
||||
logrus.Errorf("[upyun] upload file %s failed: %v", basefile, err)
|
||||
return fmt.Errorf("[upyun] upload file %s failed: %v", basefile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UperDelete 删除文件或文件夹
|
||||
func (s *Storage) UperDelete(filename string, isDir bool) (err error) {
|
||||
|
||||
if err := s.isUperNil(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.uper.Delete(&upyun.DeleteObjectConfig{
|
||||
Path: filename,
|
||||
Async: true,
|
||||
Folder: isDir,
|
||||
}); err != nil {
|
||||
logrus.Errorf("[upyun] delete file %s failed: %v", filename, err)
|
||||
return fmt.Errorf("[upyun] delete file %s failed: %v", filename, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
9
controller/data_struct.go
Normal file
9
controller/data_struct.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package controller
|
||||
|
||||
type FileInfo struct {
|
||||
Filename string `json:"filename,omitempty"`
|
||||
Prefix string `json:"prefix,omitempty"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
Size int64 `json:"size,omitempty"`
|
||||
IsDir bool `json:"is_dir,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user