imagekitio 2.0.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db316e0a4525fa5a4f72d3f993da7fdb12480e18957cf40f0a84ec40c255ab2e
4
- data.tar.gz: f24d4d783f368bb5ba90807f3dec82f677f943a9ccff4373dd6d56471e524517
3
+ metadata.gz: aabd0c2da76f3f7008b3315212cc04f81434c6bb372dff4448176cf014db01c9
4
+ data.tar.gz: a9d42b5d5c729fc600ee01361dc78ee97200518f17bd196498a0ae543623b0b9
5
5
  SHA512:
6
- metadata.gz: 5cd39012d46c350b0cbaefce9dfe60fc7bdf5e8fd110fc01e05b06fa95c9bff171c679dc0d724cc41ddf1b968ea2f3fef3dd30e867d6f2da1935d0e4cd02afd8
7
- data.tar.gz: 360f33ade97be7c1c72e7c6be026008688dfced40b4201ba17d33b180f93270070a77e23718072fb779386f1707abb3c7f1b2ba6ba4ff5102cc03ca4747ad809
6
+ metadata.gz: e1bdc9b382371fe0620bb4f0f834037d823063ae924c2806cf78f09ffa644308b0f7739484da388a279b48913ab79fe29228b0328c8d4c8c15674e0a293a7d7b
7
+ data.tar.gz: 5395acd1194c6a218de14e86cc9e60a3bfb06bb048743967c6645f7452cff7b1978a3fa2bbd72cd8508f061ffa08b4947c12b3f986638b04bd7fa4458b32f222
data/README.md CHANGED
@@ -8,7 +8,9 @@
8
8
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
9
  [![Twitter Follow](https://img.shields.io/twitter/follow/imagekitio?label=Follow&style=social)](https://twitter.com/ImagekitIo)
10
10
 
11
- ImageKit gem for Ruby on Rails that allows you to use real-time [image resizing](https://docs.imagekit.io/features/image-transformations), [optimization](https://docs.imagekit.io/features/image-optimization), and [file uploading](https://docs.imagekit.io/api-reference/upload-file-api/).
11
+ Ruby on Rails gem for [ImageKit](https://imagekit.io/) implements the new APIs and interface for different file operations.
12
+
13
+ ImageKit is complete media storage, optimization, and transformation solution that comes with an [image and video CDN](https://imagekit.io/features/imagekit-infrastructure). It can be integrated with your existing infrastructure - storage like AWS S3, web servers, your CDN, and custom domain names, allowing you to deliver optimized images in minutes with minimal code changes.
12
14
 
13
15
  Table of contents -
14
16
  * [Installation](#Installation)
@@ -155,20 +157,17 @@ imagekitio:
155
157
 
156
158
  ## Usage
157
159
 
158
- You can use this Ruby SDK for three different methods - URL generation, file upload, and file management.
160
+ You can use this Ruby SDK for three different methods - URL generation, file upload, and media management operations. The usage of the SDK has been explained below.
159
161
 
160
- The usage of the SDK has been explained below.
162
+ * `URL Generation`
163
+ * `File Upload`
164
+ * `File Management`
161
165
 
162
166
  ## URL generation
163
167
 
164
168
  **1. Using Image path and image hostname or endpoint**
165
169
 
166
- This method allows you to create a URL using the path where the image exists and the URL
167
- endpoint(url_endpoint) you want to use to access the image. You can refer to the documentation
168
- [here](https://docs.imagekit.io/integration/url-endpoints) to read more about URL endpoints
169
- in ImageKit and the section about [image origins](https://docs.imagekit.io/integration/configure-origin) to understand
170
- about paths with different kinds of origins.
171
-
170
+ This method allows you to create an URL to access a file using the relative file path and the ImageKit URL endpoint (`urlEndpoint`). The file can be an image, video or any other static file supported by ImageKit.
172
171
 
173
172
  ```ruby
174
173
  imagekitio = ImageKitIo.client
@@ -185,8 +184,7 @@ https://ik.imagekit.io/your_imagekit_id/endpoint/tr:h-300,w-400,ar-4-3,q-40/defa
185
184
  ```
186
185
 
187
186
  **2.Using full image URL**
188
- This method allows you to add transformation parameters to an existing, complete URL already mapped to ImageKit using the `src` parameter. This method should be used if you have the complete image URL mapped to ImageKit stored in your
189
- database.
187
+ This method allows you to add transformation parameters to an absolute URL. For example, if you have configured a custom CNAME and have absolute asset URLs in your database or CMS, you will often need this.
190
188
 
191
189
 
192
190
  ```ruby
@@ -286,8 +284,7 @@ https://ik.imagekit.io/your_imagekit_id/tr:h-300,w-400/default-image.jpg?v=123&i
286
284
 
287
285
  **List of transformations**
288
286
 
289
- The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations).
290
- The SDK gives a name to each transformation parameter, making the code simpler, making the code simpler, and readable.
287
+ The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations/resize-crop-and-other-transformations). The SDK gives a name to each transformation parameter, making the code simpler, making the code simpler, and readable.
291
288
 
292
289
  If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the transformation code from ImageKit docs as the name when using the `url` function.
293
290
 
@@ -385,7 +382,7 @@ be `None`.
385
382
  The SDK provides a simple interface for all the [media APIs mentioned here](https://docs.imagekit.io/api-reference/media-api)
386
383
  to manage your files. This also returns `error` and `result`. The `error` will be `None` if API succeeds.
387
384
 
388
- **1. List & Search Files**
385
+ **List & Search Files**
389
386
 
390
387
  Accepts an object specifying the parameters to be used to list and search files. All parameters specified
391
388
  in the [documentation here](https://docs.imagekit.io/api-reference/media-api/list-and-search-files#list-and-search-file-api) can be passed with the
@@ -397,7 +394,7 @@ imagekitio.list_files(
397
394
  limit: 5
398
395
  )
399
396
  ```
400
- **2. Get File Details**
397
+ **Get File Details**
401
398
  Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-details)
402
399
 
403
400
  ```ruby
@@ -406,7 +403,7 @@ imagekitio.get_file_details(
406
403
  )
407
404
  ```
408
405
 
409
- **3. Get File Metadata**
406
+ **Get File Metadata**
410
407
  Accepts the file ID and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-for-uploaded-media-files)
411
408
  ```ruby
412
409
  imagekit.get_file_metadata(
@@ -414,7 +411,7 @@ imagekit.get_file_metadata(
414
411
  )
415
412
  ```
416
413
 
417
- **3. Get File Metadata from remote url**
414
+ **Get File Metadata from remote url**
418
415
  Accepts the remote file url and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-from-remote-url)
419
416
 
420
417
  ```ruby
@@ -423,7 +420,7 @@ imagekit.get_remote_file_url_metadata(
423
420
  )
424
421
  ```
425
422
 
426
- **4. Update File Details**
423
+ **Update File Details**
427
424
  Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details).
428
425
  The first argument to the `update_field_details` method is the file ID, and a second argument is an object with the
429
426
  parameters to be updated.
@@ -436,18 +433,19 @@ imagekitio.update_file_details(
436
433
  )
437
434
  ```
438
435
 
439
- **5. Copy File**
436
+ **Copy File**
440
437
 
441
438
  Copy file from one path to another path using the source file path and the destination path as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-file)
442
439
 
443
440
  ```ruby
444
441
  imagekitio.copy_file(
445
442
  source_file_path: '/path/to/file.jpg',
446
- destination_path: '/folder/to/copy/into'
443
+ destination_path: '/folder/to/copy/into',
444
+ include_file_versions: true #default false
447
445
  )
448
446
  ```
449
447
 
450
- **6. Move File**
448
+ **Move File**
451
449
 
452
450
  Move file from one folder to another folder using the source file path and destination path as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-file)
453
451
 
@@ -458,7 +456,7 @@ imagekitio.move_file(
458
456
  )
459
457
  ```
460
458
 
461
- **7. Rename File**
459
+ **Rename File**
462
460
 
463
461
  Rename file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/rename-file)
464
462
 
@@ -470,7 +468,8 @@ imagekitio.rename_file(
470
468
  )
471
469
  ```
472
470
 
473
- **8. Delete file**
471
+ **Delete File**
472
+
474
473
  Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file). The method accepts the file ID of the file that has to be deleted.
475
474
 
476
475
  ```ruby
@@ -478,8 +477,50 @@ imagekitio.delete_file(
478
477
  file_id: '598821f949c0a938d57563bd'
479
478
  )
480
479
  ```
480
+ **File versions**
481
+
482
+ Get all file versions as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-versions). The method accepts the file ID of the file.
483
+
484
+ ```ruby
485
+ imagekitio.file_versions(
486
+ file_id: '598821f949c0a938d57563bd'
487
+ )
488
+ ```
489
+ **File version details**
490
+
491
+ Get all file version detail as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-version-details). The method accepts the file ID and version ID of the file.
492
+
493
+ ```ruby
494
+ imagekitio.file_version_detail(
495
+ file_id: '598821f949c0a938d57563bd',
496
+ version_id: '846321f949c0a938d57567ty'
497
+ )
498
+ ```
499
+
500
+ **Delete file version**
501
+
502
+ Delete file version as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file-version). The method accepts the file ID and version ID of the file.
503
+
504
+ ```ruby
505
+ imagekitio.delete_file_version(
506
+ file_id: '598821f949c0a938d57563bd',
507
+ version_id: '846321f949c0a938d57567ty'
508
+ )
509
+ ```
510
+
511
+ **Restore file version**
512
+
513
+ Restore deleted file version as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/restore-file-version). The method accepts the file ID and version ID of the file.
514
+
515
+ ```ruby
516
+ imagekitio.restore_file_version(
517
+ file_id: '598821f949c0a938d57563bd',
518
+ version_id: '846321f949c0a938d57567ty'
519
+ )
520
+ ```
521
+
522
+ **Bulk File Delete by IDs**
481
523
 
482
- **9. Bulk File Delete by IDs**
483
524
  Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-files-bulk). The method accepts a list of file IDs of files that has to be
484
525
  deleted.
485
526
 
@@ -489,7 +530,7 @@ imagekitio.delete_bulk_files(
489
530
  )
490
531
  ```
491
532
 
492
- **10. Purge Cache**
533
+ **Purge Cache**
493
534
  Programmatically issue a clear cache request as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
494
535
  Accepts the full URL of the file for which the cache has to be cleared.
495
536
 
@@ -498,7 +539,7 @@ imagekitio.purge_file_cache(
498
539
  file_url: 'https://ik.imagekit.io/demo/logo-white_SJwqB4Nfe.png'
499
540
  )
500
541
  ```
501
- **11. Purge Cache Status**
542
+ **Purge Cache Status**
502
543
 
503
544
  Get the purge cache request status using the request ID returned when a purge cache request gets submitted as per the
504
545
  [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
@@ -509,7 +550,7 @@ imagekitio.purge_file_cache_status(
509
550
  )
510
551
  ```
511
552
 
512
- **12. Add Bulk Tags**
553
+ **Add Bulk Tags**
513
554
 
514
555
  Add multiple tags on multiple files using an array of file ids and an array of tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/add-tags-bulk)
515
556
 
@@ -520,7 +561,7 @@ imagekitio.add_bulk_tags(
520
561
  )
521
562
  ```
522
563
 
523
- **13. Delete Bulk Tags**
564
+ **Delete Bulk Tags**
524
565
 
525
566
  Remove multiple tags from multiple files using an array of file ids and an array of tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/remove-tags-bulk)
526
567
 
@@ -531,7 +572,7 @@ imagekitio.delete_bulk_tags(
531
572
  )
532
573
  ```
533
574
 
534
- **14. Delete Bulk Ai Tags**
575
+ **Delete Bulk AI Tags**
535
576
 
536
577
  Delete bulk ai tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/remove-aitags-bulk)
537
578
 
@@ -542,7 +583,7 @@ imagekitio.delete_bulk_ai_tags(
542
583
  )
543
584
  ```
544
585
 
545
- **15. Create Folder**
586
+ **Create Folder**
546
587
 
547
588
  Create folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/create-folder)
548
589
 
@@ -554,18 +595,19 @@ imagekitio.create_folder(
554
595
  ```
555
596
 
556
597
 
557
- **16. Copy Folder**
598
+ **Copy Folder**
558
599
 
559
600
  Copy folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder)
