github.com/Files-com/files-sdk-go/v3@v3.1.81/file/uploadstatus.go (about)

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