(req dto.ContainerFileReq)
| 1415 | } |
| 1416 | |
| 1417 | func (u *ContainerService) DownloadContainerFile(req dto.ContainerFileReq) (io.ReadCloser, string, string, error) { |
| 1418 | if len(req.Path) == 0 { |
| 1419 | req.Path = "/" |
| 1420 | } |
| 1421 | cli, err := docker.NewDockerClient() |
| 1422 | if err != nil { |
| 1423 | return nil, "", "", err |
| 1424 | } |
| 1425 | |
| 1426 | ctx := context.Background() |
| 1427 | stat, err := cli.ContainerStatPath(ctx, req.ContainerID, req.Path) |
| 1428 | if err != nil { |
| 1429 | _ = cli.Close() |
| 1430 | return nil, "", "", normalizeContainerFileError(err) |
| 1431 | } |
| 1432 | |
| 1433 | fileName := stat.Name |
| 1434 | if len(fileName) == 0 { |
| 1435 | fileName = "container-file" |
| 1436 | } |
| 1437 | if stat.Mode.IsDir() { |
| 1438 | if _, err := runContainerCommand(cli, req.ContainerID, []string{"tar", "--help"}); err != nil { |
| 1439 | _ = cli.Close() |
| 1440 | return nil, "", "", fmt.Errorf("tar command not found in container") |
| 1441 | } |
| 1442 | |
| 1443 | targetPath := path.Clean(req.Path) |
| 1444 | parentPath := path.Dir(targetPath) |
| 1445 | targetName := path.Base(targetPath) |
| 1446 | if parentPath == "." || parentPath == "" { |
| 1447 | parentPath = "/" |
| 1448 | } |
| 1449 | tarStream, err := runContainerCommandStream(cli, req.ContainerID, []string{ |
| 1450 | "tar", "-czf", "-", "-C", parentPath, "--", targetName, |
| 1451 | }) |
| 1452 | if err != nil { |
| 1453 | _ = cli.Close() |
| 1454 | return nil, "", "", err |
| 1455 | } |
| 1456 | if !strings.HasSuffix(fileName, ".tar.gz") { |
| 1457 | fileName += ".tar.gz" |
| 1458 | } |
| 1459 | return &closeHookReader{ |
| 1460 | ReadCloser: tarStream, |
| 1461 | onClose: cli.Close, |
| 1462 | }, fileName, "application/gzip", nil |
| 1463 | } |
| 1464 | |
| 1465 | fileStream, err := runContainerCommandStream(cli, req.ContainerID, []string{"cat", "--", req.Path}) |
| 1466 | if err != nil { |
| 1467 | _ = cli.Close() |
| 1468 | return nil, "", "", err |
| 1469 | } |
| 1470 | return &closeHookReader{ |
| 1471 | ReadCloser: fileStream, |
| 1472 | onClose: cli.Close, |
| 1473 | }, fileName, "application/octet-stream", nil |
| 1474 | } |
nothing calls this directly
no test coverage detected