560
601
 
561
602
  ```ruby
562
603
  imagekitio.copy_folder(
563
604
  source_folder_path: '/folder/to/copy',
564
- destination_path: '/folder/to/copy/into'
605
+ destination_path: '/folder/to/copy/into',
606
+ include_file_versions: true #default false
565
607
  )
566
608
  ```
567
609
 
568
- **17. Move Folder**
610
+ **Move Folder**
569
611
 
570
612
  Move folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder)
571
613
 
@@ -576,7 +618,7 @@ imagekitio.move_folder(
576
618
  )
577
619
  ```
578
620
 
579
- **18. Delete Folder**
621
+ **Delete Folder**
580
622
 
581
623
  Delete folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder)
582
624
 
@@ -586,7 +628,7 @@ imagekitio.delete_folder(
586
628
  )
587
629
  ```
588
630
 
589
- **19. Bulk Job Status**
631
+ **Bulk Job Status**
590
632
 
591
633
  Get the bulk job status as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status)
592
634
 
@@ -596,7 +638,7 @@ imagekitio.bulk_job_status(
596
638
  )
597
639
  ```
598
640
 
599
- **20. Create Custom Metadata Fields**
641
+ **Create a custom metadata field**
600
642
 
601
643
  Create custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/create-custom-metadata-field)
602
644
 
@@ -613,7 +655,7 @@ imagekitio.create_custom_metadata_field(
613
655
  )
614
656
  ```
