github.com/Files-com/files-sdk-go/v2@v2.1.2/file.go (about)

     1  package files_sdk
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	lib "github.com/Files-com/files-sdk-go/v2/lib"
     8  )
     9  
    10  type File struct {
    11  	Path             string     `json:"path,omitempty" path:"path,omitempty" url:"path,omitempty"`
    12  	DisplayName      string     `json:"display_name,omitempty" path:"display_name,omitempty" url:"display_name,omitempty"`
    13  	Type             string     `json:"type,omitempty" path:"type,omitempty" url:"type,omitempty"`
    14  	Size             int64      `json:"size,omitempty" path:"size,omitempty" url:"size,omitempty"`
    15  	CreatedAt        *time.Time `json:"created_at,omitempty" path:"created_at,omitempty" url:"created_at,omitempty"`
    16  	Mtime            *time.Time `json:"mtime,omitempty" path:"mtime,omitempty" url:"mtime,omitempty"`
    17  	ProvidedMtime    *time.Time `json:"provided_mtime,omitempty" path:"provided_mtime,omitempty" url:"provided_mtime,omitempty"`
    18  	Crc32            string     `json:"crc32,omitempty" path:"crc32,omitempty" url:"crc32,omitempty"`
    19  	Md5              string     `json:"md5,omitempty" path:"md5,omitempty" url:"md5,omitempty"`
    20  	MimeType         string     `json:"mime_type,omitempty" path:"mime_type,omitempty" url:"mime_type,omitempty"`
    21  	Region           string     `json:"region,omitempty" path:"region,omitempty" url:"region,omitempty"`
    22  	Permissions      string     `json:"permissions,omitempty" path:"permissions,omitempty" url:"permissions,omitempty"`
    23  	SubfoldersLocked *bool      `json:"subfolders_locked?,omitempty" path:"subfolders_locked?,omitempty" url:"subfolders_locked?,omitempty"`
    24  	IsLocked         *bool      `json:"is_locked,omitempty" path:"is_locked,omitempty" url:"is_locked,omitempty"`
    25  	DownloadUri      string     `json:"download_uri,omitempty" path:"download_uri,omitempty" url:"download_uri,omitempty"`
    26  	PriorityColor    string     `json:"priority_color,omitempty" path:"priority_color,omitempty" url:"priority_color,omitempty"`
    27  	PreviewId        int64      `json:"preview_id,omitempty" path:"preview_id,omitempty" url:"preview_id,omitempty"`
    28  	Preview          Preview    `json:"preview,omitempty" path:"preview,omitempty" url:"preview,omitempty"`
    29  	Action           string     `json:"action,omitempty" path:"action,omitempty" url:"action,omitempty"`
    30  	Length           int64      `json:"length,omitempty" path:"length,omitempty" url:"length,omitempty"`
    31  	MkdirParents     *bool      `json:"mkdir_parents,omitempty" path:"mkdir_parents,omitempty" url:"mkdir_parents,omitempty"`
    32  	Part             int64      `json:"part,omitempty" path:"part,omitempty" url:"part,omitempty"`
    33  	Parts            int64      `json:"parts,omitempty" path:"parts,omitempty" url:"parts,omitempty"`
    34  	Ref              string     `json:"ref,omitempty" path:"ref,omitempty" url:"ref,omitempty"`
    35  	Restart          int64      `json:"restart,omitempty" path:"restart,omitempty" url:"restart,omitempty"`
    36  	Structure        string     `json:"structure,omitempty" path:"structure,omitempty" url:"structure,omitempty"`
    37  	WithRename       *bool      `json:"with_rename,omitempty" path:"with_rename,omitempty" url:"with_rename,omitempty"`
    38  }
    39  
    40  func (f File) Identifier() interface{} {
    41  	return f.Path
    42  }
    43  
    44  type FileCollection []File
    45  
    46  type EtagsParam struct {
    47  	Etag string `url:"etag,omitempty" json:"etag,omitempty" path:"etag"`
    48  	Part string `url:"part,omitempty" json:"part,omitempty" path:"part"`
    49  }
    50  
    51  // Download file
    52  type FileDownloadParams struct {
    53  	Path              string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    54  	Action            string `url:"action,omitempty" required:"false" json:"action,omitempty" path:"action"`
    55  	PreviewSize       string `url:"preview_size,omitempty" required:"false" json:"preview_size,omitempty" path:"preview_size"`
    56  	WithPreviews      *bool  `url:"with_previews,omitempty" required:"false" json:"with_previews,omitempty" path:"with_previews"`
    57  	WithPriorityColor *bool  `url:"with_priority_color,omitempty" required:"false" json:"with_priority_color,omitempty" path:"with_priority_color"`
    58  	File              File   `url:"-,omitempty" required:"false" json:"-,omitempty"`
    59  }
    60  
    61  type FileCreateParams struct {
    62  	Path          string       `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    63  	Action        string       `url:"action,omitempty" required:"false" json:"action,omitempty" path:"action"`
    64  	EtagsParam    []EtagsParam `url:"etags,omitempty" required:"false" json:"etags,omitempty" path:"etags"`
    65  	Length        int64        `url:"length,omitempty" required:"false" json:"length,omitempty" path:"length"`
    66  	MkdirParents  *bool        `url:"mkdir_parents,omitempty" required:"false" json:"mkdir_parents,omitempty" path:"mkdir_parents"`
    67  	Part          int64        `url:"part,omitempty" required:"false" json:"part,omitempty" path:"part"`
    68  	Parts         int64        `url:"parts,omitempty" required:"false" json:"parts,omitempty" path:"parts"`
    69  	ProvidedMtime *time.Time   `url:"provided_mtime,omitempty" required:"false" json:"provided_mtime,omitempty" path:"provided_mtime"`
    70  	Ref           string       `url:"ref,omitempty" required:"false" json:"ref,omitempty" path:"ref"`
    71  	Restart       int64        `url:"restart,omitempty" required:"false" json:"restart,omitempty" path:"restart"`
    72  	Size          int64        `url:"size,omitempty" required:"false" json:"size,omitempty" path:"size"`
    73  	Structure     string       `url:"structure,omitempty" required:"false" json:"structure,omitempty" path:"structure"`
    74  	WithRename    *bool        `url:"with_rename,omitempty" required:"false" json:"with_rename,omitempty" path:"with_rename"`
    75  }
    76  
    77  type FileUpdateParams struct {
    78  	Path          string     `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    79  	ProvidedMtime *time.Time `url:"provided_mtime,omitempty" required:"false" json:"provided_mtime,omitempty" path:"provided_mtime"`
    80  	PriorityColor string     `url:"priority_color,omitempty" required:"false" json:"priority_color,omitempty" path:"priority_color"`
    81  }
    82  
    83  type FileDeleteParams struct {
    84  	Path      string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    85  	Recursive *bool  `url:"recursive,omitempty" required:"false" json:"recursive,omitempty" path:"recursive"`
    86  }
    87  
    88  type FileFindParams struct {
    89  	Path              string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    90  	PreviewSize       string `url:"preview_size,omitempty" required:"false" json:"preview_size,omitempty" path:"preview_size"`
    91  	WithPreviews      *bool  `url:"with_previews,omitempty" required:"false" json:"with_previews,omitempty" path:"with_previews"`
    92  	WithPriorityColor *bool  `url:"with_priority_color,omitempty" required:"false" json:"with_priority_color,omitempty" path:"with_priority_color"`
    93  }
    94  
    95  // Copy file/folder
    96  type FileCopyParams struct {
    97  	Path        string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    98  	Destination string `url:"destination,omitempty" required:"true" json:"destination,omitempty" path:"destination"`
    99  	Structure   *bool  `url:"structure,omitempty" required:"false" json:"structure,omitempty" path:"structure"`
   100  }
   101  
   102  // Move file/folder
   103  type FileMoveParams struct {
   104  	Path        string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   105  	Destination string `url:"destination,omitempty" required:"true" json:"destination,omitempty" path:"destination"`
   106  }
   107  
   108  // Begin file upload
   109  type FileBeginUploadParams struct {
   110  	Path         string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   111  	MkdirParents *bool  `url:"mkdir_parents,omitempty" required:"false" json:"mkdir_parents,omitempty" path:"mkdir_parents"`
   112  	Part         int64  `url:"part,omitempty" required:"false" json:"part,omitempty" path:"part"`
   113  	Parts        int64  `url:"parts,omitempty" required:"false" json:"parts,omitempty" path:"parts"`
   114  	Ref          string `url:"ref,omitempty" required:"false" json:"ref,omitempty" path:"ref"`
   115  	Restart      int64  `url:"restart,omitempty" required:"false" json:"restart,omitempty" path:"restart"`
   116  	Size         int64  `url:"size,omitempty" required:"false" json:"size,omitempty" path:"size"`
   117  	WithRename   *bool  `url:"with_rename,omitempty" required:"false" json:"with_rename,omitempty" path:"with_rename"`
   118  }
   119  
   120  func (f File) ToFolder() (Folder, error) {
   121  	bodyBytes, err := json.Marshal(f)
   122  	if err != nil {
   123  		return Folder{}, err
   124  	}
   125  	folder := Folder{}
   126  	folder.UnmarshalJSON(bodyBytes)
   127  	return folder, nil
   128  }
   129  
   130  func (f File) String() string {
   131  	return f.Path
   132  }
   133  
   134  func (f File) Iterable() bool {
   135  	return f.IsDir()
   136  }
   137  
   138  func (f File) IsDir() bool {
   139  	return f.Type == "directory"
   140  }
   141  
   142  func (f *File) UnmarshalJSON(data []byte) error {
   143  	type file File
   144  	var v file
   145  	if err := json.Unmarshal(data, &v); err != nil {
   146  		return lib.ErrorWithOriginalResponse{}.ProcessError(data, err, map[string]interface{}{})
   147  	}
   148  
   149  	*f = File(v)
   150  	return nil
   151  }
   152  
   153  func (f *FileCollection) UnmarshalJSON(data []byte) error {
   154  	type files FileCollection
   155  	var v files
   156  	if err := json.Unmarshal(data, &v); err != nil {
   157  		return lib.ErrorWithOriginalResponse{}.ProcessError(data, err, []map[string]interface{}{})
   158  	}
   159  
   160  	*f = FileCollection(v)
   161  	return nil
   162  }
   163  
   164  func (f *FileCollection) ToSlice() *[]interface{} {
   165  	ret := make([]interface{}, len(*f))
   166  	for i, v := range *f {
   167  		ret[i] = v
   168  	}
   169  
   170  	return &ret
   171  }