@@ -10,6 +10,7 @@ import (
1010 "path/filepath"
1111 "time"
1212
13+ "github.com/cenkalti/log"
1314 "github.com/putdotio/putio-sync/v2/internal/inode"
1415 "github.com/putdotio/putio-sync/v2/internal/progress"
1516)
@@ -85,42 +86,48 @@ func (d *downloadJob) Run(ctx context.Context) error {
8586 }
8687 }
8788
88- ctx , cancel := context .WithCancel (ctx )
89- defer cancel ()
90- rc , err := d .openRemote (ctx , d .state .Offset )
91- if err != nil {
92- return err
93- }
94- defer rc .Close ()
89+ remaining := d .state .Size - d .state .Offset
90+ if remaining > 0 {
91+ ctx , cancel := context .WithCancel (ctx )
92+ defer cancel ()
93+ rc , err := d .openRemote (ctx , d .state .Offset )
94+ if err != nil {
95+ return err
96+ }
97+ defer rc .Close ()
9598
96- // Stop download if download speed is too slow.
97- // Timer for cancelling the context will be reset after each successful read from stream.
98- trw := & timerResetWriter {timer : time .AfterFunc (defaultTimeout , cancel )}
99- tr := io .TeeReader (rc , trw )
99+ // Stop download if download speed is too slow.
100+ // Timer for cancelling the context will be reset after each successful read from stream.
101+ trw := & timerResetWriter {timer : time .AfterFunc (defaultTimeout , cancel )}
102+ tr := io .TeeReader (rc , trw )
100103
101- pr := progress .New (tr , d .state .Offset , d .state .Size , d .String ())
102- pr .Start ()
103- remaining := d .state .Size - d .state .Offset
104- n , copyErr := io .CopyN (wc , pr , remaining )
105- pr .Stop ()
104+ pr := progress .New (tr , d .state .Offset , d .state .Size , d .String ())
105+ pr .Start ()
106+ n , copyErr := io .CopyN (wc , pr , remaining )
107+ pr .Stop ()
106108
107- err = wc .Close ()
108- if err != nil {
109- return err
110- }
109+ err = wc .Close ()
110+ if err != nil {
111+ return err
112+ }
111113
112- d .state .Offset += n
113- err = d .state .Write ()
114- if err != nil {
115- return err
116- }
114+ d .state .Offset += n
115+ err = d .state .Write ()
116+ if err != nil {
117+ return err
118+ }
117119
118- if copyErr != nil {
119- return copyErr
120+ if copyErr != nil {
121+ return copyErr
122+ }
120123 }
121124
122125 oldPath := filepath .Join (tempDirPath , d .state .DownloadTempName )
123126 newPath := filepath .Join (localPath , filepath .FromSlash (d .state .relpath ))
127+ err := os .MkdirAll (filepath .Dir (newPath ), 0777 )
128+ if err != nil {
129+ return err
130+ }
124131 err = os .Rename (oldPath , newPath )
125132 if err != nil {
126133 return err
@@ -148,6 +155,7 @@ func (d *downloadJob) openRemote(baseCtx context.Context, offset int64) (rc io.R
148155 return
149156 }
150157 req .Header .Set ("range" , fmt .Sprintf ("bytes=%d-" , offset ))
158+ log .Debugln ("range" , req .Header .Get ("range" ))
151159 resp , err := httpClient .Do (req )
152160 if err != nil {
153161 return
0 commit comments