fixed:区分文件和目录,目录tar打包下载,文件直接下载(应对部分镜像tar命令异常导致下载不了),容器明细页面也可以打开文件管理器 (#241)

This commit is contained in:
awol2005ex
2024-07-15 13:49:35 +08:00
committed by GitHub
parent 312946787e
commit 7dba154e7b
5 changed files with 118 additions and 4 deletions
+28 -1
View File
@@ -152,6 +152,32 @@ func (h *Handler) RmFolder() iris.Handler {
}
}
func (h *Handler) DownloadFolder() iris.Handler {
return func(ctx *context.Context) {
var req fileModel.Request
req.Path = ctx.URLParam("path")
req.Namespace = ctx.URLParam("namespace")
req.Cluster = ctx.URLParam("cluster")
req.PodName = ctx.URLParam("podName")
req.ContainerName = ctx.URLParam("containerName")
file, err := h.fileService.DownloadFolder(req)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
filename := path.Base(file)
err = ctx.SendFile(file, filename)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
os.RemoveAll(file)
}
}
func (h *Handler) DownloadFile() iris.Handler {
return func(ctx *context.Context) {
@@ -257,5 +283,6 @@ func Install(parent iris.Party) {
sp.Post("/files/rename", handler.ReNameFile())
sp.Post("/files/upload", handler.UploadFile())
sp.Post("/files/update", handler.UpdateFile())
sp.Get("/files/download", handler.DownloadFile())
sp.Get("/files/download/folder", handler.DownloadFolder())
sp.Get("/files/download/file", handler.DownloadFile())
}
+23 -1
View File
@@ -20,6 +20,7 @@ import (
type Service interface {
ListFiles(request file.Request) ([]podtool.File, error)
DownloadFile(request file.Request) (string, error)
DownloadFolder(request file.Request) (string, error)
UploadFile(request file.Request) error
ExecNewCommand(request file.Request) ([]byte, error)
EditFile(request file.Request) error
@@ -90,6 +91,27 @@ func (f service) EditFile(request file.Request) error {
func (f service) DownloadFile(request file.Request) (string, error) {
var fileP string
pt, err := f.GetPodTool(request)
if err != nil {
return fileP, err
}
fileNameWithSuffix := path.Base(request.Path)
fileP = filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().UnixNano()))
err = os.MkdirAll(fileP, os.ModePerm)
if err != nil {
return "", err
}
fileP = filepath.Join(fileP, fileNameWithSuffix)
err = pt.CopyFileFromPod(request.Path, fileP)
if err != nil {
return "", err
}
return fileP, nil
}
func (f service) DownloadFolder(request file.Request) (string, error) {
var fileP string
pt, err := f.GetPodTool(request)
if err != nil {
@@ -104,7 +126,7 @@ func (f service) DownloadFile(request file.Request) (string, error) {
return "", err
}
fileP = filepath.Join(fileP, fileName+".tar")
err = pt.CopyFromPod(request.Path, fileP)
err = pt.CopyFolderFromPod(request.Path, fileP)
if err != nil {
return "", err
}
+47 -1
View File
@@ -5,8 +5,54 @@ import (
"io"
"os"
)
func (p *PodTool) CopyFileFromPod(filePath string, destPath string) error {
reader, outStream := io.Pipe()
func (p *PodTool) CopyFromPod(filePath string, destPath string) error {
p.ExecConfig = ExecConfig{
Command: []string{"cat", filePath},
Stdin: os.Stdin,
Stdout: outStream,
Stderr: os.Stderr,
NoPreserve: true,
}
err := p.Exec(Download)
if err != nil {
return err
}
file, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
r := bufio.NewReader(reader)
w := bufio.NewWriter(file)
size := 4 * 1024
buf := make([]byte, 4*1024)
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
_, err = w.Write(buf[:n])
if err != nil {
return err
}
if n < size {
break
}
}
err = w.Flush()
if err != nil {
return err
}
return err
}
func (p *PodTool) CopyFolderFromPod(filePath string, destPath string) error {
reader, outStream := io.Pipe()
p.ExecConfig = ExecConfig{
@@ -420,7 +420,11 @@ export default {
return
}
const url = this.getUrl(row.name)
window.open("/kubepi/api/v1/pod/files/download" + url, "_blank")
//区分文件和目录,目录tar打包下载,文件直接下载(应对部分镜像tar命令异常导致下载不了)
if(row["mode"].startsWith("d"))
window.open("/kubepi/api/v1/pod/files/download/folder" + url, "_blank")
else
window.open("/kubepi/api/v1/pod/files/download/file" + url, "_blank")
},
getUrl (name) {
this.fileRequest.path = this.getPath(name)
@@ -127,6 +127,16 @@ export default {
return !checkPermissions({ scope: "namespace", apiGroup: "", resource: "pods/exec", verb: "create" })
},
},
{
label: this.$t("business.pod.pod_file"),
icon: "el-icon-files",
click: (row) => {
this.openPodFiles(row)
},
disabled: () => {
return !checkPermissions({ scope: "namespace", apiGroup: "", resource: "pods/exec", verb: "create" })
},
},
],
form: {},
namespace: "",
@@ -151,6 +161,11 @@ export default {
let routeUrl = this.$router.resolve({ path: "/terminal", query: { cluster: this.cluster, namespace: this.namespace, pod: this.name, container: row.container.name, type: "log" } })
window.open(routeUrl.href, "_blank")
},
openPodFiles(row) {
this.$router.push({ name: "PodFile", params: { namespace: this.yamlInfo.metadata.namespace, name: this.yamlInfo.metadata.name },query:{
container: row.container.name
}})
},
},
}
</script>