github.com/artpar/rclone@v1.67.3/backend/imagekit/client/media.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"net/url"
     9  	"time"
    10  
    11  	"github.com/artpar/rclone/lib/rest"
    12  	"gopkg.in/validator.v2"
    13  )
    14  
    15  // FilesOrFolderParam struct is a parameter type to ListFiles() function to search / list media library files.
    16  type FilesOrFolderParam struct {
    17  	Path        string `json:"path,omitempty"`
    18  	Limit       int    `json:"limit,omitempty"`
    19  	Skip        int    `json:"skip,omitempty"`
    20  	SearchQuery string `json:"searchQuery,omitempty"`
    21  }
    22  
    23  // AITag represents an AI tag for a media library file.
    24  type AITag struct {
    25  	Name       string  `json:"name"`
    26  	Confidence float32 `json:"confidence"`
    27  	Source     string  `json:"source"`
    28  }
    29  
    30  // File represents media library File details.
    31  type File struct {
    32  	FileID            string            `json:"fileId"`
    33  	Name              string            `json:"name"`
    34  	FilePath          string            `json:"filePath"`
    35  	Type              string            `json:"type"`
    36  	VersionInfo       map[string]string `json:"versionInfo"`
    37  	IsPrivateFile     *bool             `json:"isPrivateFile"`
    38  	CustomCoordinates *string           `json:"customCoordinates"`
    39  	URL               string            `json:"url"`
    40  	Thumbnail         string            `json:"thumbnail"`
    41  	FileType          string            `json:"fileType"`
    42  	Mime              string            `json:"mime"`
    43  	Height            int               `json:"height"`
    44  	Width             int               `json:"Width"`
    45  	Size              uint64            `json:"size"`
    46  	HasAlpha          bool              `json:"hasAlpha"`
    47  	CustomMetadata    map[string]any    `json:"customMetadata,omitempty"`
    48  	EmbeddedMetadata  map[string]any    `json:"embeddedMetadata"`
    49  	CreatedAt         time.Time         `json:"createdAt"`
    50  	UpdatedAt         time.Time         `json:"updatedAt"`
    51  	Tags              []string          `json:"tags"`
    52  	AITags            []AITag           `json:"AITags"`
    53  }
    54  
    55  // Folder represents media library Folder details.
    56  type Folder struct {
    57  	*File
    58  	FolderPath string `json:"folderPath"`
    59  }
    60  
    61  // CreateFolderParam represents parameter to create folder api
    62  type CreateFolderParam struct {
    63  	FolderName       string `validate:"nonzero" json:"folderName"`
    64  	ParentFolderPath string `validate:"nonzero" json:"parentFolderPath"`
    65  }
    66  
    67  // DeleteFolderParam represents parameter to delete folder api
    68  type DeleteFolderParam struct {
    69  	FolderPath string `validate:"nonzero" json:"folderPath"`
    70  }
    71  
    72  // MoveFolderParam represents parameter to move folder api
    73  type MoveFolderParam struct {
    74  	SourceFolderPath string `validate:"nonzero" json:"sourceFolderPath"`
    75  	DestinationPath  string `validate:"nonzero" json:"destinationPath"`
    76  }
    77  
    78  // JobIDResponse respresents response struct with JobID for folder operations
    79  type JobIDResponse struct {
    80  	JobID string `json:"jobId"`
    81  }
    82  
    83  // JobStatus represents response Data to job status api
    84  type JobStatus struct {
    85  	JobID  string `json:"jobId"`
    86  	Type   string `json:"type"`
    87  	Status string `json:"status"`
    88  }
    89  
    90  // File represents media library File details.
    91  func (ik *ImageKit) File(ctx context.Context, fileID string) (*http.Response, *File, error) {
    92  	data := &File{}
    93  	response, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
    94  		Method:       "GET",
    95  		Path:         fmt.Sprintf("/files/%s/details", fileID),
    96  		RootURL:      ik.Prefix,
    97  		IgnoreStatus: true,
    98  	}, nil, data)
    99  
   100  	return response, data, err
   101  }
   102  
   103  // Files retrieves media library files. Filter options can be supplied as FilesOrFolderParam.
   104  func (ik *ImageKit) Files(ctx context.Context, params FilesOrFolderParam, includeVersion bool) (*http.Response, *[]File, error) {
   105  	var SearchQuery = `type = "file"`
   106  
   107  	if includeVersion {
   108  		SearchQuery = `type IN ["file", "file-version"]`
   109  	}
   110  	if params.SearchQuery != "" {
   111  		SearchQuery = params.SearchQuery
   112  	}
   113  
   114  	parameters := url.Values{}
   115  
   116  	parameters.Set("skip", fmt.Sprintf("%d", params.Skip))
   117  	parameters.Set("limit", fmt.Sprintf("%d", params.Limit))
   118  	parameters.Set("path", params.Path)
   119  	parameters.Set("searchQuery", SearchQuery)
   120  
   121  	data := &[]File{}
   122  
   123  	response, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   124  		Method:     "GET",
   125  		Path:       "/files",
   126  		RootURL:    ik.Prefix,
   127  		Parameters: parameters,
   128  	}, nil, data)
   129  
   130  	return response, data, err
   131  }
   132  
   133  // DeleteFile removes file by FileID from media library
   134  func (ik *ImageKit) DeleteFile(ctx context.Context, fileID string) (*http.Response, error) {
   135  	var err error
   136  
   137  	if fileID == "" {
   138  		return nil, errors.New("fileID can not be empty")
   139  	}
   140  
   141  	response, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   142  		Method:     "DELETE",
   143  		Path:       fmt.Sprintf("/files/%s", fileID),
   144  		RootURL:    ik.Prefix,
   145  		NoResponse: true,
   146  	}, nil, nil)
   147  
   148  	return response, err
   149  }
   150  
   151  // Folders retrieves media library files. Filter options can be supplied as FilesOrFolderParam.
   152  func (ik *ImageKit) Folders(ctx context.Context, params FilesOrFolderParam) (*http.Response, *[]Folder, error) {
   153  	var SearchQuery = `type = "folder"`
   154  
   155  	if params.SearchQuery != "" {
   156  		SearchQuery = params.SearchQuery
   157  	}
   158  
   159  	parameters := url.Values{}
   160  
   161  	parameters.Set("skip", fmt.Sprintf("%d", params.Skip))
   162  	parameters.Set("limit", fmt.Sprintf("%d", params.Limit))
   163  	parameters.Set("path", params.Path)
   164  	parameters.Set("searchQuery", SearchQuery)
   165  
   166  	data := &[]Folder{}
   167  
   168  	resp, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   169  		Method:     "GET",
   170  		Path:       "/files",
   171  		RootURL:    ik.Prefix,
   172  		Parameters: parameters,
   173  	}, nil, data)
   174  
   175  	if err != nil {
   176  		return resp, data, err
   177  	}
   178  
   179  	return resp, data, err
   180  }
   181  
   182  // CreateFolder creates a new folder in media library
   183  func (ik *ImageKit) CreateFolder(ctx context.Context, param CreateFolderParam) (*http.Response, error) {
   184  	var err error
   185  
   186  	if err = validator.Validate(&param); err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	response, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   191  		Method:     "POST",
   192  		Path:       "/folder",
   193  		RootURL:    ik.Prefix,
   194  		NoResponse: true,
   195  	}, param, nil)
   196  
   197  	return response, err
   198  }
   199  
   200  // DeleteFolder removes the folder from media library
   201  func (ik *ImageKit) DeleteFolder(ctx context.Context, param DeleteFolderParam) (*http.Response, error) {
   202  	var err error
   203  
   204  	if err = validator.Validate(&param); err != nil {
   205  		return nil, err
   206  	}
   207  
   208  	response, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   209  		Method:     "DELETE",
   210  		Path:       "/folder",
   211  		RootURL:    ik.Prefix,
   212  		NoResponse: true,
   213  	}, param, nil)
   214  
   215  	return response, err
   216  }
   217  
   218  // MoveFolder moves given folder to new path in media library
   219  func (ik *ImageKit) MoveFolder(ctx context.Context, param MoveFolderParam) (*http.Response, *JobIDResponse, error) {
   220  	var err error
   221  	var response = &JobIDResponse{}
   222  
   223  	if err = validator.Validate(&param); err != nil {
   224  		return nil, nil, err
   225  	}
   226  
   227  	resp, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   228  		Method:  "PUT",
   229  		Path:    "bulkJobs/moveFolder",
   230  		RootURL: ik.Prefix,
   231  	}, param, response)
   232  
   233  	return resp, response, err
   234  }
   235  
   236  // BulkJobStatus retrieves the status of a bulk job by job ID.
   237  func (ik *ImageKit) BulkJobStatus(ctx context.Context, jobID string) (*http.Response, *JobStatus, error) {
   238  	var err error
   239  	var response = &JobStatus{}
   240  
   241  	if jobID == "" {
   242  		return nil, nil, errors.New("jobId can not be blank")
   243  	}
   244  
   245  	resp, err := ik.HTTPClient.CallJSON(ctx, &rest.Opts{
   246  		Method:  "GET",
   247  		Path:    "bulkJobs/" + jobID,
   248  		RootURL: ik.Prefix,
   249  	}, nil, response)
   250  
   251  	return resp, response, err
   252  }