github.com/Files-com/files-sdk-go/v3@v3.1.81/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/v3/lib"
     8  )
     9  
    10  type File struct {
    11  	Path                               string                 `json:"path,omitempty" path:"path,omitempty" url:"path,omitempty"`
    12  	CreatedById                        int64                  `json:"created_by_id,omitempty" path:"created_by_id,omitempty" url:"created_by_id,omitempty"`
    13  	CreatedByApiKeyId                  int64                  `json:"created_by_api_key_id,omitempty" path:"created_by_api_key_id,omitempty" url:"created_by_api_key_id,omitempty"`
    14  	CreatedByAs2IncomingMessageId      int64                  `json:"created_by_as2_incoming_message_id,omitempty" path:"created_by_as2_incoming_message_id,omitempty" url:"created_by_as2_incoming_message_id,omitempty"`
    15  	CreatedByAutomationId              int64                  `json:"created_by_automation_id,omitempty" path:"created_by_automation_id,omitempty" url:"created_by_automation_id,omitempty"`
    16  	CreatedByBundleRegistrationId      int64                  `json:"created_by_bundle_registration_id,omitempty" path:"created_by_bundle_registration_id,omitempty" url:"created_by_bundle_registration_id,omitempty"`
    17  	CreatedByInboxId                   int64                  `json:"created_by_inbox_id,omitempty" path:"created_by_inbox_id,omitempty" url:"created_by_inbox_id,omitempty"`
    18  	CreatedByRemoteServerId            int64                  `json:"created_by_remote_server_id,omitempty" path:"created_by_remote_server_id,omitempty" url:"created_by_remote_server_id,omitempty"`
    19  	CreatedByRemoteServerSyncId        int64                  `json:"created_by_remote_server_sync_id,omitempty" path:"created_by_remote_server_sync_id,omitempty" url:"created_by_remote_server_sync_id,omitempty"`
    20  	CustomMetadata                     map[string]interface{} `json:"custom_metadata,omitempty" path:"custom_metadata,omitempty" url:"custom_metadata,omitempty"`
    21  	DisplayName                        string                 `json:"display_name,omitempty" path:"display_name,omitempty" url:"display_name,omitempty"`
    22  	Type                               string                 `json:"type,omitempty" path:"type,omitempty" url:"type,omitempty"`
    23  	Size                               int64                  `json:"size,omitempty" path:"size,omitempty" url:"size,omitempty"`
    24  	CreatedAt                          *time.Time             `json:"created_at,omitempty" path:"created_at,omitempty" url:"created_at,omitempty"`
    25  	LastModifiedById                   int64                  `json:"last_modified_by_id,omitempty" path:"last_modified_by_id,omitempty" url:"last_modified_by_id,omitempty"`
    26  	LastModifiedByApiKeyId             int64                  `json:"last_modified_by_api_key_id,omitempty" path:"last_modified_by_api_key_id,omitempty" url:"last_modified_by_api_key_id,omitempty"`
    27  	LastModifiedByAutomationId         int64                  `json:"last_modified_by_automation_id,omitempty" path:"last_modified_by_automation_id,omitempty" url:"last_modified_by_automation_id,omitempty"`
    28  	LastModifiedByBundleRegistrationId int64                  `json:"last_modified_by_bundle_registration_id,omitempty" path:"last_modified_by_bundle_registration_id,omitempty" url:"last_modified_by_bundle_registration_id,omitempty"`
    29  	LastModifiedByRemoteServerId       int64                  `json:"last_modified_by_remote_server_id,omitempty" path:"last_modified_by_remote_server_id,omitempty" url:"last_modified_by_remote_server_id,omitempty"`
    30  	LastModifiedByRemoteServerSyncId   int64                  `json:"last_modified_by_remote_server_sync_id,omitempty" path:"last_modified_by_remote_server_sync_id,omitempty" url:"last_modified_by_remote_server_sync_id,omitempty"`
    31  	Mtime                              *time.Time             `json:"mtime,omitempty" path:"mtime,omitempty" url:"mtime,omitempty"`
    32  	ProvidedMtime                      *time.Time             `json:"provided_mtime,omitempty" path:"provided_mtime,omitempty" url:"provided_mtime,omitempty"`
    33  	Crc32                              string                 `json:"crc32,omitempty" path:"crc32,omitempty" url:"crc32,omitempty"`
    34  	Md5                                string                 `json:"md5,omitempty" path:"md5,omitempty" url:"md5,omitempty"`
    35  	MimeType                           string                 `json:"mime_type,omitempty" path:"mime_type,omitempty" url:"mime_type,omitempty"`
    36  	Region                             string                 `json:"region,omitempty" path:"region,omitempty" url:"region,omitempty"`
    37  	Permissions                        string                 `json:"permissions,omitempty" path:"permissions,omitempty" url:"permissions,omitempty"`
    38  	SubfoldersLocked                   *bool                  `json:"subfolders_locked?,omitempty" path:"subfolders_locked?,omitempty" url:"subfolders_locked?,omitempty"`
    39  	IsLocked                           *bool                  `json:"is_locked,omitempty" path:"is_locked,omitempty" url:"is_locked,omitempty"`
    40  	DownloadUri                        string                 `json:"download_uri,omitempty" path:"download_uri,omitempty" url:"download_uri,omitempty"`
    41  	PriorityColor                      string                 `json:"priority_color,omitempty" path:"priority_color,omitempty" url:"priority_color,omitempty"`
    42  	PreviewId                          int64                  `json:"preview_id,omitempty" path:"preview_id,omitempty" url:"preview_id,omitempty"`
    43  	Preview                            Preview                `json:"preview,omitempty" path:"preview,omitempty" url:"preview,omitempty"`
    44  	Action                             string                 `json:"action,omitempty" path:"action,omitempty" url:"action,omitempty"`
    45  	Length                             int64                  `json:"length,omitempty" path:"length,omitempty" url:"length,omitempty"`
    46  	MkdirParents                       *bool                  `json:"mkdir_parents,omitempty" path:"mkdir_parents,omitempty" url:"mkdir_parents,omitempty"`
    47  	Part                               int64                  `json:"part,omitempty" path:"part,omitempty" url:"part,omitempty"`
    48  	Parts                              int64                  `json:"parts,omitempty" path:"parts,omitempty" url:"parts,omitempty"`
    49  	Ref                                string                 `json:"ref,omitempty" path:"ref,omitempty" url:"ref,omitempty"`
    50  	Restart                            int64                  `json:"restart,omitempty" path:"restart,omitempty" url:"restart,omitempty"`
    51  	Structure                          string                 `json:"structure,omitempty" path:"structure,omitempty" url:"structure,omitempty"`
    52  	WithRename                         *bool                  `json:"with_rename,omitempty" path:"with_rename,omitempty" url:"with_rename,omitempty"`
    53  }
    54  
    55  func (f File) Identifier() interface{} {
    56  	return f.Path
    57  }
    58  
    59  type FileCollection []File
    60  
    61  type EtagsParam struct {
    62  	Etag string `url:"etag,omitempty" json:"etag,omitempty" path:"etag"`
    63  	Part string `url:"part,omitempty" json:"part,omitempty" path:"part"`
    64  }
    65  
    66  // Download file
    67  type FileDownloadParams struct {
    68  	Path              string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    69  	Action            string `url:"action,omitempty" required:"false" json:"action,omitempty" path:"action"`
    70  	PreviewSize       string `url:"preview_size,omitempty" required:"false" json:"preview_size,omitempty" path:"preview_size"`
    71  	WithPreviews      *bool  `url:"with_previews,omitempty" required:"false" json:"with_previews,omitempty" path:"with_previews"`
    72  	WithPriorityColor *bool  `url:"with_priority_color,omitempty" required:"false" json:"with_priority_color,omitempty" path:"with_priority_color"`
    73  	File              File   `url:"-,omitempty" required:"false" json:"-,omitempty"`
    74  }
    75  
    76  type FileCreateParams struct {
    77  	Path          string       `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    78  	Action        string       `url:"action,omitempty" required:"false" json:"action,omitempty" path:"action"`
    79  	EtagsParam    []EtagsParam `url:"etags,omitempty" required:"false" json:"etags,omitempty" path:"etags"`
    80  	Length        int64        `url:"length,omitempty" required:"false" json:"length,omitempty" path:"length"`
    81  	MkdirParents  *bool        `url:"mkdir_parents,omitempty" required:"false" json:"mkdir_parents,omitempty" path:"mkdir_parents"`
    82  	Part          int64        `url:"part,omitempty" required:"false" json:"part,omitempty" path:"part"`
    83  	Parts         int64        `url:"parts,omitempty" required:"false" json:"parts,omitempty" path:"parts"`
    84  	ProvidedMtime *time.Time   `url:"provided_mtime,omitempty" required:"false" json:"provided_mtime,omitempty" path:"provided_mtime"`
    85  	Ref           string       `url:"ref,omitempty" required:"false" json:"ref,omitempty" path:"ref"`
    86  	Restart       int64        `url:"restart,omitempty" required:"false" json:"restart,omitempty" path:"restart"`
    87  	Size          int64        `url:"size,omitempty" required:"false" json:"size,omitempty" path:"size"`
    88  	Structure     string       `url:"structure,omitempty" required:"false" json:"structure,omitempty" path:"structure"`
    89  	WithRename    *bool        `url:"with_rename,omitempty" required:"false" json:"with_rename,omitempty" path:"with_rename"`
    90  }
    91  
    92  type FileUpdateParams struct {
    93  	Path           string                 `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
    94  	CustomMetadata map[string]interface{} `url:"custom_metadata,omitempty" required:"false" json:"custom_metadata,omitempty" path:"custom_metadata"`
    95  	ProvidedMtime  *time.Time             `url:"provided_mtime,omitempty" required:"false" json:"provided_mtime,omitempty" path:"provided_mtime"`
    96  	PriorityColor  string                 `url:"priority_color,omitempty" required:"false" json:"priority_color,omitempty" path:"priority_color"`
    97  }
    98  
    99  type FileDeleteParams struct {
   100  	Path      string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   101  	Recursive *bool  `url:"recursive,omitempty" required:"false" json:"recursive,omitempty" path:"recursive"`
   102  }
   103  
   104  type FileFindParams struct {
   105  	Path              string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   106  	PreviewSize       string `url:"preview_size,omitempty" required:"false" json:"preview_size,omitempty" path:"preview_size"`
   107  	WithPreviews      *bool  `url:"with_previews,omitempty" required:"false" json:"with_previews,omitempty" path:"with_previews"`
   108  	WithPriorityColor *bool  `url:"with_priority_color,omitempty" required:"false" json:"with_priority_color,omitempty" path:"with_priority_color"`
   109  }
   110  
   111  // Copy file/folder
   112  type FileCopyParams struct {
   113  	Path        string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   114  	Destination string `url:"destination,omitempty" required:"true" json:"destination,omitempty" path:"destination"`
   115  	Structure   *bool  `url:"structure,omitempty" required:"false" json:"structure,omitempty" path:"structure"`
   116  	Overwrite   *bool  `url:"overwrite,omitempty" required:"false" json:"overwrite,omitempty" path:"overwrite"`
   117  }
   118  
   119  // Move file/folder
   120  type FileMoveParams struct {
   121  	Path        string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   122  	Destination string `url:"destination,omitempty" required:"true" json:"destination,omitempty" path:"destination"`
   123  	Overwrite   *bool  `url:"overwrite,omitempty" required:"false" json:"overwrite,omitempty" path:"overwrite"`
   124  }
   125  
   126  // Begin file upload
   127  type FileBeginUploadParams struct {
   128  	Path         string `url:"-,omitempty" required:"false" json:"-,omitempty" path:"path"`
   129  	MkdirParents *bool  `url:"mkdir_parents,omitempty" required:"false" json:"mkdir_parents,omitempty" path:"mkdir_parents"`
   130  	Part         int64  `url:"part,omitempty" required:"false" json:"part,omitempty" path:"part"`
   131  	Parts        int64  `url:"parts,omitempty" required:"false" json:"parts,omitempty" path:"parts"`
   132  	Ref          string `url:"ref,omitempty" required:"false" json:"ref,omitempty" path:"ref"`
   133  	Restart      int64  `url:"restart,omitempty" required:"false" json:"restart,omitempty" path:"restart"`
   134  	Size         int64  `url:"size,omitempty" required:"false" json:"size,omitempty" path:"size"`
   135  	WithRename   *bool  `url:"with_rename,omitempty" required:"false" json:"with_rename,omitempty" path:"with_rename"`
   136  }
   137  
   138  func (f File) ToFolder() (Folder, error) {
   139  	bodyBytes, err := json.Marshal(f)
   140  	if err != nil {
   141  		return Folder{}, err
   142  	}
   143  	folder := Folder{}
   144  	folder.UnmarshalJSON(bodyBytes)
   145  	return folder, nil
   146  }
   147  
   148  func (f File) String() string {
   149  	return f.Path
   150  }
   151  
   152  func (f File) Iterable() bool {
   153  	return f.IsDir()
   154  }
   155  
   156  func (f File) IsDir() bool {
   157  	return f.Type == "directory"
   158  }
   159  
   160  func (f File) ModTime() time.Time {
   161  	if f.ProvidedMtime != nil {
   162  		return *f.ProvidedMtime
   163  	}
   164  	if f.Mtime != nil {
   165  		return *f.Mtime
   166  	}
   167  	return time.Time{}
   168  }
   169  
   170  func (f *File) UnmarshalJSON(data []byte) error {
   171  	type file File
   172  	var v file
   173  	if err := json.Unmarshal(data, &v); err != nil {
   174  		return lib.ErrorWithOriginalResponse{}.ProcessError(data, err, map[string]interface{}{})
   175  	}
   176  
   177  	*f = File(v)
   178  	return nil
   179  }
   180  
   181  func (f *FileCollection) UnmarshalJSON(data []byte) error {
   182  	type files FileCollection
   183  	var v files
   184  	if err := json.Unmarshal(data, &v); err != nil {
   185  		return lib.ErrorWithOriginalResponse{}.ProcessError(data, err, []map[string]interface{}{})
   186  	}
   187  
   188  	*f = FileCollection(v)
   189  	return nil
   190  }
   191  
   192  func (f *FileCollection) ToSlice() *[]interface{} {
   193  	ret := make([]interface{}, len(*f))
   194  	for i, v := range *f {
   195  		ret[i] = v
   196  	}
   197  
   198  	return &ret
   199  }