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 @@ - - - - - GTools - - -
- - - + + + + + + GTools + + + + +
+ + + + \ No newline at end of file diff --git a/frontend/src/views/Mditor.vue b/frontend/src/views/Mditor.vue index 87379a2cd07600f819c2946c6b4d949ad013c59e..4968ac1bdd53859e9b77021574cd34256d702ea7 100644 --- a/frontend/src/views/Mditor.vue +++ b/frontend/src/views/Mditor.vue @@ -400,6 +400,7 @@ export default { let _this = this this.app.UploadScreenshot().then((res) => { if (res.code == 200) { + console.log(res); _this.insertValue(res.data) } else { message.error(res.msg) diff --git a/frontend/wailsjs/go/gtools/App.d.ts b/frontend/wailsjs/go/gtools/App.d.ts index 0344554a90073b360afef9a77e87eb52ecb665c9..0c80824d183d67032f7b26512e54d9cf2081d7e3 100755 --- a/frontend/wailsjs/go/gtools/App.d.ts +++ b/frontend/wailsjs/go/gtools/App.d.ts @@ -25,6 +25,8 @@ export function DelTodoItemById(arg1:internal.TodoItem):Promise; export function GetCmdItemList():Promise; +export function GetConfigMap():Promise<{[key: string]: {[key: string]: string}}>; + export function GetMdContent(arg1:string):Promise; export function GetMdDirList():Promise; @@ -41,6 +43,8 @@ export function OpenMdFolderWindow():Promise; export function OpenMdSaveFileWindow():Promise; +export function SaveImgPath():Promise; + export function SaveMdContent(arg1:string,arg2:string):Promise; export function UpdateTodoItem(arg1:internal.TodoItem):Promise; diff --git a/frontend/wailsjs/go/gtools/App.js b/frontend/wailsjs/go/gtools/App.js index 16269b899857cd5a8946ac569d1a64bfb518bc6e..7668c462325d7e8fcc995dbf9801baa7b00023f0 100755 --- a/frontend/wailsjs/go/gtools/App.js +++ b/frontend/wailsjs/go/gtools/App.js @@ -46,6 +46,10 @@ export function GetCmdItemList() { return window['go']['gtools']['App']['GetCmdItemList'](); } +export function GetConfigMap() { + return window['go']['gtools']['App']['GetConfigMap'](); +} + export function GetMdContent(arg1) { return window['go']['gtools']['App']['GetMdContent'](arg1); } @@ -78,6 +82,10 @@ export function OpenMdSaveFileWindow() { return window['go']['gtools']['App']['OpenMdSaveFileWindow'](); } +export function SaveImgPath() { + return window['go']['gtools']['App']['SaveImgPath'](); +} + export function SaveMdContent(arg1, arg2) { return window['go']['gtools']['App']['SaveMdContent'](arg1, arg2); } diff --git a/go.mod b/go.mod index 398cd02efe06bf8c7caf70add7979e515a307bd7..243c1902ef8d8b62f55e762b8d7bf4553817d571 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.18 require ( github.com/aliyun/aliyun-oss-go-sdk v2.2.5+incompatible + github.com/mattn/go-sqlite3 v1.14.16 github.com/sirupsen/logrus v1.9.0 github.com/wailsapp/wails/v2 v2.2.0 - modernc.org/sqlite v1.19.4 xorm.io/xorm v1.3.2 ) @@ -14,11 +14,8 @@ require ( github.com/goccy/go-json v0.9.11 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect - github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/samber/lo v1.33.0 // indirect github.com/stretchr/testify v1.8.1 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect @@ -26,17 +23,7 @@ require ( golang.org/x/exp/shiny v0.0.0-20221109134031-9ce248df8de5 // indirect golang.org/x/image v0.1.0 // indirect golang.org/x/mobile v0.0.0-20221020085226-b36e6246172e // indirect - golang.org/x/mod v0.6.0 // indirect - golang.org/x/tools v0.2.0 // indirect - lukechampine.com/uint128 v1.2.0 // indirect - modernc.org/cc/v3 v3.40.0 // indirect - modernc.org/ccgo/v3 v3.16.13 // indirect - modernc.org/libc v1.21.4 // indirect - modernc.org/mathutil v1.5.0 // indirect - modernc.org/memory v1.4.0 // indirect - modernc.org/opt v0.1.3 // indirect - modernc.org/strutil v1.1.3 // indirect - modernc.org/token v1.0.1 // indirect + modernc.org/sqlite v1.19.4 // indirect xorm.io/builder v0.3.12 // indirect ) diff --git a/go.sum b/go.sum index beb6ebb7d6499bf5d958e77e7f79758ba3425378..ab5230de0d9695e2f74fca5c46ade1668f9f1505 100644 --- a/go.sum +++ b/go.sum @@ -54,7 +54,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= @@ -107,7 +106,6 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -472,7 +470,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I= -golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -585,7 +582,6 @@ golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE= -golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -639,7 +635,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= -lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= modernc.org/cc/v3 v3.33.6/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g= modernc.org/cc/v3 v3.33.9/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g= modernc.org/cc/v3 v3.33.11/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g= @@ -655,7 +650,6 @@ modernc.org/cc/v3 v3.35.16/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g modernc.org/cc/v3 v3.35.17/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g= modernc.org/cc/v3 v3.35.18/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g= modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw= -modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0= modernc.org/ccgo/v3 v3.9.5/go.mod h1:umuo2EP2oDSBnD3ckjaVUXMrmeAw8C8OSICVa0iFf60= modernc.org/ccgo/v3 v3.10.0/go.mod h1:c0yBmkRFi7uW4J7fwx/JiijwOjeAeR2NoSaRVFPmjMw= modernc.org/ccgo/v3 v3.11.0/go.mod h1:dGNposbDp9TOZ/1KBxghxtUp/bzErD0/0QW4hhSaBMI= @@ -692,10 +686,7 @@ modernc.org/ccgo/v3 v3.12.73/go.mod h1:hngkB+nUUqzOf3iqsM48Gf1FZhY599qzVg1iX+BT3 modernc.org/ccgo/v3 v3.12.81/go.mod h1:p2A1duHoBBg1mFtYvnhAnQyI6vL0uw5PGYLSIgF6rYY= modernc.org/ccgo/v3 v3.12.82/go.mod h1:ApbflUfa5BKadjHynCficldU1ghjen84tuM5jRynB7w= modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw= -modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY= modernc.org/ccorpus v1.11.1/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ= -modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk= -modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM= modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= modernc.org/libc v1.9.8/go.mod h1:U1eq8YWr/Kc1RWCMFUWEdkTg8OTcfLw2kY8EDwl039w= modernc.org/libc v1.9.11/go.mod h1:NyF3tsA5ArIjJ83XB0JlqhjTabTCHm9aX4XMPHyQn0Q= @@ -733,33 +724,25 @@ modernc.org/libc v1.11.82/go.mod h1:NF+Ek1BOl2jeC7lw3a7Jj5PWyHPwWD4aq3wVKxqV1fI= modernc.org/libc v1.11.86/go.mod h1:ePuYgoQLmvxdNT06RpGnaDKJmDNEkV7ZPKI2jnsvZoE= modernc.org/libc v1.11.87/go.mod h1:Qvd5iXTeLhI5PS0XSyqMY99282y+3euapQFxM7jYnpY= modernc.org/libc v1.21.4 h1:CzTlumWeIbPV5/HVIMzYHNPCRP8uiU/CWiN2gtd/Qu8= -modernc.org/libc v1.21.4/go.mod h1:przBsL5RDOZajTVslkugzLBj1evTue36jEomFQOoYuI= modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.4.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= -modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/memory v1.0.4/go.mod h1:nV2OApxradM3/OVbs2/0OsP6nPfakXpi50C7dcoHXlc= modernc.org/memory v1.0.5/go.mod h1:B7OYswTRnfGg+4tDH1t1OeUNnsy2viGTdME4tzd+IjM= modernc.org/memory v1.4.0 h1:crykUfNSnMAXaOJnnxcSzbUGMqkLWjklJKkBK2nwZwk= -modernc.org/memory v1.4.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= modernc.org/opt v0.1.1/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= -modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= modernc.org/sqlite v1.14.2/go.mod h1:yqfn85u8wVOE6ub5UT8VI9JjhrwBUUCNyTACN0h6Sx8= modernc.org/sqlite v1.19.4 h1:nlPIDqumn6/mSvs7T5C8MNYEuN73sISzPdKtMdURpUI= modernc.org/sqlite v1.19.4/go.mod h1:x/yZNb3h5+I3zGQSlwIv4REL5eJhiRkUH5MReogAeIc= modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw= modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= -modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= modernc.org/tcl v1.8.13/go.mod h1:V+q/Ef0IJaNUSECieLU4o+8IScapxnMyFV6i/7uQlAY= -modernc.org/tcl v1.15.0 h1:oY+JeD11qVVSgVvodMJsu7Edf8tr5E/7tuhF5cNYz34= modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg= -modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= modernc.org/z v1.2.19/go.mod h1:+ZpP0pc4zz97eukOzW3xagV/lS82IpPN9NGG5pNF9vY= -modernc.org/z v1.7.0 h1:xkDw/KepgEjeizO2sNco+hqYkU12taxQFqPEmgm1GWE= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU= xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= diff --git a/internal/ali.go b/internal/ali.go index b61f45eddaf608be400ee9ed20970ef340fe0da2..09e8353882b711ebdd137e393970128bc242b3f8 100644 --- a/internal/ali.go +++ b/internal/ali.go @@ -1,16 +1,18 @@ package internal import ( + "fmt" "gtools/configs" "github.com/aliyun/aliyun-oss-go-sdk/oss" + "github.com/sirupsen/logrus" ) -func NewOssClient() *oss.Client { +func NewOssClient(endPoint string, accessKeyId string, accessKeySecret string, log *logrus.Logger) *oss.Client { // 创建OSSClient实例。 - client, err := oss.New(configs.Endpoint, configs.AccessKeyId, configs.AccessKeySecret) + client, err := oss.New(endPoint, accessKeyId, accessKeySecret) if err != nil { - panic(err.Error()) + log.Error(fmt.Sprintf(configs.CreateAliOSSClientErr, err.Error())) } return client } diff --git a/internal/domain.go b/internal/domain.go index 5429fcaafc37bd0bad50a793f2bd6a7c72da3c62..f50db736461be88371ec2aad9d70aecb93244739 100644 --- a/internal/domain.go +++ b/internal/domain.go @@ -46,3 +46,12 @@ type ConfigItem struct { State int8 `json:"state" xorm:"default(0) notnull"` // 启用状态 Created time.Time `json:"created" xorm:"created"` // 创建时间 } + +// 图床配置项 +type ImageItem struct { + Id int64 `json:"id"` // 图片 + Path string `json:"path" xorm:"default('') notnull"` // 图片路径 + Type string `json:"type" xorm:"default('') notnull"` // 图片类型-本地/阿里OSS/腾讯OSS/sm.ms + Article string `json:"article" xorm:"default('') notnull"` // 图片所在文章 + Created time.Time `json:"created" xorm:"created"` // 创建时间 +} diff --git a/internal/xorm.go b/internal/xorm.go index 65363997698b8b9fb7867b66398664c79c97c936..8a51ba47084d72d8194c47026d6b214b3f498488 100644 --- a/internal/xorm.go +++ b/internal/xorm.go @@ -3,14 +3,14 @@ package internal import ( "time" - _ "modernc.org/sqlite" + _ "github.com/mattn/go-sqlite3" "xorm.io/xorm" "xorm.io/xorm/names" ) func NewXormEngine(dbPath string) *xorm.Engine { - engine, err := xorm.NewEngine("sqlite", dbPath) + engine, err := xorm.NewEngine("sqlite3", dbPath) // 引入 _ "github.com/go-sql-driver/mysql" 即可使用mysql进行数据存储, 配置如下 // engine, err := xorm.NewEngine("mysql", "root:root@/gtools?charset=utf8") if err != nil { diff --git a/main.go b/main.go index 3dd1c8a6c7fd963ee2d1c3e2718b7a44a19e1549..b6cf6aa52009c44c1348f042d6f54632da61348f 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,15 @@ package main import ( "embed" + "fmt" gtools "gtools/controller" + "net/http" + "os" + "strings" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" + "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/mac" "golang.design/x/clipboard" ) @@ -24,6 +29,29 @@ func init() { } } +type FileLoader struct { + http.Handler +} + +func NewFileLoader() *FileLoader { + return &FileLoader{} +} + +func (h *FileLoader) ServeHTTP(res http.ResponseWriter, req *http.Request) { + var err error + + println(req.URL.Path) + requestedFilename := strings.TrimPrefix(req.URL.Path, "/") + println(requestedFilename) + fileData, err := os.ReadFile("/" + requestedFilename) + if err != nil { + res.WriteHeader(http.StatusBadRequest) + res.Write([]byte(fmt.Sprintf("Could not load file %s", requestedFilename))) + } + + res.Write(fileData) +} + func main() { // Create an instance of the app structure app := gtools.NewApp() @@ -36,14 +64,17 @@ func main() { MinWidth: 1100, // 最小宽度 MinHeight: 768, // 最小高度 HideWindowOnClose: true, // 关闭的时候隐藏窗口 - StartHidden: false, // 启动的时候隐藏窗口 (建议生产环境关闭此项,开发环境开启此项,原因自己体会) + StartHidden: true, // 启动的时候隐藏窗口 (建议生产环境关闭此项,开发环境开启此项,原因自己体会) AlwaysOnTop: false, // 窗口固定在最顶层 - Assets: assets, - BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 128}, - OnStartup: app.OnStartup, - OnBeforeClose: app.OnBeforeClose, - CSSDragProperty: "--wails-draggable", - CSSDragValue: "drag", + AssetServer: &assetserver.Options{ + Assets: assets, + Handler: NewFileLoader(), + }, + BackgroundColour: &options.RGBA{R: 0, G: 0, B: 0, A: 128}, + OnStartup: app.OnStartup, + OnBeforeClose: app.OnBeforeClose, + CSSDragProperty: "--wails-draggable", + CSSDragValue: "drag", Bind: []interface{}{ app, }, diff --git a/util/oss.go b/util/oss.go index ceb80dd366c4d894dff2557332828a43bb4da9ea..7c0263d9a6b7feff9e03a213098e673615492be3 100644 --- a/util/oss.go +++ b/util/oss.go @@ -3,24 +3,25 @@ package util import ( "fmt" "gtools/configs" + "os" "strings" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/sirupsen/logrus" ) -func UploadByAliOss(path string, client *oss.Client, log *logrus.Logger) (bool, string) { +func UploadByAliOss(path string, client *oss.Client, log *logrus.Logger, config map[string]string) (bool, string) { // 获取文件名称 s := strings.Split(path, "/") var fileName = s[len(s)-1] // 生成Bucket下的图片路径 - objectName := fmt.Sprintf("%s/%s", configs.ProjectDir, fileName) + objectName := fmt.Sprintf("%s/%s", config["projectDir"], fileName) // 获取存储空间。 - bucket, err := client.Bucket(configs.BucketName) + bucket, err := client.Bucket(config["bucketName"]) if err != nil { - log.Error("获取AliOSS存储空间失败: " + err.Error()) + log.Error(fmt.Sprintf(configs.GetAliOSSBucketErr, err.Error())) return false, err.Error() } // 上传文件。 @@ -29,5 +30,17 @@ func UploadByAliOss(path string, client *oss.Client, log *logrus.Logger) (bool, log.Error("AliOSS图片上传失败: " + err.Error()) return false, err.Error() } - return true, fmt.Sprintf("https://%s.%s/%s", configs.BucketName, configs.Endpoint, objectName) + return true, fmt.Sprintf("https://%s.%s/%s", config["bucketName"], config["endPoint"], objectName) +} + +// 将临时图片文件存储到指定的文件夹下 +func MoveImgToPath(imgPath string, targetPath string) (bool, string) { + s := strings.Split(imgPath, "/") + imgName := s[len(s)-1] + newPath := fmt.Sprintf("%s/%s", targetPath, imgName) + if err := os.Rename(imgPath, newPath); err != nil { + return false, configs.SaveImgToLocalErr + } + // return true, imgName + return true, newPath }