70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package k8s_manager
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
func GetFiles(c *gin.Context) {
|
|
username := c.MustGet("username").(string)
|
|
appName := c.Param("app")
|
|
dirPath := path.Join(config.NFSPath, username, appName)
|
|
dir, err := os.ReadDir(dirPath)
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
log.WithError(err).Error("Cannot ReadDir ", dirPath)
|
|
c.JSON(http.StatusBadRequest, gin.H{"ok": false, "msg": err})
|
|
return
|
|
}
|
|
}
|
|
log.Info(dir)
|
|
ans := make([]string, len(dir))
|
|
for i, d := range dir {
|
|
ans[i] = d.Name()
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true, "files": ans})
|
|
}
|
|
|
|
func GetPostOrDeleteFile(c *gin.Context) {
|
|
username := c.MustGet("username").(string)
|
|
appName := c.Param("app")
|
|
fileName := c.Param("filename")
|
|
dirPath := path.Join(config.NFSPath, username, appName)
|
|
filePath := path.Join(config.NFSPath, username, appName, fileName)
|
|
if c.Request.Method == http.MethodGet {
|
|
c.FileAttachment(filePath, fileName)
|
|
} else if c.Request.Method == http.MethodPost {
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
log.WithError(err).Error("GetPostOrDeleteFile: Cannot get file")
|
|
c.JSON(http.StatusBadRequest, gin.H{"ok": false, "msg": err})
|
|
return
|
|
}
|
|
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
|
err := os.MkdirAll(dirPath, 0777)
|
|
if err != nil {
|
|
log.WithError(err).Error("GetPostOrDeleteFile: Make nfs dir error")
|
|
}
|
|
}
|
|
log.Infof("Receive file name: %s, size: %d, path: %s", file.Filename, file.Size, filePath)
|
|
err = c.SaveUploadedFile(file, filePath)
|
|
if err != nil {
|
|
log.WithError(err).Error("GetPostOrDeleteFile: Save file error")
|
|
c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "msg": err})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
} else if c.Request.Method == http.MethodDelete {
|
|
err := os.Remove(filePath)
|
|
if err != nil {
|
|
log.WithError(err).Error("GetPostOrDeleteFile: Remove file error")
|
|
c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "msg": err})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
}
|