feat: 完成了 file list 右键简单菜单
feat: 完成了 file 下载 todo: 桶右键菜单和删除桶
This commit is contained in:
@ -5,6 +5,8 @@ import (
|
||||
"github.com/loveuer/nf-disk/ndh"
|
||||
"github.com/samber/lo"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func DialogOpen(ctx context.Context) ndh.Handler {
|
||||
@ -54,3 +56,58 @@ func DialogOpen(ctx context.Context) ndh.Handler {
|
||||
return c.Send200(map[string]interface{}{"result": result})
|
||||
}
|
||||
}
|
||||
|
||||
func DialogSave(ctx context.Context) ndh.Handler {
|
||||
return func(c *ndh.Ctx) error {
|
||||
type Req struct {
|
||||
Title string `json:"title"`
|
||||
Filters []string `json:"filters"`
|
||||
DefaultDirectory string `json:"default_directory"`
|
||||
DefaultFilename string `json:"default_filename"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
opt = runtime.SaveDialogOptions{
|
||||
Title: "将文件保存到",
|
||||
}
|
||||
result any
|
||||
)
|
||||
|
||||
if err = c.ReqParse(req); err != nil {
|
||||
return c.Send400(err.Error())
|
||||
}
|
||||
|
||||
if req.Title != "" {
|
||||
opt.Title = req.Title
|
||||
}
|
||||
|
||||
if req.DefaultFilename != "" {
|
||||
opt.DefaultFilename = req.DefaultFilename
|
||||
}
|
||||
|
||||
if req.DefaultDirectory != "" {
|
||||
opt.DefaultDirectory = req.DefaultDirectory
|
||||
}
|
||||
|
||||
if opt.DefaultDirectory == "" {
|
||||
var home string
|
||||
if home, err = os.UserHomeDir(); err != nil {
|
||||
opt.DefaultDirectory = filepath.Join(home, "Downloads")
|
||||
}
|
||||
}
|
||||
|
||||
if len(req.Filters) > 0 {
|
||||
opt.Filters = lo.Map(req.Filters, func(item string, index int) runtime.FileFilter {
|
||||
return runtime.FileFilter{Pattern: item}
|
||||
})
|
||||
}
|
||||
|
||||
if result, err = runtime.SaveFileDialog(ctx, opt); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
return c.Send200(map[string]interface{}{"result": result})
|
||||
}
|
||||
}
|
||||
|
@ -86,3 +86,100 @@ func FileUpload(c *ndh.Ctx) error {
|
||||
|
||||
return c.Send200(req)
|
||||
}
|
||||
|
||||
func FileInfo(c *ndh.Ctx) error {
|
||||
type Req struct {
|
||||
ConnId uint64 `json:"conn_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
client *s3.Client
|
||||
info *s3.ObjectInfo
|
||||
)
|
||||
|
||||
if err = c.ReqParse(req); err != nil {
|
||||
return c.Send400(err.Error())
|
||||
}
|
||||
|
||||
if _, client, err = manager.Manager.Use(req.ConnId); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if info, err = client.GetObjectInfo(c.Context(), req.Bucket, req.Key); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
return c.Send200(info)
|
||||
}
|
||||
|
||||
func FileGet(c *ndh.Ctx) error {
|
||||
type Req struct {
|
||||
ConnId uint64 `json:"conn_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
client *s3.Client
|
||||
link *s3.ObjectEntry
|
||||
)
|
||||
|
||||
if err = c.ReqParse(req); err != nil {
|
||||
return c.Send400(err.Error())
|
||||
}
|
||||
|
||||
if _, client, err = manager.Manager.Use(req.ConnId); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if link, err = client.GetObjectEntry(c.Context(), req.Bucket, req.Key); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
return c.Send200(link)
|
||||
}
|
||||
|
||||
func FileDownload(c *ndh.Ctx) error {
|
||||
type Req struct {
|
||||
ConnId uint64 `json:"conn_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
Key string `json:"key"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
client *s3.Client
|
||||
obj *s3.ObjectEntity
|
||||
target *os.File
|
||||
)
|
||||
|
||||
if err = c.ReqParse(req); err != nil {
|
||||
return c.Send400(err.Error())
|
||||
}
|
||||
|
||||
if _, client, err = manager.Manager.Use(req.ConnId); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if obj, err = client.GetObject(c.Context(), req.Bucket, req.Key); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if target, err = os.OpenFile(filepath.Clean(req.Location), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
if _, err = io.Copy(target, obj.Body); err != nil {
|
||||
return c.Send500(err.Error())
|
||||
}
|
||||
|
||||
return c.Send200(req)
|
||||
}
|
||||
|
Reference in New Issue
Block a user