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