file operate

This commit is contained in:
w-mj 2023-03-12 22:27:24 +08:00
parent 757a4b93c5
commit b391bca67f
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
3 changed files with 64 additions and 2 deletions

View File

@ -1,5 +1,5 @@
build:
image: docker:latest
image: docker:23.0.1-dind
stage: build
tags:
- dind-runner
@ -12,7 +12,7 @@ build:
deploy:
image:
name: bitnami/kubectl:latest
name: bitnami/kubectl:1.26.2
entrypoint: [""]
stage: deploy
only:

View File

@ -45,6 +45,12 @@ func main() {
r.GET("/login", LoginToken)
r.Any("/test_token", TestToken)
r.GET("/username", m.JWTAuthMiddleware, m.GetUsername)
r.GET("/files/:app", m.JWTAuthMiddleware, m.GetFiles)
r.GET("/file/:app/:filename", m.JWTAuthMiddleware, m.GetPostOrDeleteFile)
r.POST("/file/:app/:filename", m.JWTAuthMiddleware, m.GetPostOrDeleteFile)
r.DELETE("/file/:app/:filename", m.JWTAuthMiddleware, m.GetPostOrDeleteFile)
err := r.Run(":8000")
if err != nil {
log.Fatal(err)

View File

@ -0,0 +1,56 @@
package k8s_manager
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
"io/fs"
"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 {
log.WithError(err).Error("Cannot ReadDir ", dirPath)
c.JSON(http.StatusBadRequest, gin.H{"ok": false, "msg": err})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true, "files": funk.Map(dir, func(d fs.DirEntry) string { return d.Name() }).([]string)})
}
func GetPostOrDeleteFile(c *gin.Context) {
username := c.MustGet("username").(string)
appName := c.Param("app")
fileName := c.Param("filename")
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
}
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})
}
}