文件 读取 和 保存 api 调整

实现 markdown 文件读写
This commit is contained in:
shikong 2024-02-19 14:30:13 +08:00
parent d96232128a
commit 787db345cd
8 changed files with 168 additions and 9 deletions

View File

@ -8,10 +8,15 @@ export default http;
export interface ReadFileParams { export interface ReadFileParams {
chunkSize?: number chunkSize?: number
offset?: number, offset?: number
path: string path: string
} }
export interface SaveFileParams {
fileName: string
data: Blob
}
export class Api { export class Api {
server:string server:string
constructor(server:string) { constructor(server:string) {
@ -19,10 +24,17 @@ export class Api {
} }
readFile(params: ReadFileParams){ readFile(params: ReadFileParams){
return http.get(`${this.server}/s/file/`,{ return http.get(`${this.server}/s/file/get`,{
params: params params: params
}) })
} }
saveFile(params: SaveFileParams){
return http.postForm(`${this.server}/s/file/save`,{
fileName: params.fileName,
fileContent: params.data
})
}
} }
let api = new Api("") let api = new Api("")

View File

@ -24,8 +24,9 @@ import tableMergedCell from "@toast-ui/editor-plugin-table-merged-cell"
import uml from "@toast-ui/editor-plugin-uml" import uml from "@toast-ui/editor-plugin-uml"
import {useI18n} from "vue-i18n"; 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 {useApi} from "@/api";
import {ElMessage} from "element-plus";
const emit = defineEmits([ const emit = defineEmits([
"update:modelValue" "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)
}
})
})
}
</script> </script>
<template> <template>
<el-button type="primary" @click="openFile">打开</el-button> <el-button type="primary" @click="openFile">打开</el-button>
<el-button type="success">保存</el-button> <el-button type="success" @click="saveFile">保存</el-button>
<div ref="editorRef" class="overscroll-none"/> <div ref="editorRef" class="overscroll-none"/>
</template> </template>

View File

@ -55,7 +55,7 @@ func GetPath(ctx *gin.Context) {
// @Accept json // @Accept json
// @Produce octet-stream // @Produce octet-stream
// @Param vo query controller.GetFileDto true "获取文件" // @Param vo query controller.GetFileDto true "获取文件"
// @Router /file/ [get] // @Router /file/get [get]
func GetFile(ctx *gin.Context) { func GetFile(ctx *gin.Context) {
dto := &GetFileDto{} dto := &GetFileDto{}
err := ctx.BindQuery(dto) err := ctx.BindQuery(dto)
@ -86,3 +86,27 @@ func GetFile(ctx *gin.Context) {
return 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("保存成功")))
}
}

View File

@ -171,7 +171,7 @@ const docTemplate = `{
} }
} }
}, },
"/file/": { "/file/get": {
"get": { "get": {
"description": "获取文件", "description": "获取文件",
"consumes": [ "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": { "/wol/wakeup": {
"post": { "post": {
"description": "wol 唤醒", "description": "wol 唤醒",

View File

@ -164,7 +164,7 @@
} }
} }
}, },
"/file/": { "/file/get": {
"get": { "get": {
"description": "获取文件", "description": "获取文件",
"consumes": [ "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": { "/wol/wakeup": {
"post": { "post": {
"description": "wol 唤醒", "description": "wol 唤醒",

View File

@ -157,7 +157,7 @@ paths:
summary: 错误处理 summary: 错误处理
tags: tags:
- Error - Error
/file/: /file/get:
get: get:
consumes: consumes:
- application/json - application/json
@ -214,6 +214,28 @@ paths:
summary: 获取文件路径 summary: 获取文件路径
tags: tags:
- File - 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: /wol/wakeup:
post: post:
consumes: consumes:

View File

@ -26,8 +26,9 @@ func setupConfigRouter(g *gin.RouterGroup) {
func setupFileRouter(g *gin.RouterGroup) { func setupFileRouter(g *gin.RouterGroup) {
file := g.Group("/file") file := g.Group("/file")
file.GET("/", controller.GetFile) file.GET("/get", controller.GetFile)
file.GET("/path", controller.GetPath) file.GET("/path", controller.GetPath)
file.POST("/save", controller.SaveFile)
} }
func setupWolRouter(g *gin.RouterGroup) { func setupWolRouter(g *gin.RouterGroup) {

3
pkg/server/swag.go Normal file
View File

@ -0,0 +1,3 @@
package server
//go:generate swag init -pd ./