615
657
 
616
- **21. Get Custom Metadata Fields**
658
+ **Get Custom Metadata Fields**
617
659
 
618
660
  Get the custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/get-custom-metadata-field)
619
661
 
@@ -623,7 +665,7 @@ imagekitio.get_custom_metadata_fields(
623
665
  )
624
666
  ```
625
667
 
626
- **22. Update Custom Metadata Fields**
668
+ **Update Custom Metadata Fields**
627
669
 
628
670
  Update custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/update-custom-metadata-field)
629
671
 
@@ -635,7 +677,7 @@ imagekitio.update_custom_metadata_field(
635
677
  )
636
678
  ```
637
679
 
638
- **23. Delete Custom Metadata Fields**
680
+ **Delete Custom Metadata Fields**
639
681
 
640
682
  Delete custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/delete-custom-metadata-field)
641
683
 
@@ -645,6 +687,38 @@ imagekitio.delete_custom_metadata_field(
645
687
  )
646
688
  ```
647
689
 
690
+ ## Access request-id, other response headers and HTTP status code
691
+
692
+ Each media management function returns a hash with `response`, `error`, `status_code`, `headers`, `raw_body` keys with respective values.
693
+
694
+ ```ruby
695
+ upload = imagekitio.upload_file(
696
+ file: file,
697
+ file_name: "default.jpg",
698
+ folder: '/test',
699
+ response_fields: 'tags,customCoordinates,isPrivateFile,metadata',
700
+ tags: %w[abc def],
701
+ use_unique_file_name: false,
702
+ is_private_file: true
703
+ )
704
+ puts upload[:status_code] # 200
705
+ puts upload[:headers]
706
+
707
+ # {
708
+ # "access-control-allow-origin"=>["*"],
709
+ # "x-ik-requestid"=>["6963194e-014f-8945-b05a-bdb0e088f1bd"],
710
+ # "content-type"=>["application/json; charset=utf-8"],
711
+ # "content-length"=>["611"],
712
+ # "etag"=>["W/\"859-GOeZiRFGOZERjHBgRUhG0EGcODs\""],
713
+ # "date"=>["Wed, 29 Jun 2022 07:04:33 GMT"],
714
+ # "x-request-id"=>["6963194e-014f-8945-b05a-bdb0e088f1bd"],
715
+ # "connection"=>["close"]
716
+ # }
717
+
718
+
719
+ puts upload[:raw_body]
720
+ # "{\"fileId\":\"62bjf980rb886bd691b86760\",\"name\":\"default.jpg\",\"size\":102117,\"versionInfo\":{\"id\":\"62bjf980rb886bd691b86760\",\"name\":\"Version 1\"},\"filePath\":\"/test/default.jpg\",\"url\":\"https://ik.imagekit.io/46865sdf6sdf/test/default.jpg\",\"fileType\":\"image\",\"height\":700,\"width\":1050,\"thumbnailUrl\":\"https://ik.imagekit.io/46865sdf6sdf/tr:n-ik_ml_thumbnail/test/default.jpg\",\"tags\":[\"abc\",\"def\"],\"AITags\":null,\"isPrivateFile\":true,\"customCoordinates\":null,\"metadata\":{\"height\":700,\"width\":1050,\"size\":102117,\"format\":\"jpg\",\"hasColorProfile\":true,\"quality\":0,\"density\":72,\"hasTransparency\":false,\"exif\":{},\"pHash\":\"90249d9b1fc74367\"}}"
721
+ ```
648
722
 
649
723
  ## Utility functions
650
724
 
@@ -3,7 +3,7 @@ require_relative './ik_file'
3
3
  if defined? Rails
4
4
  # Overwrite the ActiveStorage::Downloader's open method and remove the file integrity check constraint method verify_integrity_of
5
5
  class DownloaderExtension < ::ActiveStorage::Downloader
6
- def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil)
6
+ def open(key, checksum:, name: "ActiveStorage-", tmpdir: nil, **options)
7
7
  open_tempfile(name, tmpdir) do |file|
8
8
  download key, file
9
9
  # verify_integrity_of file, checksum: checksum
@@ -71,9 +71,11 @@ if defined? Rails
71
71
  def upload(key, io, checksum: nil, **options)
72
72
  instrument :upload, key: key, checksum: checksum do
73
73
  blob = storage_blob(key)
74
- response = client.upload_file(file: io, file_name: blob.filename.to_s)
74
+ response = client.upload_file(file: io, file_name: blob.filename.to_s, content_type: blob.content_type)
75
75
  if response[:error].nil?
76
76
  blob.update_columns(metadata: response[:response].transform_keys(&:to_sym))
77
+ else
78
+ raise Exception.new response[:error]
77
79
  end
78
80
  end
79
81
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require_relative '../constant'
3
3
  require_relative '../utils/option_validator'
4
+ require 'net/http/post/multipart'
4
5
 
5
6
  module ImageKitIo
6
7
  module ApiService
@@ -32,6 +33,7 @@ module ImageKitIo
32
33
  raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file
33
34
  raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name
34
35
 
36
+ content_type = options.delete(:content_type) || ''
35
37
  options = format_to_json(options, :extensions, Array)
36
38
  options = format_to_json(options, :custom_metadata, Hash)
37
39
  options = validate_upload_options(options || {})
@@ -39,7 +41,12 @@ module ImageKitIo
39
41
  raise ArgumentError, "Invalid Upload option"
40
42
  else
41
43
  headers = @req_obj.create_headers
42
- payload = {multipart: true, file: file, fileName: file_name}.merge(options)
44
+ payload = {
45
+ multipart: true,
46
+ file: file.is_a?(String) ? file : ::UploadIO.new(file, content_type, file_name),
47
+ fileName: file_name
48
+ }
49
+ payload.merge!(options)
43
50
  url = "#{constants.BASE_URL}#{constants.UPLOAD}"
44
51
  @req_obj.request("post", url, headers, payload)
45
52
  end
@@ -68,6 +75,51 @@ module ImageKitIo
68
75
  @req_obj.request("get", url, headers, formatted_options)
69
76
  end
70
77
 
78
+ def get_file_versions(file_id: nil)
79
+ if file_id == '' || file_id.nil?
80
+ raise ArgumentError, 'file_id is required'
81
+ end
82
+ url = "#{constants.BASE_URL}/#{file_id}/versions"
83
+ headers = @req_obj.create_headers
84
+ @req_obj.request('get', url, headers)
85
+ end
86
+
87
+ def get_file_version_detail(file_id: nil, version_id: nil)
88
+ if file_id == "" || file_id.nil?
89
+ raise ArgumentError, "file_id is required"
90
+ end
91
+ if version_id == "" || version_id.nil?
92
+ raise ArgumentError, "version_id is required"
93
+ end
94
+ url = "#{constants.BASE_URL}/#{file_id}/versions/#{version_id}"
95
+ headers = @req_obj.create_headers
96
+ @req_obj.request('get', url, headers)
97
+ end
98
+
99
+ def delete_file_version(file_id: nil, version_id: nil)
100
+ if file_id == "" || file_id.nil?
101
+ raise ArgumentError, "file_id is required"
102
+ end
103
+ if version_id == "" || version_id.nil?
104
+ raise ArgumentError, "version_id is required"
105
+ end
106
+ url = "#{constants.BASE_URL}/#{file_id}/versions/#{version_id}"
107
+ headers = @req_obj.create_headers
108
+ @req_obj.request('delete', url, headers)
109
+ end
110
+
111
+ def restore_file_version(file_id: nil, version_id: nil)
112
+ if file_id == "" || file_id.nil?
113
+ raise ArgumentError, "file_id is required"
114
+ end
115
+ if version_id == "" || version_id.nil?
116
+ raise ArgumentError, "version_id is required"
117
+ end
118
+ url = "#{constants.BASE_URL}/#{file_id}/versions/#{version_id}/restore"
119
+ headers = @req_obj.create_headers
120
+ @req_obj.request('put', url, headers)
121
+ end
122
+
71
123
  def details(file_identifier: nil)
72
124
  # Get detail of file by file_identifier
73
125
  if file_identifier == "" || file_identifier.nil?
@@ -131,12 +183,12 @@ module ImageKitIo
131
183
  @req_obj.request_stream('get', remote_file_url, headers: @req_obj.create_headers, &block)
132
184
  end
133
185
 
134
- def copy(source_file_path: nil, destination_path: nil)
186
+ def copy(source_file_path: nil, destination_path: nil, include_file_versions: false)
135
187
  if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil?
136
188
  raise ArgumentError, 'parameters required'
137
189
  end
138
190
  url = "#{constants.BASE_URL}/copy"
139
- payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path }
191
+ payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path, 'includeFileVersions': include_file_versions }.to_json
140
192
  @req_obj.request('post', url, @req_obj.create_headers, payload)
141
193
  end
142
194
 
@@ -157,6 +209,12 @@ module ImageKitIo
157
209
  payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter(options)).to_json
158
210
  @req_obj.request('put', url, @req_obj.create_headers, payload)
159
211
  end
212
+
213
+
214
+ private
215
+ def image_format?(type)
216
+ %(image/jpeg image/bmp image/apng image/avif image/gif image/ief image/svg+xml image/tiff image/x-icon image/rgb image/webp).include?(type)
217
+ end
160
218
  end
161
219
  end
162
220
  end
@@ -27,12 +27,12 @@ module ImageKitIo
27
27
  @req_obj.request('delete', url, @req_obj.create_headers, payload)
28
28
  end
29
29
 
30
- def copy(source_folder_path: nil, destination_path: nil)
30
+ def copy(source_folder_path: nil, destination_path: nil, include_file_versions: false)
31
31
  if source_folder_path == '' || source_folder_path.nil? || destination_path == '' || destination_path.nil?
32
32
  raise ArgumentError, 'Parameters required'
33
33
  end
34
34
  url = "#{constants.BULK_BASE_URL}/copyFolder"
35
- payload = { 'sourceFolderPath': source_folder_path, 'destinationPath': destination_path }
35
+ payload = { 'sourceFolderPath': source_folder_path, 'destinationPath': destination_path, 'includeFileVersions': include_file_versions }.to_json
36
36
  @req_obj.request('post', url, @req_obj.create_headers, payload)
37
37
  end
38
38
 
@@ -65,6 +65,22 @@ module ImageKitIo
65
65
  @file_service.list(**options)
66
66
  end
67
67
 
68
+ def file_versions(file_id: nil)
69
+ @file_service.get_file_versions(file_id: file_id)
70
+ end
71
+
72
+ def file_version_detail(file_id: nil, version_id: nil)
73
+ @file_service.get_file_version_detail(file_id: file_id, version_id: version_id)
74
+ end
75
+
76
+ def delete_file_version(file_id: nil, version_id: nil)
77
+ @file_service.delete_file_version(file_id: file_id, version_id: version_id)
78
+ end
79
+
80
+ def restore_file_version(file_id: nil, version_id: nil)
81
+ @file_service.restore_file_version(file_id: file_id, version_id: version_id)
82
+ end
83
+
68
84
  def get_file_details(file_id: nil)
69
85
  # Get file detail by file-id or file_url
70
86
  @file_service.details(file_identifier: file_id)
@@ -104,8 +120,8 @@ module ImageKitIo
104
120
  @file_service.stream_file(remote_file_url: file_url, &block)
105
121
  end
106
122
 
107
- def copy_file(source_file_path: nil, destination_path: nil)
108
- @file_service.copy(source_file_path: source_file_path, destination_path: destination_path)
123
+ def copy_file(source_file_path: nil, destination_path: nil, include_file_versions: false)
124
+ @file_service.copy(source_file_path: source_file_path, destination_path: destination_path, include_file_versions: include_file_versions)
109
125
  end
110
126
 
111
127
  def move_file(source_file_path: nil, destination_path: nil)
@@ -145,8 +161,8 @@ module ImageKitIo
145
161
  @folder_service.delete(folder_path: folder_path)
146
162
  end
147
163
 
148
- def copy_folder(source_folder_path: nil, destination_path: nil)
149
- @folder_service.copy(source_folder_path: source_folder_path, destination_path: destination_path)
164
+ def copy_folder(source_folder_path: nil, destination_path: nil, include_file_versions: false)
165
+ @folder_service.copy(source_folder_path: source_folder_path, destination_path: destination_path, include_file_versions: include_file_versions)
150
166
  end
151
167
 
152
168
  def move_folder(source_folder_path: nil, destination_path: nil)
@@ -5,7 +5,7 @@ module ImageKitIo
5
5
 
6
6
  VALID_FILE_DETAIL_OPTIONS = ["fileID"]
7
7
 
8
- VALID_UPLOAD_OPTIONS = %w[file file_name use_unique_file_name tags folder is_private_file custom_coordinates response_fields extensions webhook_url overwrite_file overwrite_AI_tags overwrite_custom_metadata custom_metadata mime overwrite_tags ]
8
+ VALID_UPLOAD_OPTIONS = %w[file file_name use_unique_file_name tags folder is_private_file custom_coordinates response_fields extensions webhook_url overwrite_file overwrite_AI_tags overwrite_custom_metadata custom_metadata mime overwrite_tags content_type ]
9
9
  end
10
10
  end
11
11
  end
@@ -3,6 +3,7 @@
3
3
  require "base64"
4
4
  require "rest-client"
5
5
  require "json"
6
+ require 'net/http/post/multipart'
6
7
  require_relative './constant'
7
8
  # Request requests and sends data from server
8
9
  module ImageKitIo
@@ -33,28 +34,46 @@ module ImageKitIo
33
34
  # request method communicates with server
34
35
  def request(method, url, headers = create_headers, payload = nil)
35
36
  headers ||= create_headers
36
- response = {response: nil, error: nil}
37
+ response = {}
37
38
  begin
38
- resp = RestClient::Request.new(method: method,
39
- url: url,
40
- headers: headers,
41
- payload: payload).execute
42
-
43
- if (resp.code >= 200) && (resp.code < 204)
44
- if (resp.headers[:content_type].include? "application/json")
39
+ if(method.downcase.to_sym == :post && payload.is_a?(Hash) && payload[:multipart])
40
+ uri = URI.parse(url)
41
+ http = Net::HTTP.new(uri.host, uri.port)
42
+ http.use_ssl = (uri.scheme == 'https')
43
+ req = Net::HTTP::Post::Multipart.new uri.path, payload, headers
44
+ resp = http.request(req)
45
+ response[:headers] = resp.to_hash
46
+ else
47
+ resp = RestClient::Request.new(method: method,
48
+ url: url,
49
+ headers: headers,
50
+ payload: payload).execute
51
+ response[:headers] = resp.raw_headers
52
+ end
53
+ response[:raw_body] = resp.body
54
+ response[:status_code] = resp.code
55
+ resp_c = resp.code.to_i
56
+ if [400, 403].include?(resp_c)
57
+ raise RestClient::ExceptionWithResponse, OpenStruct.new({ body: resp.body, code: resp_c, headers: response[:headers] })
58
+ end
59
+ if (resp.code.to_i >= 200) && (resp.code.to_i < 204)
60
+ content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type
61
+ if (content_type.include? "application/json")
45
62
  response[:response] = JSON.parse(resp.body.to_s)
46
63
  else
47
- raise =RestClient::ExceptionWithResponse
64
+ raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 404, body: resp.body)
48
65
  end
49
- elsif resp.code == 204
66
+ elsif resp.code.to_i == 204
50
67
  response[:response] = {'success': true}
51
68
  end
52
69
 
53
70
  rescue RestClient::ExceptionWithResponse => err
54
- response[:error] = if err.http_code == 404
71
+ response[:status_code] = err.http_code if response[:status_code].nil?
72
+ response[:headers] = err.http_headers if response[:headers].nil?
73
+ response[:error] = if err.http_code.to_i == 404
55
74
  {'message': err.response.to_s}
56
75
  else
57
- JSON.parse(err.response)
76
+ err.response.is_a?(OpenStruct) ? JSON.parse(err.response.body) : JSON.parse(err.response)
58
77
  end
59
78
  end
60
79
  response
@@ -1,5 +1,5 @@
1
1
  module ImageKitIo
2
2
  module Sdk
3
- VERSION = '2.0.1'
3
+ VERSION = '2.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imagekitio
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ImageKit.io team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-10 00:00:00.000000000 Z
11
+ date: 2022-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -78,6 +78,20 @@ dependencies:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: 5.2.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: multipart-post
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.1.0
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.1.0
81
95
  - !ruby/object:Gem::Dependency
82
96
  name: rails
83
97
  requirement: !ruby/object:Gem::Requirement
@@ -157,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
171
  - !ruby/object:Gem::Version
158
172
  version: '0'
159
173
  requirements: []
160
- rubygems_version: 3.2.32
174
+ rubygems_version: 3.3.7
161
175
  signing_key:
162
176
  specification_version: 4
163
177
  summary: Automate image optimization on rails platforms.