github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/onedrive/api/types.go (about) 1 // Types passed and returned to and from the API 2 3 package api 4 5 import ( 6 "strings" 7 "time" 8 ) 9 10 const ( 11 timeFormat = `"` + time.RFC3339 + `"` 12 13 // PackageTypeOneNote is the package type value for OneNote files 14 PackageTypeOneNote = "oneNote" 15 ) 16 17 // Error is returned from one drive when things go wrong 18 type Error struct { 19 ErrorInfo struct { 20 Code string `json:"code"` 21 Message string `json:"message"` 22 InnerError struct { 23 Code string `json:"code"` 24 } `json:"innererror"` 25 } `json:"error"` 26 } 27 28 // Error returns a string for the error and satisfies the error interface 29 func (e *Error) Error() string { 30 out := e.ErrorInfo.Code 31 if e.ErrorInfo.InnerError.Code != "" { 32 out += ": " + e.ErrorInfo.InnerError.Code 33 } 34 out += ": " + e.ErrorInfo.Message 35 return out 36 } 37 38 // Check Error satisfies the error interface 39 var _ error = (*Error)(nil) 40 41 // Identity represents an identity of an actor. For example, and actor 42 // can be a user, device, or application. 43 type Identity struct { 44 DisplayName string `json:"displayName"` 45 ID string `json:"id"` 46 } 47 48 // IdentitySet is a keyed collection of Identity objects. It is used 49 // to represent a set of identities associated with various events for 50 // an item, such as created by or last modified by. 51 type IdentitySet struct { 52 User Identity `json:"user"` 53 Application Identity `json:"application"` 54 Device Identity `json:"device"` 55 } 56 57 // Quota groups storage space quota-related information on OneDrive into a single structure. 58 type Quota struct { 59 Total int64 `json:"total"` 60 Used int64 `json:"used"` 61 Remaining int64 `json:"remaining"` 62 Deleted int64 `json:"deleted"` 63 State string `json:"state"` // normal | nearing | critical | exceeded 64 } 65 66 // Drive is a representation of a drive resource 67 type Drive struct { 68 ID string `json:"id"` 69 DriveType string `json:"driveType"` 70 Owner IdentitySet `json:"owner"` 71 Quota Quota `json:"quota"` 72 } 73 74 // Timestamp represents represents date and time information for the 75 // OneDrive API, by using ISO 8601 and is always in UTC time. 76 type Timestamp time.Time 77 78 // MarshalJSON turns a Timestamp into JSON (in UTC) 79 func (t *Timestamp) MarshalJSON() (out []byte, err error) { 80 timeString := (*time.Time)(t).UTC().Format(timeFormat) 81 return []byte(timeString), nil 82 } 83 84 // UnmarshalJSON turns JSON into a Timestamp 85 func (t *Timestamp) UnmarshalJSON(data []byte) error { 86 newT, err := time.Parse(timeFormat, string(data)) 87 if err != nil { 88 return err 89 } 90 *t = Timestamp(newT) 91 return nil 92 } 93 94 // ItemReference groups data needed to reference a OneDrive item 95 // across the service into a single structure. 96 type ItemReference struct { 97 DriveID string `json:"driveId"` // Unique identifier for the Drive that contains the item. Read-only. 98 ID string `json:"id"` // Unique identifier for the item. Read/Write. 99 Path string `json:"path"` // Path that used to navigate to the item. Read/Write. 100 DriveType string `json:"driveType"` // Type of the drive, Read-Only 101 } 102 103 // RemoteItemFacet groups data needed to reference a OneDrive remote item 104 type RemoteItemFacet struct { 105 ID string `json:"id"` // The unique identifier of the item within the remote Drive. Read-only. 106 Name string `json:"name"` // The name of the item (filename and extension). Read-write. 107 CreatedBy IdentitySet `json:"createdBy"` // Identity of the user, device, and application which created the item. Read-only. 108 LastModifiedBy IdentitySet `json:"lastModifiedBy"` // Identity of the user, device, and application which last modified the item. Read-only. 109 CreatedDateTime Timestamp `json:"createdDateTime"` // Date and time of item creation. Read-only. 110 LastModifiedDateTime Timestamp `json:"lastModifiedDateTime"` // Date and time the item was last modified. Read-only. 111 Folder *FolderFacet `json:"folder"` // Folder metadata, if the item is a folder. Read-only. 112 File *FileFacet `json:"file"` // File metadata, if the item is a file. Read-only. 113 Package *PackageFacet `json:"package"` // If present, indicates that this item is a package instead of a folder or file. Packages are treated like files in some contexts and folders in others. Read-only. 114 FileSystemInfo *FileSystemInfoFacet `json:"fileSystemInfo"` // File system information on client. Read-write. 115 ParentReference *ItemReference `json:"parentReference"` // Parent information, if the item has a parent. Read-write. 116 Size int64 `json:"size"` // Size of the item in bytes. Read-only. 117 WebURL string `json:"webUrl"` // URL that displays the resource in the browser. Read-only. 118 } 119 120 // FolderFacet groups folder-related data on OneDrive into a single structure 121 type FolderFacet struct { 122 ChildCount int64 `json:"childCount"` // Number of children contained immediately within this container. 123 } 124 125 // HashesType groups different types of hashes into a single structure, for an item on OneDrive. 126 type HashesType struct { 127 Sha1Hash string `json:"sha1Hash"` // hex encoded SHA1 hash for the contents of the file (if available) 128 Crc32Hash string `json:"crc32Hash"` // hex encoded CRC32 value of the file (if available) 129 QuickXorHash string `json:"quickXorHash"` // base64 encoded QuickXorHash value of the file (if available) 130 } 131 132 // FileFacet groups file-related data on OneDrive into a single structure. 133 type FileFacet struct { 134 MimeType string `json:"mimeType"` // The MIME type for the file. This is determined by logic on the server and might not be the value provided when the file was uploaded. 135 Hashes HashesType `json:"hashes"` // Hashes of the file's binary content, if available. 136 } 137 138 // FileSystemInfoFacet contains properties that are reported by the 139 // device's local file system for the local version of an item. This 140 // facet can be used to specify the last modified date or created date 141 // of the item as it was on the local device. 142 type FileSystemInfoFacet struct { 143 CreatedDateTime Timestamp `json:"createdDateTime"` // The UTC date and time the file was created on a client. 144 LastModifiedDateTime Timestamp `json:"lastModifiedDateTime"` // The UTC date and time the file was last modified on a client. 145 } 146 147 // DeletedFacet indicates that the item on OneDrive has been 148 // deleted. In this version of the API, the presence (non-null) of the 149 // facet value indicates that the file was deleted. A null (or 150 // missing) value indicates that the file is not deleted. 151 type DeletedFacet struct { 152 } 153 154 // PackageFacet indicates that a DriveItem is the top level item 155 // in a "package" or a collection of items that should be treated as a collection instead of individual items. 156 // `oneNote` is the only currently defined value. 157 type PackageFacet struct { 158 Type string `json:"type"` 159 } 160 161 // Item represents metadata for an item in OneDrive 162 type Item struct { 163 ID string `json:"id"` // The unique identifier of the item within the Drive. Read-only. 164 Name string `json:"name"` // The name of the item (filename and extension). Read-write. 165 ETag string `json:"eTag"` // eTag for the entire item (metadata + content). Read-only. 166 CTag string `json:"cTag"` // An eTag for the content of the item. This eTag is not changed if only the metadata is changed. Read-only. 167 CreatedBy IdentitySet `json:"createdBy"` // Identity of the user, device, and application which created the item. Read-only. 168 LastModifiedBy IdentitySet `json:"lastModifiedBy"` // Identity of the user, device, and application which last modified the item. Read-only. 169 CreatedDateTime Timestamp `json:"createdDateTime"` // Date and time of item creation. Read-only. 170 LastModifiedDateTime Timestamp `json:"lastModifiedDateTime"` // Date and time the item was last modified. Read-only. 171 Size int64 `json:"size"` // Size of the item in bytes. Read-only. 172 ParentReference *ItemReference `json:"parentReference"` // Parent information, if the item has a parent. Read-write. 173 WebURL string `json:"webUrl"` // URL that displays the resource in the browser. Read-only. 174 Description string `json:"description"` // Provide a user-visible description of the item. Read-write. 175 Folder *FolderFacet `json:"folder"` // Folder metadata, if the item is a folder. Read-only. 176 File *FileFacet `json:"file"` // File metadata, if the item is a file. Read-only. 177 RemoteItem *RemoteItemFacet `json:"remoteItem"` // Remote Item metadata, if the item is a remote shared item. Read-only. 178 FileSystemInfo *FileSystemInfoFacet `json:"fileSystemInfo"` // File system information on client. Read-write. 179 // Image *ImageFacet `json:"image"` // Image metadata, if the item is an image. Read-only. 180 // Photo *PhotoFacet `json:"photo"` // Photo metadata, if the item is a photo. Read-only. 181 // Audio *AudioFacet `json:"audio"` // Audio metadata, if the item is an audio file. Read-only. 182 // Video *VideoFacet `json:"video"` // Video metadata, if the item is a video. Read-only. 183 // Location *LocationFacet `json:"location"` // Location metadata, if the item has location data. Read-only. 184 Package *PackageFacet `json:"package"` // If present, indicates that this item is a package instead of a folder or file. Packages are treated like files in some contexts and folders in others. Read-only. 185 Deleted *DeletedFacet `json:"deleted"` // Information about the deleted state of the item. Read-only. 186 } 187 188 // ViewDeltaResponse is the response to the view delta method 189 type ViewDeltaResponse struct { 190 Value []Item `json:"value"` // An array of Item objects which have been created, modified, or deleted. 191 NextLink string `json:"@odata.nextLink"` // A URL to retrieve the next available page of changes. 192 DeltaLink string `json:"@odata.deltaLink"` // A URL returned instead of @odata.nextLink after all current changes have been returned. Used to read the next set of changes in the future. 193 DeltaToken string `json:"@delta.token"` // A token value that can be used in the query string on manually-crafted calls to view.delta. Not needed if you're using nextLink and deltaLink. 194 } 195 196 // ListChildrenResponse is the response to the list children method 197 type ListChildrenResponse struct { 198 Value []Item `json:"value"` // An array of Item objects 199 NextLink string `json:"@odata.nextLink"` // A URL to retrieve the next available page of items. 200 } 201 202 // CreateItemRequest is the request to create an item object 203 type CreateItemRequest struct { 204 Name string `json:"name"` // Name of the folder to be created. 205 Folder FolderFacet `json:"folder"` // Empty Folder facet to indicate that folder is the type of resource to be created. 206 ConflictBehavior string `json:"@name.conflictBehavior"` // Determines what to do if an item with a matching name already exists in this folder. Accepted values are: rename, replace, and fail (the default). 207 } 208 209 // SetFileSystemInfo is used to Update an object's FileSystemInfo. 210 type SetFileSystemInfo struct { 211 FileSystemInfo FileSystemInfoFacet `json:"fileSystemInfo"` // File system information on client. Read-write. 212 } 213 214 // CreateUploadRequest is used by CreateUploadSession to set the dates correctly 215 type CreateUploadRequest struct { 216 Item SetFileSystemInfo `json:"item"` 217 } 218 219 // CreateUploadResponse is the response from creating an upload session 220 type CreateUploadResponse struct { 221 UploadURL string `json:"uploadUrl"` // "https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337", 222 ExpirationDateTime Timestamp `json:"expirationDateTime"` // "2015-01-29T09:21:55.523Z", 223 NextExpectedRanges []string `json:"nextExpectedRanges"` // ["0-"] 224 } 225 226 // UploadFragmentResponse is the response from uploading a fragment 227 type UploadFragmentResponse struct { 228 ExpirationDateTime Timestamp `json:"expirationDateTime"` // "2015-01-29T09:21:55.523Z", 229 NextExpectedRanges []string `json:"nextExpectedRanges"` // ["0-"] 230 } 231 232 // CopyItemRequest is the request to copy an item object 233 // 234 // Note: The parentReference should include either an id or path but 235 // not both. If both are included, they need to reference the same 236 // item or an error will occur. 237 type CopyItemRequest struct { 238 ParentReference ItemReference `json:"parentReference"` // Reference to the parent item the copy will be created in. 239 Name *string `json:"name"` // Optional The new name for the copy. If this isn't provided, the same name will be used as the original. 240 } 241 242 // MoveItemRequest is the request to copy an item object 243 // 244 // Note: The parentReference should include either an id or path but 245 // not both. If both are included, they need to reference the same 246 // item or an error will occur. 247 type MoveItemRequest struct { 248 ParentReference *ItemReference `json:"parentReference,omitempty"` // Reference to the destination parent directory 249 Name string `json:"name,omitempty"` // Optional The new name for the file. If this isn't provided, the same name will be used as the original. 250 FileSystemInfo *FileSystemInfoFacet `json:"fileSystemInfo,omitempty"` // File system information on client. Read-write. 251 } 252 253 //CreateShareLinkRequest is the request to create a sharing link 254 //Always Type:view and Scope:anonymous for public sharing 255 type CreateShareLinkRequest struct { 256 Type string `json:"type"` //Link type in View, Edit or Embed 257 Scope string `json:"scope,omitempty"` //Optional. Scope in anonymousi, organization 258 } 259 260 //CreateShareLinkResponse is the response from CreateShareLinkRequest 261 type CreateShareLinkResponse struct { 262 ID string `json:"id"` 263 Roles []string `json:"roles"` 264 Link struct { 265 Type string `json:"type"` 266 Scope string `json:"scope"` 267 WebURL string `json:"webUrl"` 268 Application struct { 269 ID string `json:"id"` 270 DisplayName string `json:"displayName"` 271 } `json:"application"` 272 } `json:"link"` 273 } 274 275 // AsyncOperationStatus provides information on the status of an asynchronous job progress. 276 // 277 // The following API calls return AsyncOperationStatus resources: 278 // 279 // Copy Item 280 // Upload From URL 281 type AsyncOperationStatus struct { 282 PercentageComplete float64 `json:"percentageComplete"` // A float value between 0 and 100 that indicates the percentage complete. 283 Status string `json:"status"` // A string value that maps to an enumeration of possible values about the status of the job. "notStarted | inProgress | completed | updating | failed | deletePending | deleteFailed | waiting" 284 } 285 286 // GetID returns a normalized ID of the item 287 // If DriveID is known it will be prefixed to the ID with # separator 288 // Can be parsed using onedrive.parseNormalizedID(normalizedID) 289 func (i *Item) GetID() string { 290 if i.IsRemote() && i.RemoteItem.ID != "" { 291 return i.RemoteItem.ParentReference.DriveID + "#" + i.RemoteItem.ID 292 } else if i.ParentReference != nil && strings.Index(i.ID, "#") == -1 { 293 return i.ParentReference.DriveID + "#" + i.ID 294 } 295 return i.ID 296 } 297 298 // GetDriveID returns a normalized ParentReference of the item 299 func (i *Item) GetDriveID() string { 300 return i.GetParentReference().DriveID 301 } 302 303 // GetName returns a normalized Name of the item 304 func (i *Item) GetName() string { 305 if i.IsRemote() && i.RemoteItem.Name != "" { 306 return i.RemoteItem.Name 307 } 308 return i.Name 309 } 310 311 // GetFolder returns a normalized Folder of the item 312 func (i *Item) GetFolder() *FolderFacet { 313 if i.IsRemote() && i.RemoteItem.Folder != nil { 314 return i.RemoteItem.Folder 315 } 316 return i.Folder 317 } 318 319 // GetPackage returns a normalized Package of the item 320 func (i *Item) GetPackage() *PackageFacet { 321 if i.IsRemote() && i.RemoteItem.Package != nil { 322 return i.RemoteItem.Package 323 } 324 return i.Package 325 } 326 327 // GetPackageType returns the package type of the item if available, 328 // otherwise "" 329 func (i *Item) GetPackageType() string { 330 pack := i.GetPackage() 331 if pack == nil { 332 return "" 333 } 334 return pack.Type 335 } 336 337 // GetFile returns a normalized File of the item 338 func (i *Item) GetFile() *FileFacet { 339 if i.IsRemote() && i.RemoteItem.File != nil { 340 return i.RemoteItem.File 341 } 342 return i.File 343 } 344 345 // GetFileSystemInfo returns a normalized FileSystemInfo of the item 346 func (i *Item) GetFileSystemInfo() *FileSystemInfoFacet { 347 if i.IsRemote() && i.RemoteItem.FileSystemInfo != nil { 348 return i.RemoteItem.FileSystemInfo 349 } 350 return i.FileSystemInfo 351 } 352 353 // GetSize returns a normalized Size of the item 354 func (i *Item) GetSize() int64 { 355 if i.IsRemote() && i.RemoteItem.Size != 0 { 356 return i.RemoteItem.Size 357 } 358 return i.Size 359 } 360 361 // GetWebURL returns a normalized WebURL of the item 362 func (i *Item) GetWebURL() string { 363 if i.IsRemote() && i.RemoteItem.WebURL != "" { 364 return i.RemoteItem.WebURL 365 } 366 return i.WebURL 367 } 368 369 // GetCreatedBy returns a normalized CreatedBy of the item 370 func (i *Item) GetCreatedBy() IdentitySet { 371 if i.IsRemote() && i.RemoteItem.CreatedBy != (IdentitySet{}) { 372 return i.RemoteItem.CreatedBy 373 } 374 return i.CreatedBy 375 } 376 377 // GetLastModifiedBy returns a normalized LastModifiedBy of the item 378 func (i *Item) GetLastModifiedBy() IdentitySet { 379 if i.IsRemote() && i.RemoteItem.LastModifiedBy != (IdentitySet{}) { 380 return i.RemoteItem.LastModifiedBy 381 } 382 return i.LastModifiedBy 383 } 384 385 // GetCreatedDateTime returns a normalized CreatedDateTime of the item 386 func (i *Item) GetCreatedDateTime() Timestamp { 387 if i.IsRemote() && i.RemoteItem.CreatedDateTime != (Timestamp{}) { 388 return i.RemoteItem.CreatedDateTime 389 } 390 return i.CreatedDateTime 391 } 392 393 // GetLastModifiedDateTime returns a normalized LastModifiedDateTime of the item 394 func (i *Item) GetLastModifiedDateTime() Timestamp { 395 if i.IsRemote() && i.RemoteItem.LastModifiedDateTime != (Timestamp{}) { 396 return i.RemoteItem.LastModifiedDateTime 397 } 398 return i.LastModifiedDateTime 399 } 400 401 // GetParentReference returns a normalized ParentReference of the item 402 func (i *Item) GetParentReference() *ItemReference { 403 if i.IsRemote() && i.ParentReference == nil { 404 return i.RemoteItem.ParentReference 405 } 406 return i.ParentReference 407 } 408 409 // IsRemote checks if item is a remote item 410 func (i *Item) IsRemote() bool { 411 return i.RemoteItem != nil 412 }