Browse Source

fix:文件加密bug修改

master
刘子旺 6 months ago
parent
commit
790eb99bc8
  1. 17
      godo/files/pwdfile.go

17
godo/files/pwdfile.go

@ -39,17 +39,11 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) {
} }
defer fileContent.Close() defer fileContent.Close()
fileData, err := io.ReadAll(fileContent) fileData, err := io.ReadAll(fileContent)
fmt.Println("表单数据为:", string(fileData))
if err != nil { if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, err.Error()) libs.HTTPError(w, http.StatusInternalServerError, err.Error())
return return
} }
configPwd, ishas := libs.GetConfig("filePwd") configPwd, ishas := libs.GetConfig("filePwd")
configPwdStr, ok := configPwd.(string)
if !ok {
libs.HTTPError(w, http.StatusInternalServerError, "配置文件密码格式错误")
return
}
if !ishas { if !ishas {
// 没开启加密,直接明文写入 // 没开启加密,直接明文写入
_, err := newFile.Write(fileData) _, err := newFile.Write(fileData)
@ -63,8 +57,13 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) {
} }
libs.SuccessMsg(w, "", "文件写入成功") libs.SuccessMsg(w, "", "文件写入成功")
return return
} } else {
// 开启加密后,写入加密数据 // 开启加密后,写入加密数据
configPwdStr, ok := configPwd.(string)
if !ok {
libs.HTTPError(w, http.StatusInternalServerError, "配置文件密码格式错误")
return
}
_, err = newFile.WriteString(fmt.Sprintf("@%s@", configPwdStr)) _, err = newFile.WriteString(fmt.Sprintf("@%s@", configPwdStr))
if err != nil { if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, "密码写入失败") libs.HTTPError(w, http.StatusInternalServerError, "密码写入失败")
@ -86,6 +85,7 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) {
} }
libs.SuccessMsg(w, "", "文件写入成功") libs.SuccessMsg(w, "", "文件写入成功")
} }
}
func HandleReadFile(w http.ResponseWriter, r *http.Request) { func HandleReadFile(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path") path := r.URL.Query().Get("path")
@ -153,7 +153,7 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) {
res := libs.APIResponse{Message: "加密文件读取成功", Data: content} res := libs.APIResponse{Message: "加密文件读取成功", Data: content}
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return
} } else {
// Pwd不为空,Pwd与文件密码做比对 // Pwd不为空,Pwd与文件密码做比对
if Pwd != filePwd { if Pwd != filePwd {
res := libs.APIResponse{Message: "密码错误,请输入正确的密码", Code: -1, Error: "needPwd"} res := libs.APIResponse{Message: "密码错误,请输入正确的密码", Code: -1, Error: "needPwd"}
@ -169,6 +169,7 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) {
res := libs.APIResponse{Message: "加密文件读取成功", Data: content} res := libs.APIResponse{Message: "加密文件读取成功", Data: content}
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }
}
func HandleSetFilePwd(w http.ResponseWriter, r *http.Request) { func HandleSetFilePwd(w http.ResponseWriter, r *http.Request) {
Pwd := r.Header.Get("Pwd") Pwd := r.Header.Get("Pwd")

Loading…
Cancel
Save