|
@@ -18,9 +18,11 @@ import (
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
|
|
+ "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
uuid "github.com/satori/go.uuid"
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
+ "go.uber.org/zap"
|
|
)
|
|
)
|
|
|
|
|
|
// @Summary 读取文件
|
|
// @Summary 读取文件
|
|
@@ -47,7 +49,7 @@ func GetFilerContent(c *gin.Context) {
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- //文件读取任务是将文件内容读取到内存中。
|
|
|
|
|
|
+ // 文件读取任务是将文件内容读取到内存中。
|
|
info, err := ioutil.ReadFile(filePath)
|
|
info, err := ioutil.ReadFile(filePath)
|
|
if err != nil {
|
|
if err != nil {
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{
|
|
@@ -83,7 +85,6 @@ func GetLocalFile(c *gin.Context) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
c.File(path)
|
|
c.File(path)
|
|
- return
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// @Summary download
|
|
// @Summary download
|
|
@@ -96,7 +97,6 @@ func GetLocalFile(c *gin.Context) {
|
|
// @Success 200 {string} string "ok"
|
|
// @Success 200 {string} string "ok"
|
|
// @Router /file/download [get]
|
|
// @Router /file/download [get]
|
|
func GetDownloadFile(c *gin.Context) {
|
|
func GetDownloadFile(c *gin.Context) {
|
|
-
|
|
|
|
t := c.Query("format")
|
|
t := c.Query("format")
|
|
|
|
|
|
files := c.Query("files")
|
|
files := c.Query("files")
|
|
@@ -135,11 +135,11 @@ func GetDownloadFile(c *gin.Context) {
|
|
}
|
|
}
|
|
if !info.IsDir() {
|
|
if !info.IsDir() {
|
|
|
|
|
|
- //打开文件
|
|
|
|
|
|
+ // 打开文件
|
|
fileTmp, _ := os.Open(filePath)
|
|
fileTmp, _ := os.Open(filePath)
|
|
defer fileTmp.Close()
|
|
defer fileTmp.Close()
|
|
|
|
|
|
- //获取文件的名称
|
|
|
|
|
|
+ // 获取文件的名称
|
|
fileName := path.Base(filePath)
|
|
fileName := path.Base(filePath)
|
|
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
|
|
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
|
|
c.File(filePath)
|
|
c.File(filePath)
|
|
@@ -179,7 +179,6 @@ func GetDownloadFile(c *gin.Context) {
|
|
log.Printf("Failed to archive %s: %v", fname, err)
|
|
log.Printf("Failed to archive %s: %v", fname, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
func GetDownloadSingleFile(c *gin.Context) {
|
|
func GetDownloadSingleFile(c *gin.Context) {
|
|
@@ -202,7 +201,7 @@ func GetDownloadSingleFile(c *gin.Context) {
|
|
defer fileTmp.Close()
|
|
defer fileTmp.Close()
|
|
|
|
|
|
fileName := path.Base(filePath)
|
|
fileName := path.Base(filePath)
|
|
- //c.Header("Content-Disposition", "inline")
|
|
|
|
|
|
+ // c.Header("Content-Disposition", "inline")
|
|
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
|
|
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
|
|
c.File(filePath)
|
|
c.File(filePath)
|
|
}
|
|
}
|
|
@@ -248,7 +247,7 @@ func DirPath(c *gin.Context) {
|
|
info[i].Extensions = ex
|
|
info[i].Extensions = ex
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //Hide the files or folders in operation
|
|
|
|
|
|
+ // Hide the files or folders in operation
|
|
fileQueue := make(map[string]string)
|
|
fileQueue := make(map[string]string)
|
|
if len(service.OpStrArr) > 0 {
|
|
if len(service.OpStrArr) > 0 {
|
|
for _, v := range service.OpStrArr {
|
|
for _, v := range service.OpStrArr {
|
|
@@ -361,7 +360,6 @@ func PostCreateFile(c *gin.Context) {
|
|
// @Success 200 {string} string "ok"
|
|
// @Success 200 {string} string "ok"
|
|
// @Router /file/upload [get]
|
|
// @Router /file/upload [get]
|
|
func GetFileUpload(c *gin.Context) {
|
|
func GetFileUpload(c *gin.Context) {
|
|
-
|
|
|
|
relative := c.Query("relativePath")
|
|
relative := c.Query("relativePath")
|
|
fileName := c.Query("filename")
|
|
fileName := c.Query("filename")
|
|
chunkNumber := c.Query("chunkNumber")
|
|
chunkNumber := c.Query("chunkNumber")
|
|
@@ -405,55 +403,92 @@ func PostFileUpload(c *gin.Context) {
|
|
hash := file.GetHashByContent([]byte(fileName))
|
|
hash := file.GetHashByContent([]byte(fileName))
|
|
|
|
|
|
if len(path) == 0 {
|
|
if len(path) == 0 {
|
|
- c.JSON(common_err.INVALID_PARAMS, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
|
|
|
|
|
+ loger.Error("path should not be empty")
|
|
|
|
+ c.JSON(http.StatusBadRequest, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
tempDir := filepath.Join(path, ".temp", hash+strconv.Itoa(totalChunks)) + "/"
|
|
tempDir := filepath.Join(path, ".temp", hash+strconv.Itoa(totalChunks)) + "/"
|
|
|
|
|
|
if fileName != relative {
|
|
if fileName != relative {
|
|
dirPath = strings.TrimSuffix(relative, fileName)
|
|
dirPath = strings.TrimSuffix(relative, fileName)
|
|
tempDir += dirPath
|
|
tempDir += dirPath
|
|
- file.MkDir(path + "/" + dirPath)
|
|
|
|
|
|
+ if err := file.MkDir(path + "/" + dirPath); err != nil {
|
|
|
|
+ loger.Error("error when trying to create `"+path+"/"+dirPath+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
path += "/" + relative
|
|
path += "/" + relative
|
|
|
|
|
|
if !file.CheckNotExist(tempDir + chunkNumber) {
|
|
if !file.CheckNotExist(tempDir + chunkNumber) {
|
|
- file.RMDir(tempDir + chunkNumber)
|
|
|
|
|
|
+ if err := file.RMDir(tempDir + chunkNumber); err != nil {
|
|
|
|
+ loger.Error("error when trying to remove existing `"+tempDir+chunkNumber+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
if totalChunks > 1 {
|
|
if totalChunks > 1 {
|
|
- file.IsNotExistMkDir(tempDir)
|
|
|
|
|
|
+ if err := file.IsNotExistMkDir(tempDir); err != nil {
|
|
|
|
+ loger.Error("error when trying to create `"+tempDir+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ out, err := os.OpenFile(tempDir+chunkNumber, os.O_WRONLY|os.O_CREATE, 0o644)
|
|
|
|
+ if err != nil {
|
|
|
|
+ loger.Error("error when trying to open `"+tempDir+chunkNumber+"` for creation", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
- out, _ := os.OpenFile(tempDir+chunkNumber, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
defer out.Close()
|
|
defer out.Close()
|
|
- _, err := io.Copy(out, f)
|
|
|
|
|
|
+
|
|
|
|
+ if _, err := io.Copy(out, f); err != nil { // recommend to use https://github.com/iceber/iouring-go for faster copy
|
|
|
|
+ loger.Error("error when trying to write to `"+tempDir+chunkNumber+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fileNum, err := ioutil.ReadDir(tempDir)
|
|
if err != nil {
|
|
if err != nil {
|
|
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
|
|
|
|
|
+ loger.Error("error when trying to read number of files under `"+tempDir+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if totalChunks == len(fileNum) {
|
|
|
|
+ if err := file.SpliceFiles(tempDir, path, totalChunks, 1); err != nil {
|
|
|
|
+ loger.Error("error when trying to splice files under `"+tempDir+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := file.RMDir(tempDir); err != nil {
|
|
|
|
+ loger.Error("error when trying to remove `"+tempDir+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- out, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
- defer out.Close()
|
|
|
|
- _, err := io.Copy(out, f)
|
|
|
|
|
|
+ out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o644)
|
|
if err != nil {
|
|
if err != nil {
|
|
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
|
|
|
|
|
+ loger.Error("error when trying to open `"+path+"` for creation", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- fileNum, err := ioutil.ReadDir(tempDir)
|
|
|
|
- if err != nil {
|
|
|
|
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if totalChunks == len(fileNum) {
|
|
|
|
- file.SpliceFiles(tempDir, path, totalChunks, 1)
|
|
|
|
- file.RMDir(tempDir)
|
|
|
|
- }
|
|
|
|
|
|
|
|
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
|
|
|
|
|
+ defer out.Close()
|
|
|
|
+
|
|
|
|
+ if _, err := io.Copy(out, f); err != nil { // recommend to use https://github.com/iceber/iouring-go for faster copy
|
|
|
|
+ loger.Error("error when trying to write to `"+path+"`", zap.Error(err))
|
|
|
|
+ c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
|
}
|
|
}
|
|
|
|
|
|
// @Summary copy or move file
|
|
// @Summary copy or move file
|
|
@@ -465,7 +500,6 @@ func PostFileUpload(c *gin.Context) {
|
|
// @Success 200 {string} string "ok"
|
|
// @Success 200 {string} string "ok"
|
|
// @Router /file/operate [post]
|
|
// @Router /file/operate [post]
|
|
func PostOperateFileOrDir(c *gin.Context) {
|
|
func PostOperateFileOrDir(c *gin.Context) {
|
|
-
|
|
|
|
list := model.FileOperate{}
|
|
list := model.FileOperate{}
|
|
c.ShouldBind(&list)
|
|
c.ShouldBind(&list)
|
|
|
|
|
|
@@ -515,7 +549,6 @@ func PostOperateFileOrDir(c *gin.Context) {
|
|
// @Success 200 {string} string "ok"
|
|
// @Success 200 {string} string "ok"
|
|
// @Router /file/delete [delete]
|
|
// @Router /file/delete [delete]
|
|
func DeleteFile(c *gin.Context) {
|
|
func DeleteFile(c *gin.Context) {
|
|
-
|
|
|
|
paths := []string{}
|
|
paths := []string{}
|
|
c.ShouldBind(&paths)
|
|
c.ShouldBind(&paths)
|
|
if len(paths) == 0 {
|
|
if len(paths) == 0 {
|
|
@@ -547,7 +580,6 @@ func DeleteFile(c *gin.Context) {
|
|
// @Success 200 {string} string "ok"
|
|
// @Success 200 {string} string "ok"
|
|
// @Router /file/update [put]
|
|
// @Router /file/update [put]
|
|
func PutFileContent(c *gin.Context) {
|
|
func PutFileContent(c *gin.Context) {
|
|
-
|
|
|
|
fi := model.FileUpdate{}
|
|
fi := model.FileUpdate{}
|
|
c.ShouldBind(&fi)
|
|
c.ShouldBind(&fi)
|
|
|
|
|
|
@@ -557,7 +589,7 @@ func PutFileContent(c *gin.Context) {
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- //err := os.Remove(path)
|
|
|
|
|
|
+ // err := os.Remove(path)
|
|
err := os.RemoveAll(fi.FilePath)
|
|
err := os.RemoveAll(fi.FilePath)
|
|
if err != nil {
|
|
if err != nil {
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_DELETE_ERROR, Message: common_err.GetMsg(common_err.FILE_DELETE_ERROR), Data: err})
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_DELETE_ERROR, Message: common_err.GetMsg(common_err.FILE_DELETE_ERROR), Data: err})
|