123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- package v1
- import (
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "net/http"
- "net/url"
- url2 "net/url"
- "os"
- "path"
- "path/filepath"
- "strconv"
- "strings"
- "sync"
- "github.com/IceWhaleTech/CasaOS/model"
- "github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
- "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
- "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
- "github.com/IceWhaleTech/CasaOS/service"
- "github.com/gin-gonic/gin"
- uuid "github.com/satori/go.uuid"
- "go.uber.org/zap"
- )
- // @Summary 读取文件
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path query string true "路径"
- // @Success 200 {string} string "ok"
- // @Router /file/read [get]
- func GetFilerContent(c *gin.Context) {
- filePath := c.Query("path")
- if len(filePath) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{
- Success: common_err.INVALID_PARAMS,
- Message: common_err.GetMsg(common_err.INVALID_PARAMS),
- })
- return
- }
- if !file.Exists(filePath) {
- c.JSON(common_err.SERVICE_ERROR, model.Result{
- Success: common_err.FILE_DOES_NOT_EXIST,
- Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST),
- })
- return
- }
- // 文件读取任务是将文件内容读取到内存中。
- info, err := ioutil.ReadFile(filePath)
- if err != nil {
- c.JSON(common_err.SERVICE_ERROR, model.Result{
- Success: common_err.FILE_READ_ERROR,
- Message: common_err.GetMsg(common_err.FILE_READ_ERROR),
- Data: err.Error(),
- })
- return
- }
- result := string(info)
- c.JSON(common_err.SUCCESS, model.Result{
- Success: common_err.SUCCESS,
- Message: common_err.GetMsg(common_err.SUCCESS),
- Data: result,
- })
- }
- func GetLocalFile(c *gin.Context) {
- path := c.Query("path")
- if len(path) == 0 {
- c.JSON(http.StatusOK, model.Result{
- Success: common_err.INVALID_PARAMS,
- Message: common_err.GetMsg(common_err.INVALID_PARAMS),
- })
- return
- }
- if !file.Exists(path) {
- c.JSON(http.StatusOK, model.Result{
- Success: common_err.FILE_DOES_NOT_EXIST,
- Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST),
- })
- return
- }
- c.File(path)
- }
- // @Summary download
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param format query string false "Compression format" Enums(zip,tar,targz)
- // @Param files query string true "file list eg: filename1,filename2,filename3 "
- // @Success 200 {string} string "ok"
- // @Router /file/download [get]
- func GetDownloadFile(c *gin.Context) {
- t := c.Query("format")
- files := c.Query("files")
- if len(files) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{
- Success: common_err.INVALID_PARAMS,
- Message: common_err.GetMsg(common_err.INVALID_PARAMS),
- })
- return
- }
- list := strings.Split(files, ",")
- for _, v := range list {
- if !file.Exists(v) {
- c.JSON(common_err.SERVICE_ERROR, model.Result{
- Success: common_err.FILE_DOES_NOT_EXIST,
- Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST),
- })
- return
- }
- }
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Transfer-Encoding", "binary")
- c.Header("Cache-Control", "no-cache")
- // handles only single files not folders and multiple files
- if len(list) == 1 {
- filePath := list[0]
- info, err := os.Stat(filePath)
- if err != nil {
- c.JSON(http.StatusOK, model.Result{
- Success: common_err.FILE_DOES_NOT_EXIST,
- Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST),
- })
- return
- }
- if !info.IsDir() {
- // 打开文件
- fileTmp, _ := os.Open(filePath)
- defer fileTmp.Close()
- // 获取文件的名称
- fileName := path.Base(filePath)
- c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
- c.File(filePath)
- return
- }
- }
- extension, ar, err := file.GetCompressionAlgorithm(t)
- if err != nil {
- c.JSON(common_err.CLIENT_ERROR, model.Result{
- Success: common_err.INVALID_PARAMS,
- Message: common_err.GetMsg(common_err.INVALID_PARAMS),
- })
- return
- }
- err = ar.Create(c.Writer)
- 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
- }
- defer ar.Close()
- commonDir := file.CommonPrefix(filepath.Separator, list...)
- currentPath := filepath.Base(commonDir)
- name := "_" + currentPath
- name += extension
- c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
- for _, fname := range list {
- err = file.AddFile(ar, fname, commonDir)
- if err != nil {
- log.Printf("Failed to archive %s: %v", fname, err)
- }
- }
- }
- func GetDownloadSingleFile(c *gin.Context) {
- filePath := c.Query("path")
- if len(filePath) == 0 {
- c.JSON(service.ClientCount, model.Result{
- Success: common_err.INVALID_PARAMS,
- Message: common_err.GetMsg(common_err.INVALID_PARAMS),
- })
- return
- }
- fileTmp, err := os.Open(filePath)
- if err != nil {
- c.JSON(common_err.SERVICE_ERROR, model.Result{
- Success: common_err.FILE_DOES_NOT_EXIST,
- Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST),
- })
- return
- }
- defer fileTmp.Close()
- fileName := path.Base(filePath)
- // c.Header("Content-Disposition", "inline")
- c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
- c.File(filePath)
- }
- // @Summary 获取目录列表
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path query string false "路径"
- // @Success 200 {string} string "ok"
- // @Router /file/dirpath [get]
- func DirPath(c *gin.Context) {
- path := c.DefaultQuery("path", "")
- info := service.MyService.System().GetDirPath(path)
- shares := service.MyService.Shares().GetSharesList()
- sharesMap := make(map[string]string)
- for _, v := range shares {
- sharesMap[v.Path] = fmt.Sprint(v.ID)
- }
- if path == "/DATA/AppData" {
- list := service.MyService.Docker().DockerContainerList()
- apps := make(map[string]string, len(list))
- for _, v := range list {
- apps[strings.ReplaceAll(v.Names[0], "/", "")] = strings.ReplaceAll(v.Names[0], "/", "")
- }
- for i := 0; i < len(info); i++ {
- if v, ok := apps[info[i].Name]; ok {
- info[i].Label = v
- info[i].Type = "application"
- }
- }
- }
- for i := 0; i < len(info); i++ {
- if v, ok := sharesMap[info[i].Path]; ok {
- ex := make(map[string]interface{})
- shareEx := make(map[string]string)
- shareEx["shared"] = "true"
- shareEx["id"] = v
- ex["share"] = shareEx
- info[i].Extensions = ex
- }
- }
- // Hide the files or folders in operation
- fileQueue := make(map[string]string)
- if len(service.OpStrArr) > 0 {
- for _, v := range service.OpStrArr {
- v, ok := service.FileQueue.Load(v)
- if !ok {
- continue
- }
- vt := v.(model.FileOperate)
- for _, i := range vt.Item {
- lastPath := i.From[strings.LastIndex(i.From, "/")+1:]
- fileQueue[vt.To+"/"+lastPath] = i.From
- }
- }
- }
- pathList := []model.Path{}
- for i := 0; i < len(info); i++ {
- if info[i].Name == ".temp" && info[i].IsDir {
- continue
- }
- if _, ok := fileQueue[info[i].Path]; !ok {
- pathList = append(pathList, info[i])
- }
- }
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: pathList})
- }
- // @Summary rename file or dir
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param oldpath body string true "path of old"
- // @Param newpath body string true "path of new"
- // @Success 200 {string} string "ok"
- // @Router /file/rename [put]
- func RenamePath(c *gin.Context) {
- json := make(map[string]string)
- c.ShouldBind(&json)
- op := json["old_path"]
- np := json["new_path"]
- if len(op) == 0 || len(np) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- return
- }
- success, err := service.MyService.System().RenameFile(op, np)
- c.JSON(common_err.SUCCESS, model.Result{Success: success, Message: common_err.GetMsg(success), Data: err})
- }
- // @Summary create folder
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path body string true "path of folder"
- // @Success 200 {string} string "ok"
- // @Router /file/mkdir [post]
- func MkdirAll(c *gin.Context) {
- json := make(map[string]string)
- c.ShouldBind(&json)
- path := json["path"]
- var code int
- if len(path) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- return
- }
- // decodedPath, err := url.QueryUnescape(path)
- // if err != nil {
- // c.JSON(http.StatusOK, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- // return
- // }
- code, _ = service.MyService.System().MkdirAll(path)
- c.JSON(common_err.SUCCESS, model.Result{Success: code, Message: common_err.GetMsg(code)})
- }
- // @Summary create file
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path body string true "path of folder (path need to url encode)"
- // @Success 200 {string} string "ok"
- // @Router /file/create [post]
- func PostCreateFile(c *gin.Context) {
- json := make(map[string]string)
- c.ShouldBind(&json)
- path := json["path"]
- var code int
- if len(path) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- return
- }
- // decodedPath, err := url.QueryUnescape(path)
- // if err != nil {
- // c.JSON(http.StatusOK, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- // return
- // }
- code, _ = service.MyService.System().CreateFile(path)
- c.JSON(common_err.SUCCESS, model.Result{Success: code, Message: common_err.GetMsg(code)})
- }
- // @Summary upload file
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path formData string false "file path"
- // @Param file formData file true "file"
- // @Success 200 {string} string "ok"
- // @Router /file/upload [get]
- func GetFileUpload(c *gin.Context) {
- relative := c.Query("relativePath")
- fileName := c.Query("filename")
- chunkNumber := c.Query("chunkNumber")
- totalChunks, _ := strconv.Atoi(c.DefaultQuery("totalChunks", "0"))
- path := c.Query("path")
- dirPath := ""
- hash := file.GetHashByContent([]byte(fileName))
- tempDir := filepath.Join(path, ".temp", hash+strconv.Itoa(totalChunks)) + "/"
- if fileName != relative {
- dirPath = strings.TrimSuffix(relative, fileName)
- tempDir += dirPath
- file.MkDir(path + "/" + dirPath)
- }
- tempDir += chunkNumber
- if !file.CheckNotExist(tempDir) {
- c.JSON(200, model.Result{Success: 200, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
- return
- }
- c.JSON(204, model.Result{Success: 204, Message: common_err.GetMsg(common_err.SUCCESS)})
- }
- // @Summary upload file
- // @Produce application/json
- // @Accept multipart/form-data
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path formData string false "file path"
- // @Param file formData file true "file"
- // @Success 200 {string} string "ok"
- // @Router /file/upload [post]
- func PostFileUpload(c *gin.Context) {
- f, _, _ := c.Request.FormFile("file")
- relative := c.PostForm("relativePath")
- fileName := c.PostForm("filename")
- totalChunks, _ := strconv.Atoi(c.DefaultPostForm("totalChunks", "0"))
- chunkNumber := c.PostForm("chunkNumber")
- dirPath := ""
- path := c.PostForm("path")
- hash := file.GetHashByContent([]byte(fileName))
- if len(path) == 0 {
- 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
- }
- tempDir := filepath.Join(path, ".temp", hash+strconv.Itoa(totalChunks)) + "/"
- if fileName != relative {
- dirPath = strings.TrimSuffix(relative, fileName)
- tempDir += 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
- if !file.CheckNotExist(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 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
- }
- 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 `"+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 {
- 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
- }
- 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 {
- out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o644)
- if err != nil {
- 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
- }
- 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
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param body body model.FileOperate true "type:move,copy"
- // @Success 200 {string} string "ok"
- // @Router /file/operate [post]
- func PostOperateFileOrDir(c *gin.Context) {
- list := model.FileOperate{}
- c.ShouldBind(&list)
- if len(list.Item) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- return
- }
- if list.To == list.Item[0].From[:strings.LastIndex(list.Item[0].From, "/")] {
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SOURCE_DES_SAME, Message: common_err.GetMsg(common_err.SOURCE_DES_SAME)})
- return
- }
- var total int64 = 0
- for i := 0; i < len(list.Item); i++ {
- size, err := file.GetFileOrDirSize(list.Item[i].From)
- if err != nil {
- continue
- }
- list.Item[i].Size = size
- total += size
- }
- list.TotalSize = total
- list.ProcessedSize = 0
- uid := uuid.NewV4().String()
- service.FileQueue.Store(uid, list)
- service.OpStrArr = append(service.OpStrArr, uid)
- if len(service.OpStrArr) == 1 {
- go service.ExecOpFile()
- go service.CheckFileStatus()
- go service.MyService.Notify().SendFileOperateNotify(false)
- }
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
- }
- // @Summary delete file
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param body body string true "paths eg ["/a/b/c","/d/e/f"]"
- // @Success 200 {string} string "ok"
- // @Router /file/delete [delete]
- func DeleteFile(c *gin.Context) {
- paths := []string{}
- c.ShouldBind(&paths)
- if len(paths) == 0 {
- c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
- return
- }
- // path := c.Query("path")
- // paths := strings.Split(path, ",")
- for _, v := range paths {
- err := os.RemoveAll(v)
- 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})
- return
- }
- }
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
- }
- // @Summary update file
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path body string true "path"
- // @Param content body string true "content"
- // @Success 200 {string} string "ok"
- // @Router /file/update [put]
- func PutFileContent(c *gin.Context) {
- fi := model.FileUpdate{}
- c.ShouldBind(&fi)
- // path := c.PostForm("path")
- // content := c.PostForm("content")
- if !file.Exists(fi.FilePath) {
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
- return
- }
- // err := os.Remove(path)
- err := os.RemoveAll(fi.FilePath)
- 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})
- return
- }
- err = file.CreateFileAndWriteContent(fi.FilePath, fi.FileContent)
- 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
- }
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
- }
- // @Summary image thumbnail/original image
- // @Produce application/json
- // @Accept application/json
- // @Tags file
- // @Security ApiKeyAuth
- // @Param path query string true "path"
- // @Param type query string false "original,thumbnail" Enums(original,thumbnail)
- // @Success 200 {string} string "ok"
- // @Router /file/image [get]
- func GetFileImage(c *gin.Context) {
- t := c.Query("type")
- path := c.Query("path")
- if !file.Exists(path) {
- c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.FILE_ALREADY_EXISTS)})
- return
- }
- if t == "thumbnail" {
- f, err := file.GetImage(path, 100, 0)
- 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
- }
- c.Writer.WriteString(string(f))
- return
- }
- f, err := os.Open(path)
- 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
- }
- defer f.Close()
- data, err := ioutil.ReadAll(f)
- 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
- }
- c.Writer.WriteString(string(data))
- }
- func DeleteOperateFileOrDir(c *gin.Context) {
- id := c.Param("id")
- if id == "0" {
- service.FileQueue = sync.Map{}
- service.OpStrArr = []string{}
- } else {
- service.FileQueue.Delete(id)
- tempList := []string{}
- for _, v := range service.OpStrArr {
- if v != id {
- tempList = append(tempList, v)
- }
- }
- service.OpStrArr = tempList
- }
- go service.MyService.Notify().SendFileOperateNotify(true)
- c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
- }
|