(req dto.ContainerFileReq)
| 1336 | } |
| 1337 | |
| 1338 | func (u *ContainerService) GetContainerFileContent(req dto.ContainerFileReq) (*dto.ContainerFileContent, error) { |
| 1339 | if len(req.Path) == 0 { |
| 1340 | return nil, buserr.New("ErrInvalidChar") |
| 1341 | } |
| 1342 | cli, err := docker.NewDockerClient() |
| 1343 | if err != nil { |
| 1344 | return nil, err |
| 1345 | } |
| 1346 | defer cli.Close() |
| 1347 | |
| 1348 | stat, err := cli.ContainerStatPath(context.Background(), req.ContainerID, req.Path) |
| 1349 | if err != nil { |
| 1350 | return nil, normalizeContainerFileError(err) |
| 1351 | } |
| 1352 | if stat.Mode.IsDir() { |
| 1353 | return nil, fmt.Errorf("path %s is directory", req.Path) |
| 1354 | } |
| 1355 | |
| 1356 | content := &dto.ContainerFileContent{Size: stat.Size} |
| 1357 | headBytes, err := runContainerCommandRaw(cli, req.ContainerID, []string{"head", "-c", "4096", "--", req.Path}) |
| 1358 | if err != nil { |
| 1359 | return nil, err |
| 1360 | } |
| 1361 | if bytes.IndexByte(headBytes, 0) >= 0 { |
| 1362 | content.IsBinary = true |
| 1363 | return content, nil |
| 1364 | } |
| 1365 | |
| 1366 | const inlinePreviewMax = 512 * 1024 |
| 1367 | if stat.Size <= inlinePreviewMax { |
| 1368 | raw, err := runContainerCommandRaw(cli, req.ContainerID, []string{"cat", "--", req.Path}) |
| 1369 | if err != nil { |
| 1370 | return nil, err |
| 1371 | } |
| 1372 | content.Content = string(raw) |
| 1373 | return content, nil |
| 1374 | } |
| 1375 | |
| 1376 | raw, err := runContainerCommandRaw(cli, req.ContainerID, []string{"tail", "-n", "300", "--", req.Path}) |
| 1377 | if err != nil { |
| 1378 | return nil, err |
| 1379 | } |
| 1380 | content.Content = string(raw) |
| 1381 | content.Truncated = true |
| 1382 | return content, nil |
| 1383 | } |
| 1384 | |
| 1385 | func (u *ContainerService) GetContainerFileSize(req dto.ContainerFileReq) (int64, error) { |
| 1386 | if len(req.Path) == 0 { |
nothing calls this directly
no test coverage detected