MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / UploadContainerFile

Method UploadContainerFile

agent/app/service/container.go:1262–1336  ·  view source on GitHub ↗
(req dto.ContainerFileReq, fileName string, fileSize int64, file io.Reader)

Source from the content-addressed store, hash-verified

1260}
1261
1262func (u *ContainerService) UploadContainerFile(req dto.ContainerFileReq, fileName string, fileSize int64, file io.Reader) error {
1263 if len(req.Path) == 0 {
1264 req.Path = "/"
1265 }
1266 safeName := path.Base(fileName)
1267 if safeName == "." || safeName == "/" || len(safeName) == 0 {
1268 return buserr.New("ErrInvalidChar")
1269 }
1270
1271 cli, err := docker.NewDockerClient()
1272 if err != nil {
1273 return err
1274 }
1275 defer cli.Close()
1276
1277 ctx := context.Background()
1278 stat, err := cli.ContainerStatPath(ctx, req.ContainerID, req.Path)
1279 if err != nil {
1280 if _, mkErr := runContainerCommand(cli, req.ContainerID, []string{"mkdir", "-p", "--", req.Path}); mkErr != nil {
1281 return mkErr
1282 }
1283 stat, err = cli.ContainerStatPath(ctx, req.ContainerID, req.Path)
1284 if err != nil {
1285 return err
1286 }
1287 }
1288 if !stat.Mode.IsDir() {
1289 return fmt.Errorf("path %s is not directory", req.Path)
1290 }
1291
1292 pipeReader, pipeWriter := io.Pipe()
1293 writeErr := make(chan error, 1)
1294 go func() {
1295 tw := tar.NewWriter(pipeWriter)
1296 header := &tar.Header{
1297 Name: safeName,
1298 Mode: 0644,
1299 Size: fileSize,
1300 ModTime: time.Now(),
1301 }
1302 if err := tw.WriteHeader(header); err != nil {
1303 _ = tw.Close()
1304 _ = pipeWriter.CloseWithError(err)
1305 writeErr <- err
1306 return
1307 }
1308 if _, err := io.Copy(tw, file); err != nil {
1309 _ = tw.Close()
1310 _ = pipeWriter.CloseWithError(err)
1311 writeErr <- err
1312 return
1313 }
1314 if err := tw.Close(); err != nil {
1315 _ = pipeWriter.CloseWithError(err)
1316 writeErr <- err
1317 return
1318 }
1319 _ = pipeWriter.Close()

Callers

nothing calls this directly

Calls 5

runContainerCommandFunction · 0.85
IsDirMethod · 0.80
WriteHeaderMethod · 0.80
CopyMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected