github.com/Files-com/files-sdk-go/v3@v3.1.81/file/downloadstatus.go (about) 1 package file 2 3 import ( 4 "io/fs" 5 "sync" 6 "time" 7 8 files_sdk "github.com/Files-com/files-sdk-go/v3" 9 "github.com/Files-com/files-sdk-go/v3/file/status" 10 ) 11 12 type DownloadStatus struct { 13 fsFile fs.File 14 fs.FS 15 fs.FileInfo 16 file files_sdk.File 17 status status.Status 18 job *Job 19 DownloadedBytes int64 20 localPath string 21 remotePath string 22 Sync bool 23 endedAt time.Time 24 startedAt time.Time 25 Mutex *sync.RWMutex 26 PreserveTimes bool 27 error 28 lastError error 29 dryRun bool 30 status.Changes 31 } 32 33 var _ IFile = &DownloadStatus{} 34 35 func (d *DownloadStatus) EndedAt() time.Time { 36 return d.endedAt 37 } 38 39 func (d *DownloadStatus) StartedAt() time.Time { 40 return d.startedAt 41 } 42 43 func (d *DownloadStatus) Size() (size int64) { 44 if d.FileInfo != nil { 45 size = d.FileInfo.Size() 46 } 47 48 if size <= 0 { 49 size = d.File().Size 50 } 51 52 return 53 } 54 55 func (d *DownloadStatus) RecentError() error { 56 if d.error != nil { 57 return d.error 58 } 59 60 return d.lastError 61 } 62 63 func (d *DownloadStatus) SetStatus(s status.Status, err error) { 64 d.Mutex.Lock() 65 defer d.Mutex.Unlock() 66 var setError bool 67 d.status, setError = status.SetStatus(d.status, s, err) 68 if setError { 69 if d.error != nil { 70 d.lastError = d.error 71 } 72 d.error = err 73 } 74 75 if s.Is(status.Downloading) && d.startedAt.IsZero() { 76 d.startedAt = time.Now() 77 } 78 79 if s.Is(status.Retrying) { 80 d.DownloadedBytes = 0 81 } 82 83 if s.Is(status.Ended...) { 84 d.endedAt = time.Now() 85 } 86 87 d.Changes = append(d.Changes, status.Change{Status: d.status, Err: d.error, Time: time.Now()}) 88 } 89 90 func (d *DownloadStatus) StatusChanges() status.Changes { 91 d.Mutex.RLock() 92 defer d.Mutex.RUnlock() 93 94 return d.Changes 95 } 96 97 func (d *DownloadStatus) Id() string { 98 d.Mutex.RLock() 99 defer d.Mutex.RUnlock() 100 return d.job.Id + ":" + d.file.Path 101 } 102 103 func (d *DownloadStatus) incrementDownloadedBytes(b int64) { 104 d.Mutex.Lock() 105 d.DownloadedBytes += b 106 d.Mutex.Unlock() 107 } 108 109 func (d *DownloadStatus) IncrementTransferBytes(b int64) { 110 d.incrementDownloadedBytes(b) 111 } 112 113 func (d *DownloadStatus) TransferBytes() int64 { 114 d.Mutex.RLock() 115 defer d.Mutex.RUnlock() 116 return d.DownloadedBytes 117 } 118 119 func (d *DownloadStatus) File() files_sdk.File { 120 d.Mutex.RLock() 121 defer d.Mutex.RUnlock() 122 return d.file 123 } 124 125 func (d *DownloadStatus) LocalPath() string { 126 return d.localPath 127 } 128 129 func (d *DownloadStatus) RemotePath() string { 130 return d.remotePath 131 } 132 133 func (d *DownloadStatus) Status() status.Status { 134 d.Mutex.RLock() 135 defer d.Mutex.RUnlock() 136 return d.status 137 } 138 139 func (d *DownloadStatus) Err() error { 140 d.Mutex.RLock() 141 defer d.Mutex.RUnlock() 142 return d.error 143 } 144 145 func (d *DownloadStatus) Job() *Job { 146 return d.job 147 } 148 149 func (d *DownloadStatus) SetFinalSize(written int64) { 150 d.Mutex.Lock() 151 d.DownloadedBytes = written 152 d.file.Size = written 153 d.Mutex.Unlock() 154 }