github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/pcloud/api/types.go (about)

     1  // Package api has type definitions for pcloud
     2  //
     3  // Converted from the API docs with help from https://mholt.github.io/json-to-go/
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  )
    10  
    11  const (
    12  	// Sun, 16 Mar 2014 17:26:04 +0000
    13  	timeFormat = `"` + time.RFC1123Z + `"`
    14  )
    15  
    16  // Time represents represents date and time information for the
    17  // pcloud API, by using RFC1123Z
    18  type Time time.Time
    19  
    20  // MarshalJSON turns a Time into JSON (in UTC)
    21  func (t *Time) MarshalJSON() (out []byte, err error) {
    22  	timeString := (*time.Time)(t).Format(timeFormat)
    23  	return []byte(timeString), nil
    24  }
    25  
    26  // UnmarshalJSON turns JSON into a Time
    27  func (t *Time) UnmarshalJSON(data []byte) error {
    28  	newT, err := time.Parse(timeFormat, string(data))
    29  	if err != nil {
    30  		return err
    31  	}
    32  	*t = Time(newT)
    33  	return nil
    34  }
    35  
    36  // Error is returned from pcloud when things go wrong
    37  //
    38  // If result is 0 then everything is OK
    39  type Error struct {
    40  	Result      int    `json:"result"`
    41  	ErrorString string `json:"error"`
    42  }
    43  
    44  // Error returns a string for the error and satisfies the error interface
    45  func (e *Error) Error() string {
    46  	return fmt.Sprintf("pcloud error: %s (%d)", e.ErrorString, e.Result)
    47  }
    48  
    49  // Update returns err directly if it was != nil, otherwise it returns
    50  // an Error or nil if no error was detected
    51  func (e *Error) Update(err error) error {
    52  	if err != nil {
    53  		return err
    54  	}
    55  	if e.Result == 0 {
    56  		return nil
    57  	}
    58  	return e
    59  }
    60  
    61  // Check Error satisfies the error interface
    62  var _ error = (*Error)(nil)
    63  
    64  // Item describes a folder or a file as returned by Get Folder Items and others
    65  type Item struct {
    66  	Path           string `json:"path"`
    67  	Name           string `json:"name"`
    68  	Created        Time   `json:"created"`
    69  	IsMine         bool   `json:"ismine"`
    70  	Thumb          bool   `json:"thumb"`
    71  	Modified       Time   `json:"modified"`
    72  	Comments       int    `json:"comments"`
    73  	ID             string `json:"id"`
    74  	IsShared       bool   `json:"isshared"`
    75  	IsDeleted      bool   `json:"isdeleted"`
    76  	Icon           string `json:"icon"`
    77  	IsFolder       bool   `json:"isfolder"`
    78  	ParentFolderID int64  `json:"parentfolderid"`
    79  	FolderID       int64  `json:"folderid,omitempty"`
    80  	Height         int    `json:"height,omitempty"`
    81  	FileID         int64  `json:"fileid,omitempty"`
    82  	Width          int    `json:"width,omitempty"`
    83  	Hash           uint64 `json:"hash,omitempty"`
    84  	Category       int    `json:"category,omitempty"`
    85  	Size           int64  `json:"size,omitempty"`
    86  	ContentType    string `json:"contenttype,omitempty"`
    87  	Contents       []Item `json:"contents"`
    88  }
    89  
    90  // ModTime returns the modification time of the item
    91  func (i *Item) ModTime() (t time.Time) {
    92  	t = time.Time(i.Modified)
    93  	if t.IsZero() {
    94  		t = time.Time(i.Created)
    95  	}
    96  	return t
    97  }
    98  
    99  // ItemResult is returned from the /listfolder, /createfolder, /deletefolder, /deletefile etc methods
   100  type ItemResult struct {
   101  	Error
   102  	Metadata Item `json:"metadata"`
   103  }
   104  
   105  // Hashes contains the supported hashes
   106  type Hashes struct {
   107  	SHA1 string `json:"sha1"`
   108  	MD5  string `json:"md5"`
   109  }
   110  
   111  // UploadFileResponse is the response from /uploadfile
   112  type UploadFileResponse struct {
   113  	Error
   114  	Items     []Item   `json:"metadata"`
   115  	Checksums []Hashes `json:"checksums"`
   116  	Fileids   []int64  `json:"fileids"`
   117  }
   118  
   119  // GetFileLinkResult is returned from /getfilelink
   120  type GetFileLinkResult struct {
   121  	Error
   122  	Dwltag  string   `json:"dwltag"`
   123  	Hash    uint64   `json:"hash"`
   124  	Size    int64    `json:"size"`
   125  	Expires Time     `json:"expires"`
   126  	Path    string   `json:"path"`
   127  	Hosts   []string `json:"hosts"`
   128  }
   129  
   130  // IsValid returns whether the link is valid and has not expired
   131  func (g *GetFileLinkResult) IsValid() bool {
   132  	if g == nil {
   133  		return false
   134  	}
   135  	if len(g.Hosts) == 0 {
   136  		return false
   137  	}
   138  	return time.Time(g.Expires).Sub(time.Now()) > 30*time.Second
   139  }
   140  
   141  // URL returns a URL from the Path and Hosts.  Check with IsValid
   142  // before calling.
   143  func (g *GetFileLinkResult) URL() string {
   144  	// FIXME rotate the hosts?
   145  	return "https://" + g.Hosts[0] + g.Path
   146  }
   147  
   148  // ChecksumFileResult is returned from /checksumfile
   149  type ChecksumFileResult struct {
   150  	Error
   151  	Hashes
   152  	Metadata Item `json:"metadata"`
   153  }
   154  
   155  // UserInfo is returned from /userinfo
   156  type UserInfo struct {
   157  	Error
   158  	Cryptosetup           bool   `json:"cryptosetup"`
   159  	Plan                  int    `json:"plan"`
   160  	CryptoSubscription    bool   `json:"cryptosubscription"`
   161  	PublicLinkQuota       int64  `json:"publiclinkquota"`
   162  	Email                 string `json:"email"`
   163  	UserID                int    `json:"userid"`
   164  	Quota                 int64  `json:"quota"`
   165  	TrashRevretentionDays int    `json:"trashrevretentiondays"`
   166  	Premium               bool   `json:"premium"`
   167  	PremiumLifetime       bool   `json:"premiumlifetime"`
   168  	EmailVerified         bool   `json:"emailverified"`
   169  	UsedQuota             int64  `json:"usedquota"`
   170  	Language              string `json:"language"`
   171  	Business              bool   `json:"business"`
   172  	CryptoLifetime        bool   `json:"cryptolifetime"`
   173  	Registered            string `json:"registered"`
   174  	Journey               struct {
   175  		Claimed bool `json:"claimed"`
   176  		Steps   struct {
   177  			VerifyMail    bool `json:"verifymail"`
   178  			UploadFile    bool `json:"uploadfile"`
   179  			AutoUpload    bool `json:"autoupload"`
   180  			DownloadApp   bool `json:"downloadapp"`
   181  			DownloadDrive bool `json:"downloaddrive"`
   182  		} `json:"steps"`
   183  	} `json:"journey"`
   184  }