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