github.com/artpar/rclone@v1.67.3/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 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  	SHA256 string `json:"sha256"`
   110  }
   111  
   112  // UploadFileResponse is the response from /uploadfile
   113  type UploadFileResponse struct {
   114  	Error
   115  	Items     []Item   `json:"metadata"`
   116  	Checksums []Hashes `json:"checksums"`
   117  	Fileids   []int64  `json:"fileids"`
   118  }
   119  
   120  // GetFileLinkResult is returned from /getfilelink
   121  type GetFileLinkResult struct {
   122  	Error
   123  	Dwltag  string   `json:"dwltag"`
   124  	Hash    uint64   `json:"hash"`
   125  	Size    int64    `json:"size"`
   126  	Expires Time     `json:"expires"`
   127  	Path    string   `json:"path"`
   128  	Hosts   []string `json:"hosts"`
   129  }
   130  
   131  // IsValid returns whether the link is valid and has not expired
   132  func (g *GetFileLinkResult) IsValid() bool {
   133  	if g == nil {
   134  		return false
   135  	}
   136  	if len(g.Hosts) == 0 {
   137  		return false
   138  	}
   139  	return time.Until(time.Time(g.Expires)) > 30*time.Second
   140  }
   141  
   142  // URL returns a URL from the Path and Hosts.  Check with IsValid
   143  // before calling.
   144  func (g *GetFileLinkResult) URL() string {
   145  	// FIXME rotate the hosts?
   146  	return "https://" + g.Hosts[0] + g.Path
   147  }
   148  
   149  // ChecksumFileResult is returned from /checksumfile
   150  type ChecksumFileResult struct {
   151  	Error
   152  	Hashes
   153  	Metadata Item `json:"metadata"`
   154  }
   155  
   156  // PubLinkResult is returned from /getfilepublink and /getfolderpublink
   157  type PubLinkResult struct {
   158  	Error
   159  	LinkID   int    `json:"linkid"`
   160  	Link     string `json:"link"`
   161  	LinkCode string `json:"code"`
   162  }
   163  
   164  // UserInfo is returned from /userinfo
   165  type UserInfo struct {
   166  	Error
   167  	Cryptosetup           bool   `json:"cryptosetup"`
   168  	Plan                  int    `json:"plan"`
   169  	CryptoSubscription    bool   `json:"cryptosubscription"`
   170  	PublicLinkQuota       int64  `json:"publiclinkquota"`
   171  	Email                 string `json:"email"`
   172  	UserID                int    `json:"userid"`
   173  	Quota                 int64  `json:"quota"`
   174  	TrashRevretentionDays int    `json:"trashrevretentiondays"`
   175  	Premium               bool   `json:"premium"`
   176  	PremiumLifetime       bool   `json:"premiumlifetime"`
   177  	EmailVerified         bool   `json:"emailverified"`
   178  	UsedQuota             int64  `json:"usedquota"`
   179  	Language              string `json:"language"`
   180  	Business              bool   `json:"business"`
   181  	CryptoLifetime        bool   `json:"cryptolifetime"`
   182  	Registered            string `json:"registered"`
   183  	Journey               struct {
   184  		Claimed bool `json:"claimed"`
   185  		Steps   struct {
   186  			VerifyMail    bool `json:"verifymail"`
   187  			UploadFile    bool `json:"uploadfile"`
   188  			AutoUpload    bool `json:"autoupload"`
   189  			DownloadApp   bool `json:"downloadapp"`
   190  			DownloadDrive bool `json:"downloaddrive"`
   191  		} `json:"steps"`
   192  	} `json:"journey"`
   193  }