@@ -8,14 +8,14 @@ import (
88 "crypto/sha256"
99 "errors"
1010 "fmt"
11- "io"
1211 "io/fs"
13- "net/http"
1412 "net/url"
1513 "os"
1614 "path"
1715 "sync"
1816
17+ "github.com/ethereum-optimism/optimism/op-service/httputil"
18+
1919 "github.com/ethereum-optimism/optimism/op-service/ioutil"
2020
2121 "github.com/ethereum-optimism/optimism/op-chain-ops/foundry"
@@ -24,16 +24,16 @@ import (
2424var ErrUnsupportedArtifactsScheme = errors .New ("unsupported artifacts URL scheme" )
2525
2626type Downloader interface {
27- Download (ctx context.Context , url string , progress DownloadProgressor , targetDir string ) (string , error )
27+ Download (ctx context.Context , url string , progress ioutil. Progressor , targetDir string ) (string , error )
2828}
2929
3030type Extractor interface {
3131 Extract (src string , dest string ) (string , error )
3232}
3333
34- func Download (ctx context.Context , loc * Locator , progressor DownloadProgressor , targetDir string ) (foundry.StatDirFs , error ) {
34+ func Download (ctx context.Context , loc * Locator , progressor ioutil. Progressor , targetDir string ) (foundry.StatDirFs , error ) {
3535 if progressor == nil {
36- progressor = NoopProgressor ()
36+ progressor = ioutil . NoopProgressor ()
3737 }
3838
3939 var err error
@@ -60,7 +60,7 @@ func Download(ctx context.Context, loc *Locator, progressor DownloadProgressor,
6060 return artifactsFS .(foundry.StatDirFs ), nil
6161}
6262
63- func downloadHTTP (ctx context.Context , u * url.URL , progressor DownloadProgressor , checker integrityChecker , targetDir string ) (fs.FS , error ) {
63+ func downloadHTTP (ctx context.Context , u * url.URL , progressor ioutil. Progressor , checker integrityChecker , targetDir string ) (fs.FS , error ) {
6464 cacher := & CachingDownloader {
6565 d : new (HTTPDownloader ),
6666 }
@@ -84,38 +84,20 @@ func downloadHTTP(ctx context.Context, u *url.URL, progressor DownloadProgressor
8484
8585type HTTPDownloader struct {}
8686
87- func (d * HTTPDownloader ) Download (ctx context.Context , url string , progress DownloadProgressor , targetDir string ) (string , error ) {
88- req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
89- if err != nil {
90- return "" , fmt .Errorf ("failed to create request: %w" , err )
91- }
92-
93- res , err := http .DefaultClient .Do (req )
94- if err != nil {
95- return "" , fmt .Errorf ("failed to download artifacts: %w" , err )
96- }
97- if res .StatusCode != http .StatusOK {
98- return "" , fmt .Errorf ("failed to download artifacts: invalid status code %s" , res .Status )
99- }
100- defer res .Body .Close ()
101-
87+ func (d * HTTPDownloader ) Download (ctx context.Context , url string , progress ioutil.Progressor , targetDir string ) (string , error ) {
10288 if err := os .MkdirAll (targetDir , 0755 ); err != nil {
10389 return "" , fmt .Errorf ("failed to ensure cache directory '%s': %w" , targetDir , err )
10490 }
10591 tmpFile , err := os .CreateTemp (targetDir , "op-deployer-artifacts-*" )
10692 if err != nil {
10793 return "" , fmt .Errorf ("failed to create temporary file: %w" , err )
10894 }
109-
110- pr := & progressReader {
111- r : res .Body ,
112- progress : progress ,
113- total : res .ContentLength ,
95+ downloader := & httputil.Downloader {
96+ Progressor : progress ,
11497 }
115- if _ , err := io . Copy ( tmpFile , pr ); err != nil {
116- return "" , fmt .Errorf ("failed to write to temporary file : %w" , err )
98+ if err := downloader . Download ( ctx , url , tmpFile ); err != nil {
99+ return "" , fmt .Errorf ("failed to download : %w" , err )
117100 }
118-
119101 return tmpFile .Name (), nil
120102}
121103
@@ -124,7 +106,7 @@ type CachingDownloader struct {
124106 mtx sync.Mutex
125107}
126108
127- func (d * CachingDownloader ) Download (ctx context.Context , url string , progress DownloadProgressor , targetDir string ) (string , error ) {
109+ func (d * CachingDownloader ) Download (ctx context.Context , url string , progress ioutil. Progressor , targetDir string ) (string , error ) {
128110 d .mtx .Lock ()
129111 defer d .mtx .Unlock ()
130112
0 commit comments