diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index 6f65ea9..a99e582 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -8,10 +8,15 @@ export default http; export interface ReadFileParams { chunkSize?: number - offset?: number, + offset?: number path: string } +export interface SaveFileParams { + fileName: string + data: Blob +} + export class Api { server:string constructor(server:string) { @@ -19,10 +24,17 @@ export class Api { } readFile(params: ReadFileParams){ - return http.get(`${this.server}/s/file/`,{ + return http.get(`${this.server}/s/file/get`,{ params: params }) } + + saveFile(params: SaveFileParams){ + return http.postForm(`${this.server}/s/file/save`,{ + fileName: params.fileName, + fileContent: params.data + }) + } } let api = new Api("") diff --git a/frontend/src/components/markdown/tui-editor.vue b/frontend/src/components/markdown/tui-editor.vue index dc171a2..d699920 100644 --- a/frontend/src/components/markdown/tui-editor.vue +++ b/frontend/src/components/markdown/tui-editor.vue @@ -24,8 +24,9 @@ import tableMergedCell from "@toast-ui/editor-plugin-table-merged-cell" import uml from "@toast-ui/editor-plugin-uml" import {useI18n} from "vue-i18n"; -import {OpenFileDialog} from "../../../wailsjs/go/core/App"; +import {OpenFileDialog, SaveFileDialog} from "../../../wailsjs/go/core/App"; import {useApi} from "@/api"; +import {ElMessage} from "element-plus"; const emit = defineEmits([ "update:modelValue" @@ -126,11 +127,43 @@ function openFile(){ }) }) } + +function saveFile(){ + SaveFileDialog({ + title: "保存 markdown", + filters: [ + { + displayName: "Markdown", + pattern: "*.md;*.markdown" + } + ] + }).then(path=>{ + console.log(path) + + if(!path){ + return + } + + useApi().saveFile({ + fileName: path, + data: new Blob([editor.getMarkdown()], { + type: 'text/plain' + }) + }).then(resp=>{ + let res = resp.data + if(res.code === 200){ + ElMessage.success(res.data) + } else { + ElMessage.error(res.msg) + } + }) + }) +} diff --git a/pkg/server/controller/file.go b/pkg/server/controller/file.go index 8284961..c7d9b11 100644 --- a/pkg/server/controller/file.go +++ b/pkg/server/controller/file.go @@ -55,7 +55,7 @@ func GetPath(ctx *gin.Context) { // @Accept json // @Produce octet-stream // @Param vo query controller.GetFileDto true "获取文件" -// @Router /file/ [get] +// @Router /file/get [get] func GetFile(ctx *gin.Context) { dto := &GetFileDto{} err := ctx.BindQuery(dto) @@ -86,3 +86,27 @@ func GetFile(ctx *gin.Context) { return } } + +// SaveFile +// @Summary 保存文件 +// @Description 保存文件 +// @Tags File +// @Accept multipart/form-data +// @Produce json +// @Param fileContent formData file true "文件内容" +// @Param fileName formData string true "文件名" +// @Router /file/save [get] +func SaveFile(ctx *gin.Context) { + // 1.获取文件名 + fileName := ctx.PostForm("fileName") + + // 2.获取文件内容 + fileContent, _ := ctx.FormFile("fileContent") + err := ctx.SaveUploadedFile(fileContent, fileName) + if err != nil { + ctx.JSON(500, errorx.NewDefaultError(err.Error())) + return + } else { + ctx.JSON(200, response.NewResponse(fmt.Sprintf("保存成功"))) + } +} diff --git a/pkg/server/docs/docs.go b/pkg/server/docs/docs.go index 70a188a..02401b0 100644 --- a/pkg/server/docs/docs.go +++ b/pkg/server/docs/docs.go @@ -171,7 +171,7 @@ const docTemplate = `{ } } }, - "/file/": { + "/file/get": { "get": { "description": "获取文件", "consumes": [ @@ -260,6 +260,38 @@ const docTemplate = `{ } } }, + "/file/save": { + "get": { + "description": "保存文件", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "File" + ], + "summary": "保存文件", + "parameters": [ + { + "type": "file", + "description": "文件内容", + "name": "fileContent", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "文件名", + "name": "fileName", + "in": "formData", + "required": true + } + ], + "responses": {} + } + }, "/wol/wakeup": { "post": { "description": "wol 唤醒", diff --git a/pkg/server/docs/swagger.json b/pkg/server/docs/swagger.json index e121594..2da4b13 100644 --- a/pkg/server/docs/swagger.json +++ b/pkg/server/docs/swagger.json @@ -164,7 +164,7 @@ } } }, - "/file/": { + "/file/get": { "get": { "description": "获取文件", "consumes": [ @@ -253,6 +253,38 @@ } } }, + "/file/save": { + "get": { + "description": "保存文件", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "File" + ], + "summary": "保存文件", + "parameters": [ + { + "type": "file", + "description": "文件内容", + "name": "fileContent", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "文件名", + "name": "fileName", + "in": "formData", + "required": true + } + ], + "responses": {} + } + }, "/wol/wakeup": { "post": { "description": "wol 唤醒", diff --git a/pkg/server/docs/swagger.yaml b/pkg/server/docs/swagger.yaml index d6e3ff9..10ac34d 100644 --- a/pkg/server/docs/swagger.yaml +++ b/pkg/server/docs/swagger.yaml @@ -157,7 +157,7 @@ paths: summary: 错误处理 tags: - Error - /file/: + /file/get: get: consumes: - application/json @@ -214,6 +214,28 @@ paths: summary: 获取文件路径 tags: - File + /file/save: + get: + consumes: + - multipart/form-data + description: 保存文件 + parameters: + - description: 文件内容 + in: formData + name: fileContent + required: true + type: file + - description: 文件名 + in: formData + name: fileName + required: true + type: string + produces: + - application/json + responses: {} + summary: 保存文件 + tags: + - File /wol/wakeup: post: consumes: diff --git a/pkg/server/router/router.go b/pkg/server/router/router.go index bd18216..1b4f230 100644 --- a/pkg/server/router/router.go +++ b/pkg/server/router/router.go @@ -26,8 +26,9 @@ func setupConfigRouter(g *gin.RouterGroup) { func setupFileRouter(g *gin.RouterGroup) { file := g.Group("/file") - file.GET("/", controller.GetFile) + file.GET("/get", controller.GetFile) file.GET("/path", controller.GetPath) + file.POST("/save", controller.SaveFile) } func setupWolRouter(g *gin.RouterGroup) { diff --git a/pkg/server/swag.go b/pkg/server/swag.go new file mode 100644 index 0000000..096f02f --- /dev/null +++ b/pkg/server/swag.go @@ -0,0 +1,3 @@ +package server + +//go:generate swag init -pd ./