|
|
@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"goskeleton/app/global/consts"
|
|
|
|
|
|
|
|
"goskeleton/app/service/file"
|
|
|
|
|
|
|
|
"goskeleton/app/utils/response"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建文件
|
|
|
|
|
|
|
|
func (f *File) Creat(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
file_Path := context.GetString(consts.ValidatorPrefix + "file_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s.docx", file_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//file, err := os.Create(final_Path)
|
|
|
|
|
|
|
|
if err := file.CreateFile(user_name, final_Path); err != nil {
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件创建成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件创建成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建文件夹
|
|
|
|
|
|
|
|
func (f *File) FolderCreat(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件夹名,并拼接获得最终文件夹路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
folder_Path := context.GetString(consts.ValidatorPrefix + "folder_path")
|
|
|
|
|
|
|
|
folder_Name := context.GetString(consts.ValidatorPrefix + "folder_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s", folder_Path, folder_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := file.CreateFolder(user_name, final_Path); err != nil {
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件创建成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件夹创建成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 打开文件
|
|
|
|
|
|
|
|
func (f *File) Get(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
file_Path := context.GetString(consts.ValidatorPrefix + "file_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s.docx", file_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileObj, err := file.GetFile(user_name, final_Path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// 根据不同的错误类型,返回更具体、合适的错误响应给客户端
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
|
|
|
|
case *os.PathError:
|
|
|
|
|
|
|
|
//文件路径错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFilePathErrorCode, consts.CurdFilePathErrorMsg, err.Error())
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
|
|
|
|
//文件不存在错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fileObj.Close() // 确保文件使用完后被关闭
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件获取成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件获取成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件重命名
|
|
|
|
|
|
|
|
func (f *File) Rename(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
file_Path := context.GetString(consts.ValidatorPrefix + "file_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s.docx", file_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//old_Name := context.GetString(consts.ValidatorPrefix + "oldname")
|
|
|
|
|
|
|
|
new_Name := context.GetString(consts.ValidatorPrefix + "newname")
|
|
|
|
|
|
|
|
new_finalpath := fmt.Sprintf("%s/%s.docx", file_Path, new_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := file.FileRename(user_name, final_Path, new_finalpath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// 根据不同的错误类型,返回更具体、合适的错误响应给客户端
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
|
|
|
|
case *os.PathError:
|
|
|
|
|
|
|
|
//文件路径错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFilePathErrorCode, consts.CurdFilePathErrorMsg, err.Error())
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
|
|
|
|
//文件不存在错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件重命名成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件重命名成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件移动
|
|
|
|
|
|
|
|
func (f *File) Move(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
file_Path := context.GetString(consts.ValidatorPrefix + "file_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s.docx", file_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//old_Name := context.GetString(consts.ValidatorPrefix + "oldname")
|
|
|
|
|
|
|
|
new_Path := context.GetString(consts.ValidatorPrefix + "newpath")
|
|
|
|
|
|
|
|
new_finalpath := fmt.Sprintf("%s/%s.docx", new_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := file.FileMove(user_name, final_Path, new_finalpath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// 根据不同的错误类型,返回更具体、合适的错误响应给客户端
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
|
|
|
|
case *os.PathError:
|
|
|
|
|
|
|
|
//文件路径错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFilePathErrorCode, consts.CurdFilePathErrorMsg, err.Error())
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
|
|
|
|
//文件不存在错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件移动成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件移动成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件保存
|
|
|
|
|
|
|
|
func (f *File) Save(context *gin.Context) {
|
|
|
|
|
|
|
|
//从context获得路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
Save_path := context.GetString(consts.ValidatorPrefix + "save_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_savePath := fmt.Sprintf("%s/%s.docx", Save_path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileDataBase64 := context.PostForm("file_data")
|
|
|
|
|
|
|
|
if fileDataBase64 == "" {
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := file.FileSave(user_name, final_savePath, fileDataBase64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// 根据不同的错误类型,返回更具体、合适的错误响应给客户端
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
|
|
|
|
case *os.PathError:
|
|
|
|
|
|
|
|
//文件路径错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFilePathErrorCode, consts.CurdFilePathErrorMsg, err.Error())
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
|
|
|
|
//文件不存在错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件保存成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件保存成功")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件删除
|
|
|
|
|
|
|
|
func (f *File) Delete(context *gin.Context) {
|
|
|
|
|
|
|
|
// 从context获得用户名、文件路径和文件名,并拼接获得最终文件路径
|
|
|
|
|
|
|
|
user_name := context.GetString(consts.ValidatorPrefix + "user_name")
|
|
|
|
|
|
|
|
file_Path := context.GetString(consts.ValidatorPrefix + "file_path")
|
|
|
|
|
|
|
|
file_Name := context.GetString(consts.ValidatorPrefix + "file_name")
|
|
|
|
|
|
|
|
final_Path := fmt.Sprintf("%s/%s.docx", file_Path, file_Name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := file.FileDe(user_name, final_Path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// 根据不同的错误类型,返回更具体、合适的错误响应给客户端
|
|
|
|
|
|
|
|
switch err := err.(type) {
|
|
|
|
|
|
|
|
case *os.PathError:
|
|
|
|
|
|
|
|
//文件路径错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFilePathErrorCode, consts.CurdFilePathErrorMsg, err.Error())
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
if err == os.ErrNotExist {
|
|
|
|
|
|
|
|
//文件不存在错误
|
|
|
|
|
|
|
|
response.Fail(context, consts.CurdFileNotExistCode, consts.CurdFileNotExistMsg, err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件删除成功,返回成功响应给客户端
|
|
|
|
|
|
|
|
response.Success(context, consts.CurdStatusOkMsg, "文件删除成功")
|
|
|
|
|
|
|
|
}
|