(ctx context.Context, elem TaskElement)
| 54 | } |
| 55 | |
| 56 | func (t *Task) processElement(ctx context.Context, elem TaskElement) error { |
| 57 | logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", elem.File.Name())) |
| 58 | if elem.stream { |
| 59 | pr, pw := io.Pipe() |
| 60 | defer pr.Close() |
| 61 | errg, uploadCtx := errgroup.WithContext(ctx) |
| 62 | errg.Go(func() error { |
| 63 | return elem.Storage.Save(uploadCtx, pr, elem.Path) |
| 64 | }) |
| 65 | wr := ioutil.NewProgressWriter(pw, func(n int) { |
| 66 | downloaded := t.downloaded.Add(int64(n)) |
| 67 | t.Progress.OnProgress(ctx, t) |
| 68 | taskevent.Emit(ctx, taskevent.Event{ |
| 69 | TaskID: t.ID, |
| 70 | Phase: taskevent.PhaseProgress, |
| 71 | TotalBytes: t.totalSize, |
| 72 | DownloadedBytes: downloaded, |
| 73 | }) |
| 74 | }) |
| 75 | errg.Go(func() error { |
| 76 | defer pw.Close() |
| 77 | logger.Info("Starting file download in stream mode") |
| 78 | _, err := tdler.NewDownloader(elem.File).Stream(uploadCtx, wr) |
| 79 | if err != nil { |
| 80 | logger.Errorf("Failed to download file: %v", err) |
| 81 | pw.CloseWithError(err) |
| 82 | } |
| 83 | return err |
| 84 | }) |
| 85 | if err := errg.Wait(); err != nil { |
| 86 | return fmt.Errorf("failed to download file in stream mode: %w", err) |
| 87 | } |
| 88 | logger.Info("File downloaded successfully in stream mode") |
| 89 | return nil |
| 90 | } |
| 91 | logger.Info("Starting file download") |
| 92 | localFile, err := fsutil.CreateFile(elem.localPath) |
| 93 | if err != nil { |
| 94 | return fmt.Errorf("failed to create local file: %w", err) |
| 95 | } |
| 96 | defer func() { |
| 97 | if err := localFile.CloseAndRemove(); err != nil { |
| 98 | logger.Errorf("Failed to close local file: %v", err) |
| 99 | } |
| 100 | }() |
| 101 | wrAt := ioutil.NewProgressWriterAt(localFile, func(n int) { |
| 102 | downloaded := t.downloaded.Add(int64(n)) |
| 103 | t.Progress.OnProgress(ctx, t) |
| 104 | taskevent.Emit(ctx, taskevent.Event{ |
| 105 | TaskID: t.ID, |
| 106 | Phase: taskevent.PhaseProgress, |
| 107 | TotalBytes: t.totalSize, |
| 108 | DownloadedBytes: downloaded, |
| 109 | }) |
| 110 | }) |
| 111 | _, err = tdler.NewDownloader(elem.File).Parallel(ctx, wrAt) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("failed to download file: %w", err) |
no test coverage detected