diff --git a/configs/config.go b/configs/config.go index 9dd525adac3d67567b3e957a4a8a4ee461b45abb..c5221f8177f3c7a11a490bb3cec4253c9a1988df 100644 --- a/configs/config.go +++ b/configs/config.go @@ -3,27 +3,19 @@ package configs import "github.com/wailsapp/wails/v2/pkg/runtime" // 存储所有配置项的类型 -var ConfigTypes = [...]string{"alioss", "localImgPath", "tuChuang"} +var ConfigTypes = [...]string{"alioss", "localImgPath", "imgBed"} // 按照类型存储所有配置项 var AliOSS = [...]string{"point", "endPoint", "accessKeyId", "accessKeySecret", "bucketName", "projectDir"} var LocalImgPath = [...]string{"path"} -var TuChuang = [...]string{"configType"} +var ImgBed = [...]string{"configType"} +// 默认文件路径 var ( LogFile = "%s/gtools.log" DBFile = "%s/gtools.db" ) -// TODO 之后的阿里云数据默认为空值,并且数据入sqlite库 -var ( - Point = "oss-cn-beijing" - Endpoint = Point + ".aliyuncs.com" - AccessKeyId = "LTAI5tFNagDQNmkdeLoXrkdL" - AccessKeySecret = "iUt3xEV5EgdmyLfASAwVNBsUfpQm4c" - BucketName = "ly-img-xiao" - ProjectDir = "test" -) - +// markdown 文件类型 var ( MdFilter = runtime.FileFilter{ DisplayName: "Markdown (*.md)", diff --git a/configs/log.go b/configs/log.go index 9e88f8a0c03c310b474a57f63ae627247955ce35..347fd38e067146ffb85ab80f6c1ee7213f2b2a27 100644 --- a/configs/log.go +++ b/configs/log.go @@ -12,6 +12,16 @@ var ( DelCmdItemErr = "删除快捷指令[%s]失败--异常信息[%s]" ) var ( - GetConfigListErr = "获取系统配置列表失败--异常信息[%s]" - AddConfigItemErr = "新增[%s]系统配置失败--异常信息[%s]" + GetConfigListErr = "获取系统配置列表失败--异常信息[%s]" + AddConfigItemErr = "新增[%s]系统配置失败--异常信息[%s]" + NoImgBedConfigErr = "系统无图床配置" +) +var ( + GetSystemClipboardImgErr = "获取系统剪贴板图片失败" + SaveImgToLocalErr = "图片本地存储失败" +) + +var ( + CreateAliOSSClientErr = "创建阿里云OSS客户端失败--异常信息[%s]" + GetAliOSSBucketErr = "获取AliOSS存储空间失败--异常信息[%s]" ) diff --git a/controller/app.go b/controller/app.go index 058e34498eaa09e2eeaefd377c40b24729a22d41..b9410a89625665f0658ddc1cf14314d0b5cedb46 100644 --- a/controller/app.go +++ b/controller/app.go @@ -14,13 +14,13 @@ import ( // App 结构体 type App struct { - ctx context.Context - Log *logrus.Logger - Db *xorm.Engine - LogFile string - DBFile string - ConfigMap map[string]map[string]string - AliOSS *oss.Client + ctx context.Context + Log *logrus.Logger + Db *xorm.Engine + LogFile string + DBFile string + ConfigMap map[string]map[string]string + AliOSS *oss.Client LocalImgPath string } @@ -46,10 +46,10 @@ func (a *App) OnStartup(ctx context.Context) { a.Db = internal.NewXormEngine(a.DBFile) // 获取系统配置文件 - a.ConfigMap = a.getConfigMap() + a.ConfigMap = a.GetConfigMap() // 初始化图床配置 - a.initTuChuang() + a.initImgBed() } // OnBeforeClose @@ -60,29 +60,6 @@ func (a *App) OnBeforeClose(ctx context.Context) bool { return false } -// TODO 获取系统配置 -func (a *App) getConfigMap() map[string]map[string]string { - configMap := make(map[string]map[string]string) - for _, ctype := range configs.ConfigTypes { - list := make([]internal.ConfigItem, 0) - if err := a.Db.Where("type = ?", ctype).Find(&list); err != nil { - a.Log.Error(fmt.Sprintf(configs.GetConfigListErr, err.Error())) - panic(err.Error()) - } - if len(list) == 0 { - a.initConfigData(ctype) - } else { - typeMap := make(map[string]string, 0) - for _, v := range list { - typeMap[v.Name] = v.Value - } - configMap[ctype] = typeMap - fmt.Printf("configMap: %v\n", configMap) - } - } - return configMap -} - // 系统配置数据初始化 func (a *App) initConfigData(ctype string) { switch ctype { @@ -94,10 +71,10 @@ func (a *App) initConfigData(ctype string) { if _, err := a.Db.Insert(&datas); err != nil { a.Log.Error(fmt.Sprintf(configs.AddConfigItemErr, ctype, err.Error())) } - case "tuChuang": + case "imgBed": datas := make([]*internal.ConfigItem, 0) - for i := 0; i < len(configs.TuChuang); i++ { - datas = append(datas, &internal.ConfigItem{Name: configs.TuChuang[i], Value: "", Type: ctype}) + for i := 0; i < len(configs.ImgBed); i++ { + datas = append(datas, &internal.ConfigItem{Name: configs.ImgBed[i], Value: "", Type: ctype}) } if _, err := a.Db.Insert(&datas); err != nil { a.Log.Error(fmt.Sprintf(configs.AddConfigItemErr, ctype, err.Error())) @@ -115,15 +92,15 @@ func (a *App) initConfigData(ctype string) { } } -func (a *App) initTuChuang() { - switch a.ConfigMap["tuChuang"]["configType"] { +func (a *App) initImgBed() { + switch a.ConfigMap["imgBed"]["configType"] { case "alioss": // 初始化阿里云OSS客户端 - a.AliOSS = internal.NewOssClient() + a.AliOSS = internal.NewOssClient(a.ConfigMap["alioss"]["endPoint"], a.ConfigMap["alioss"]["accessKeyId"], a.ConfigMap["alioss"]["accessKeySecret"], a.Log) case "localImgPath": // 配置本地图床文件夹路径 - a.LocalImgPath = a.ConfigMap["tuChuang"]["path"] - default: a.Log.Info("系统无图床配置") + a.LocalImgPath = a.ConfigMap["imgBed"]["path"] + default: + a.Log.Info(configs.NoImgBedConfigErr) } - } diff --git a/controller/clipboard.go b/controller/clipboard.go index fe0aff7b7cbe01a81d449b6080740079e2780fe5..3cb3b18a13c50eb6c736275528f86c975eb876e4 100644 --- a/controller/clipboard.go +++ b/controller/clipboard.go @@ -1,8 +1,9 @@ package gtools import ( - "gtools/util" "fmt" + "gtools/configs" + "gtools/util" "os" "strconv" "time" @@ -11,12 +12,19 @@ import ( ) func (a *App) UploadScreenshot() *util.Resp { + imgBedType := a.ConfigMap["imgBed"]["configType"] + if imgBedType == "" { + return util.Error(configs.NoImgBedConfigErr) + } // 获取系统粘贴板中的文件地址 b, imgPath := savePngFromClipboard() if !b { + // 删除图片临时文件 + os.Remove(imgPath) + a.Log.Error("系统剪贴板图片地址异常: " + imgPath) - return util.Error(imgPath) + return util.Error(configs.GetSystemClipboardImgErr) } // 判断文件是否存在并为非文件夹 @@ -26,12 +34,22 @@ func (a *App) UploadScreenshot() *util.Resp { return util.Error(s) } - // TODO 从配置文件中选取上传首选项 目前先写死只使用阿里云OSS上传图片 - b, s = util.UploadByAliOss(imgPath, a.AliOSS, a.Log) + // 选择图床存储文件 + switch imgBedType { + case "alioss": + if b, s = util.UploadByAliOss(imgPath, a.AliOSS, a.Log, a.ConfigMap["alioss"]); !b { + return util.Error("图片上传阿里云OSS失败, 请检查软件配置和网路状况") + } + case "localImgPath": + if b, s = util.MoveImgToPath(imgPath, a.ConfigMap["localImgPath"]["path"]); !b { + return util.Error("图片本地存储失败, 请检查软件配置") + } + default: + } + // 删除图片临时文件 + os.Remove(imgPath) if b { - // 删除图片临时文件 - os.Remove(imgPath) - // TODO 将线上图片地址存储到sqlite数据库中 + // TODO 图片地址存储到sqlite数据库中 mdImg := "![](%v)" // 可以输出markdown原始图片 return util.Success(fmt.Sprintf(mdImg, s)) diff --git a/controller/settings.go b/controller/configs.go similarity index 50% rename from controller/settings.go rename to controller/configs.go index 2ef0d850a1c92002b532f95660569180949286c9..7660472b4bfbb6cbb4ba8c1df4477a146f8941a9 100644 --- a/controller/settings.go +++ b/controller/configs.go @@ -1,13 +1,37 @@ package gtools import ( + "fmt" "gtools/configs" + "gtools/internal" "gtools/util" - "fmt" "os" "os/user" ) +// 获取系统配置 +func (a *App) GetConfigMap() map[string]map[string]string { + configMap := make(map[string]map[string]string) + for _, ctype := range configs.ConfigTypes { + list := make([]internal.ConfigItem, 0) + if err := a.Db.Where("type = ?", ctype).Find(&list); err != nil { + a.Log.Error(fmt.Sprintf(configs.GetConfigListErr, err.Error())) + panic(err.Error()) + } + if len(list) == 0 { + a.initConfigData(ctype) + } else { + typeMap := make(map[string]string, 0) + for _, v := range list { + typeMap[v.Name] = v.Value + } + configMap[ctype] = typeMap + } + } + return configMap +} + +// 清理Mac上的Webkit缓存 func (a *App) CleanWebKitCache() *util.Resp { // 直接将MacOS上对应的Cache文件夹删除 userInfo, err := user.Current() diff --git a/controller/imgbed.go b/controller/imgbed.go new file mode 100644 index 0000000000000000000000000000000000000000..667a2054242d89eb9932f9ee138770099d5e757d --- /dev/null +++ b/controller/imgbed.go @@ -0,0 +1,5 @@ +package gtools + +func (a *App) SaveImgPath() { + +} \ No newline at end of file diff --git a/frontend/index.html b/frontend/index.html index 774a81b282656fe7b8d35f7c5a1220af4d880d06..7590055f43c5290a9bd06382c65f2ef794443c6b 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,13 +1,22 @@ -
- - - -