github.com/artpar/rclone@v1.67.3/backend/hidrive/api/types.go (about)

     1  // Package api has type definitions and code related to API-calls for the HiDrive-API.
     2  package api
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/url"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  // Time represents date and time information for the API.
    13  type Time time.Time
    14  
    15  // MarshalJSON turns Time into JSON (in Unix-time/UTC).
    16  func (t *Time) MarshalJSON() ([]byte, error) {
    17  	secs := time.Time(*t).Unix()
    18  	return []byte(strconv.FormatInt(secs, 10)), nil
    19  }
    20  
    21  // UnmarshalJSON turns JSON into Time.
    22  func (t *Time) UnmarshalJSON(data []byte) error {
    23  	secs, err := strconv.ParseInt(string(data), 10, 64)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	*t = Time(time.Unix(secs, 0))
    28  	return nil
    29  }
    30  
    31  // Error is returned from the API when things go wrong.
    32  type Error struct {
    33  	Code        json.Number `json:"code"`
    34  	ContextInfo json.RawMessage
    35  	Message     string `json:"msg"`
    36  }
    37  
    38  // Error returns a string for the error and satisfies the error interface.
    39  func (e *Error) Error() string {
    40  	out := fmt.Sprintf("Error %q", e.Code.String())
    41  	if e.Message != "" {
    42  		out += ": " + e.Message
    43  	}
    44  	if e.ContextInfo != nil {
    45  		out += fmt.Sprintf(" (%+v)", e.ContextInfo)
    46  	}
    47  	return out
    48  }
    49  
    50  // Check Error satisfies the error interface.
    51  var _ error = (*Error)(nil)
    52  
    53  // possible types for HiDriveObject
    54  const (
    55  	HiDriveObjectTypeDirectory = "dir"
    56  	HiDriveObjectTypeFile      = "file"
    57  	HiDriveObjectTypeSymlink   = "symlink"
    58  )
    59  
    60  // HiDriveObject describes a folder, a symlink or a file.
    61  // Depending on the type and content, not all fields are present.
    62  type HiDriveObject struct {
    63  	Type         string `json:"type"`
    64  	ID           string `json:"id"`
    65  	ParentID     string `json:"parent_id"`
    66  	Name         string `json:"name"`
    67  	Path         string `json:"path"`
    68  	Size         int64  `json:"size"`
    69  	MemberCount  int64  `json:"nmembers"`
    70  	ModifiedAt   Time   `json:"mtime"`
    71  	ChangedAt    Time   `json:"ctime"`
    72  	MetaHash     string `json:"mhash"`
    73  	MetaOnlyHash string `json:"mohash"`
    74  	NameHash     string `json:"nhash"`
    75  	ContentHash  string `json:"chash"`
    76  	IsTeamfolder bool   `json:"teamfolder"`
    77  	Readable     bool   `json:"readable"`
    78  	Writable     bool   `json:"writable"`
    79  	Shareable    bool   `json:"shareable"`
    80  	MIMEType     string `json:"mime_type"`
    81  }
    82  
    83  // ModTime returns the modification time of the HiDriveObject.
    84  func (i *HiDriveObject) ModTime() time.Time {
    85  	t := time.Time(i.ModifiedAt)
    86  	if t.IsZero() {
    87  		t = time.Time(i.ChangedAt)
    88  	}
    89  	return t
    90  }
    91  
    92  // UnmarshalJSON turns JSON into HiDriveObject and
    93  // introduces specific default-values where necessary.
    94  func (i *HiDriveObject) UnmarshalJSON(data []byte) error {
    95  	type objectAlias HiDriveObject
    96  	defaultObject := objectAlias{
    97  		Size:        -1,
    98  		MemberCount: -1,
    99  	}
   100  
   101  	err := json.Unmarshal(data, &defaultObject)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	name, err := url.PathUnescape(defaultObject.Name)
   106  	if err == nil {
   107  		defaultObject.Name = name
   108  	}
   109  
   110  	*i = HiDriveObject(defaultObject)
   111  	return nil
   112  }
   113  
   114  // DirectoryContent describes the content of a directory.
   115  type DirectoryContent struct {
   116  	TotalCount int64           `json:"nmembers"`
   117  	Entries    []HiDriveObject `json:"members"`
   118  }
   119  
   120  // UnmarshalJSON turns JSON into DirectoryContent and
   121  // introduces specific default-values where necessary.
   122  func (d *DirectoryContent) UnmarshalJSON(data []byte) error {
   123  	type directoryContentAlias DirectoryContent
   124  	defaultDirectoryContent := directoryContentAlias{
   125  		TotalCount: -1,
   126  	}
   127  
   128  	err := json.Unmarshal(data, &defaultDirectoryContent)
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	*d = DirectoryContent(defaultDirectoryContent)
   134  	return nil
   135  }