|
| 1 | +# frozen_string_literal: true |
| 2 | +require_relative '../constant' |
| 3 | +require_relative '../utils/option_validator' |
| 4 | + |
| 5 | +module ImageKitIo |
| 6 | + module ApiService |
| 7 | + class File |
| 8 | + include Utils::OptionValidator |
| 9 | + include Constantable |
| 10 | + |
| 11 | + # This File class holds file related operations like |
| 12 | + # upload, list etc |
| 13 | + def initialize(req_obj) |
| 14 | + @req_obj = req_obj |
| 15 | + end |
| 16 | + |
| 17 | + # uploads files with required arguments, supports bot url and binary |
| 18 | + # Options types: |
| 19 | + # - `extensions` should be array of hash |
| 20 | + # eg: option['extension'] = [ |
| 21 | + # { 'name' => 'remove-bg', 'options' => { 'add_shadow' => true } }, |
| 22 | + # { 'name' => 'google-auto-tagging', 'minConfidence' => 80 } |
| 23 | + # ] |
| 24 | + # - `custom_metadata` should be hash |
| 25 | + # eg: option['custom_metadata'] = { |
| 26 | + # "SKU": "VS882HJ2JD", |
| 27 | + # "price": 599.99, |
| 28 | + # "brand": "H&M", |
| 29 | + # "discount": 30 |
| 30 | + # } |
| 31 | + def upload(file, file_name, options) |
| 32 | + raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file |
| 33 | + raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name |
| 34 | + |
| 35 | + options = format_to_json(options, :extensions, Array) |
| 36 | + options = format_to_json(options, :custom_metadata, Hash) |
| 37 | + options = validate_upload_options(options || {}) |
| 38 | + if options.is_a?(FalseClass) |
| 39 | + raise ArgumentError, "Invalid Upload option" |
| 40 | + else |
| 41 | + headers = @req_obj.create_headers |
| 42 | + payload = {multipart: true, file: file, fileName: file_name}.merge(options) |
| 43 | + |
| 44 | + url = "#{constants.BASE_URL}#{constants.UPLOAD}" |
| 45 | + @req_obj.request("post", url, headers, payload) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def update_details(file_id, options) |
| 50 | + unless options.fetch(:tags, []).is_a?(Array) |
| 51 | + raise ArgumentError, constants.UPDATE_DATA_TAGS_INVALID |
| 52 | + end |
| 53 | + unless options.fetch(:custom_coordinates, "").is_a?(String) |
| 54 | + raise ArgumentError, constants.UPDATE_DATA_COORDS_INVALID |
| 55 | + end |
| 56 | + url = "#{constants.BASE_URL}/#{file_id}/details/" |
| 57 | + headers = @req_obj.create_headers |
| 58 | + payload = request_formatter(options) |
| 59 | + @req_obj.request("patch", url, headers, payload.to_json) |
| 60 | + end |
| 61 | + |
| 62 | + def list(options) |
| 63 | + # returns list of files on ImageKitIo Server |
| 64 | + # :options dictionary of options |
| 65 | + formatted_options = request_formatter(options) |
| 66 | + raise KeyError(constants.LIST_FILES_INPUT_MISSING) unless formatted_options.is_a?(Hash) |
| 67 | + url = constants.BASE_URL |
| 68 | + headers = @req_obj.create_headers.update({params: formatted_options}) |
| 69 | + @req_obj.request("get", url, headers, formatted_options) |
| 70 | + end |
| 71 | + |
| 72 | + def details(file_identifier) |
| 73 | + # Get detail of file by file_identifier |
| 74 | + url = "#{constants.BASE_URL}/#{file_identifier}/details/" |
| 75 | + headers = @req_obj.create_headers |
| 76 | + @req_obj.request("get", url, headers) |
| 77 | + end |
| 78 | + |
| 79 | + def get_metadata(file_id) |
| 80 | + # Get metadata of a file by file_id |
| 81 | + url = "#{constants.BASE_URL}/#{file_id}/metadata" |
| 82 | + @req_obj.request("get", url, @req_obj.create_headers) |
| 83 | + end |
| 84 | + |
| 85 | + def delete(file_id) |
| 86 | + # Delete a file_id by file_id |
| 87 | + url = "#{constants.BASE_URL}/#{file_id}" |
| 88 | + headers = @req_obj.create_headers |
| 89 | + @req_obj.request("delete", url, headers) |
| 90 | + end |
| 91 | + |
| 92 | + def purge_cache(file_url) |
| 93 | + |
| 94 | + # purges cache from server by file_url |
| 95 | + |
| 96 | + url = "#{constants.BASE_URL}/purge" |
| 97 | + payload = {'url': file_url} |
| 98 | + @req_obj.request("post", url, @req_obj.create_headers, payload) |
| 99 | + end |
| 100 | + |
| 101 | + def purge_cache_status(request_id) |
| 102 | + # This function is to get cache_status |
| 103 | + url = "#{constants.BASE_URL}/purge/#{request_id}" |
| 104 | + @req_obj.request("get", url, @req_obj.create_headers) |
| 105 | + end |
| 106 | + |
| 107 | + def get_metadata_from_remote_url(remote_file_url) |
| 108 | + if remote_file_url == "" |
| 109 | + raise ArgumentError, "remote_file_url is required" |
| 110 | + end |
| 111 | + url = "#{constants.REMOTE_METADATA_FULL_URL}?url=#{remote_file_url}" |
| 112 | + @req_obj.request("get", url, @req_obj.create_headers) |
| 113 | + end |
| 114 | + |
| 115 | + def stream_file(remote_file_url, &block) |
| 116 | + if remote_file_url == '' || remote_file_url.nil? |
| 117 | + raise ArgumentError, 'remote_file_url is required' |
| 118 | + end |
| 119 | + @req_obj.request_stream('get', remote_file_url, headers: @req_obj.create_headers, &block) |
| 120 | + end |
| 121 | + |
| 122 | + def copy(source_file_path, destination_path) |
| 123 | + if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil? |
| 124 | + raise ArgumentError, 'parameters required' |
| 125 | + end |
| 126 | + url = "#{constants.BASE_URL}/copy" |
| 127 | + payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path } |
| 128 | + @req_obj.request('post', url, @req_obj.create_headers, payload) |
| 129 | + end |
| 130 | + |
| 131 | + def move(source_file_path, destination_path) |
| 132 | + if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil? |
| 133 | + raise ArgumentError, 'parameters required' |
| 134 | + end |
| 135 | + url = "#{constants.BASE_URL}/move" |
| 136 | + payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path } |
| 137 | + @req_obj.request('post', url, @req_obj.create_headers, payload) |
| 138 | + end |
| 139 | + |
| 140 | + def rename(file_path, new_file_name, **options) |
| 141 | + if file_path == '' || file_path.nil? || new_file_name == '' || new_file_name.nil? |
| 142 | + raise ArgumentError, 'parameters required' |
| 143 | + end |
| 144 | + url = "#{constants.BASE_URL}/rename" |
| 145 | + payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)) |
| 146 | + @req_obj.request('put', url, @req_obj.create_headers, payload) |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | +end |
0 commit comments