|
| 1 | +require_relative "../../lib/imagekit/sdk/version" |
| 2 | +require_relative "../../lib/imagekit/imagekit" |
| 3 | +require "base64" |
| 4 | + |
| 5 | +private_key = "" |
| 6 | +public_key = "" |
| 7 | +url_endpoint = "" |
| 8 | +# dummy image url |
| 9 | +url = "https://homepages.cae.wisc.edu/~ece533/images/cat.png" |
| 10 | + |
| 11 | +imagekitio = ImageKit::ImageKitClient.new(private_key, public_key, url_endpoint) |
| 12 | + |
| 13 | +# URL generation using image path and image hostname |
| 14 | +gen_url = imagekitio.url({path: "/default-image.jpg", |
| 15 | + url_endpoint: url_endpoint, |
| 16 | + transformation: [{height: "300", width: "400"}],}) |
| 17 | + |
| 18 | +puts "-------------------------------------" |
| 19 | +puts "generated url => #{gen_url}" |
| 20 | + |
| 21 | + |
| 22 | +# 2 Using full image URL |
| 23 | +image_url = imagekitio.url({src: url_endpoint.chomp("/") + "/default-image.jpg", |
| 24 | + transformation: [{height: "300", width: "400"}],}) |
| 25 | +puts "-------------------------------------" |
| 26 | + |
| 27 | +puts "Url using full image url => #{image_url}" |
| 28 | + |
| 29 | + |
| 30 | +image_url = imagekitio.url({src: url_endpoint + "default-image.jpg", |
| 31 | + transformation: [{format: "jpg", |
| 32 | + progressive: "true", |
| 33 | + effect_sharpen: "-", |
| 34 | + effect_contrast: "1",}],}) |
| 35 | + |
| 36 | +print("-------------------------------------", "\n") |
| 37 | +puts "sharpening and contrast transforms => #{image_url}" |
| 38 | +# 4. Sharpening and contrast transforms and a progressive JPG image |
| 39 | +print("-------------------------------------") |
| 40 | + |
| 41 | + |
| 42 | +# Uploading image from file |
| 43 | +file = open("sample.jpg", "rb") |
| 44 | +upload = imagekitio.upload_file(file, "testing.jpg", { |
| 45 | + response_fields: 'tags,customCoordinates,isPrivateFile,metadata', |
| 46 | + tags: %w[abc def], |
| 47 | + use_unique_file_name: false, |
| 48 | + is_private_file: true |
| 49 | + },) |
| 50 | +puts "------------------------------------------", "\n" |
| 51 | +puts "Upload Private with binary => ", upload |
| 52 | + |
| 53 | + |
| 54 | +# signed url |
| 55 | +url = imagekitio.url({path: upload[:response]["filePath"], |
| 56 | + transformation: [{'height': "300", 'width': "400"}], |
| 57 | + signed: true, |
| 58 | + expire_seconds: 10}) |
| 59 | +print("-------------------------------------", "\n") |
| 60 | +puts "Signed url => #{url}" |
| 61 | + |
| 62 | + |
| 63 | +# Uploading image from Base64 |
| 64 | +image64 = Base64.encode64(File.open("sample.jpg", "rb").read) |
| 65 | + |
| 66 | +upload = imagekitio.upload_file( |
| 67 | + file = image64, file_name = "testing", |
| 68 | + options = { |
| 69 | + response_fields: 'tags,customCoordinates,isPrivateFile,metadata', |
| 70 | + tags: %w[abc def], |
| 71 | + use_unique_file_name: true, |
| 72 | + }, |
| 73 | + ) |
| 74 | +puts "------------------------------------------", "\n" |
| 75 | +puts "Upload with base64 => ", upload |
| 76 | + |
| 77 | +# Uploading image from remote URL |
| 78 | +upload = imagekitio.upload_file( |
| 79 | + file = "https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg", |
| 80 | + file_name = "testing", |
| 81 | + options = { |
| 82 | + response_fields: 'tags,customCoordinates,isPrivateFile,metadata', |
| 83 | + tags: %w[abc def], |
| 84 | + use_unique_file_name: true, |
| 85 | + }, |
| 86 | + ) |
| 87 | +puts "------------------------------------------", "\n" |
| 88 | +puts "Upload with url => #{upload}" |
| 89 | + |
| 90 | +# Uploading repeat to grow number of files in the server. |
| 91 | +upload = imagekitio.upload_file( |
| 92 | + file = "https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg", |
| 93 | + file_name = "testing", |
| 94 | + options = { |
| 95 | + response_fields: 'tags,customCoordinates,isPrivateFile,metadata', |
| 96 | + tags: %w[abc def], |
| 97 | + use_unique_file_name: true, |
| 98 | + }, |
| 99 | + ) |
| 100 | +puts "------------------------------------------", "\n" |
| 101 | +puts "Upload with url => #{upload}" |
| 102 | +upload = imagekitio.upload_file( |
| 103 | + file = "https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg", |
| 104 | + file_name = "testing", |
| 105 | + options = { |
| 106 | + response_fields: 'tags,customCoordinates,isPrivateFile,metadata', |
| 107 | + tags: %w[abc def], |
| 108 | + use_unique_file_name: true, |
| 109 | + }, |
| 110 | + ) |
| 111 | +puts "------------------------------------------", "\n" |
| 112 | +puts "Upload with url => #{upload}" |
| 113 | + |
| 114 | + |
| 115 | +# Getting file list from server |
| 116 | +list_files = imagekitio.list_files({skip: 0, limit: 5}) |
| 117 | +bulk_ids = Array[list_files[:response][1]["fileId"],list_files[:response][2]["fileId"],list_files[:response][3]["fileId"], list_files[:response][4]["fileId"],] |
| 118 | + |
| 119 | +puts bulk_ids |
| 120 | +puts list_files[:response][0]["fileId"] |
| 121 | +print("-------------------------------------", "\n") |
| 122 | + |
| 123 | +print("List files => ", "\n", list_files, "\n") |
| 124 | + |
| 125 | + |
| 126 | +# puts "upload with image url", upload |
| 127 | +updated_detail = imagekitio.update_file_details( |
| 128 | + list_files[:response][0]["fileId"], |
| 129 | + { |
| 130 | + "tags": ['image_tag1'], |
| 131 | + "custom_coordinates": "10,10,100,200" |
| 132 | + } |
| 133 | +) |
| 134 | + |
| 135 | +puts "------------------------------------------", "\n" |
| 136 | + |
| 137 | +print("Updated detail => ", updated_detail, "\n\n") |
| 138 | + |
| 139 | + |
| 140 | +details = imagekitio.get_file_details(list_files[:response][0]["fileId"]) |
| 141 | + |
| 142 | +puts "------------------------------------------", "\n" |
| 143 | + |
| 144 | +print("File detail => ", details, "\n\n") |
| 145 | + |
| 146 | + |
| 147 | +file_metadata = imagekitio.get_file_metadata(list_files[:response][0]["fileId"]) |
| 148 | + |
| 149 | +puts "------------------------------------------", "\n" |
| 150 | +puts "File Metadata => #{file_metadata}" |
| 151 | + |
| 152 | +print("-------------------------------------") |
| 153 | + |
| 154 | +delete = imagekitio.delete_file(list_files[:response][0]["fileId"],) |
| 155 | + |
| 156 | +puts "------------------------------------------", "\n" |
| 157 | +puts "Delete file => #{delete}", "\n" |
| 158 | + |
| 159 | +puts "-------------------------------------" |
| 160 | +purge_cache = imagekitio.purge_file_cache(file_url = image_url) |
| 161 | +puts "Purge cache => #{purge_cache}", "\n" |
| 162 | + |
| 163 | +request_id = purge_cache[:response]["requestId"] |
| 164 | +purge_cache_status = imagekitio.purge_file_cache_status(request_id) |
| 165 | + |
| 166 | +puts "-------------------------------------", "\n" |
| 167 | + |
| 168 | +print("Cache status => ", purge_cache_status, "\n") |
| 169 | + |
| 170 | +puts "-------------------------------------", "\n" |
| 171 | + |
| 172 | +auth_params = imagekitio.get_authentication_parameters |
| 173 | +puts "Auth params => ", auth_params, "\n" |
| 174 | + |
| 175 | +print("-------------------------------------", "\n") |
| 176 | +print("Phash distance => ", imagekitio.phash_distance("f06830ca9f1e3e90", "f06830ca9f1e3e90"), "\n") |
| 177 | + |
| 178 | +print("-------------------------------------") |
| 179 | + |
| 180 | + |
| 181 | +puts "-------------------------------------", "\n" |
| 182 | +remote_file_url = upload[:response]["url"] |
| 183 | + |
| 184 | +print("Get metadata => ", imagekitio.get_remote_file_url_metadata(list_files[:response][1]["url"])) |
| 185 | + |
| 186 | +puts "-------------------------------------", "\n" |
| 187 | + |
| 188 | + |
| 189 | +print("Bulk File delete => ", imagekitio.bulk_file_delete(bulk_ids)) |
0 commit comments