Skip to content

Commit 0fb8d5c

Browse files
authored
Merge pull request #44 from bishosilwal/add-new-methods
Add file version methods
2 parents 7db588e + 02964d1 commit 0fb8d5c

File tree

10 files changed

+533
-104
lines changed

10 files changed

+533
-104
lines changed

README.md

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ Copy file from one path to another path using the source file path and the desti
443443
```ruby
444444
imagekitio.copy_file(
445445
source_file_path: '/path/to/file.jpg',
446-
destination_path: '/folder/to/copy/into'
446+
destination_path: '/folder/to/copy/into',
447+
include_file_versions: true #default false
447448
)
448449
```
449450

@@ -470,16 +471,59 @@ imagekitio.rename_file(
470471
)
471472
```
472473

473-
**8. Delete file**
474+
**8. Delete File**
475+
474476
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.
475477

476478
```ruby
477479
imagekitio.delete_file(
478480
file_id: '598821f949c0a938d57563bd'
479481
)
480482
```
483+
**9. File versions**
484+
485+
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.
486+
487+
```ruby
488+
imagekitio.file_versions(
489+
file_id: '598821f949c0a938d57563bd'
490+
)
491+
```
492+
**10. File version details**
493+
494+
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.
495+
496+
```ruby
497+
imagekitio.file_version_detail(
498+
file_id: '598821f949c0a938d57563bd',
499+
version_id: '846321f949c0a938d57567ty'
500+
)
501+
```
502+
503+
**11. Delete file version**
504+
505+
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.
506+
507+
```ruby
508+
imagekitio.delete_file_version(
509+
file_id: '598821f949c0a938d57563bd',
510+
version_id: '846321f949c0a938d57567ty'
511+
)
512+
```
513+
514+
**12. Restore file version**
515+
516+
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.
517+
518+
```ruby
519+
imagekitio.restore_file_version(
520+
file_id: '598821f949c0a938d57563bd',
521+
version_id: '846321f949c0a938d57567ty'
522+
)
523+
```
524+
525+
**13. Bulk File Delete by IDs**
481526

482-
**9. Bulk File Delete by IDs**
483527
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
484528
deleted.
485529

@@ -489,7 +533,7 @@ imagekitio.delete_bulk_files(
489533
)
490534
```
491535

492-
**10. Purge Cache**
536+
**14. Purge Cache**
493537
Programmatically issue a clear cache request as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache).
494538
Accepts the full URL of the file for which the cache has to be cleared.
495539

@@ -498,7 +542,7 @@ imagekitio.purge_file_cache(
498542
file_url: 'https://ik.imagekit.io/demo/logo-white_SJwqB4Nfe.png'
499543
)
500544
```
501-
**11. Purge Cache Status**
545+
**15. Purge Cache Status**
502546

503547
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as per the
504548
[API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
@@ -509,7 +553,7 @@ imagekitio.purge_file_cache_status(
509553
)
510554
```
511555

512-
**12. Add Bulk Tags**
556+
**16. Add Bulk Tags**
513557

514558
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)
515559

@@ -520,7 +564,7 @@ imagekitio.add_bulk_tags(
520564
)
521565
```
522566

523-
**13. Delete Bulk Tags**
567+
**17. Delete Bulk Tags**
524568

525569
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)
526570

@@ -531,7 +575,7 @@ imagekitio.delete_bulk_tags(
531575
)
532576
```
533577

534-
**14. Delete Bulk Ai Tags**
578+
**18. Delete Bulk Ai Tags**
535579

536580
Delete bulk ai tags as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/remove-aitags-bulk)
537581

@@ -542,7 +586,7 @@ imagekitio.delete_bulk_ai_tags(
542586
)
543587
```
544588

545-
**15. Create Folder**
589+
**19. Create Folder**
546590

547591
Create folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/create-folder)
548592

@@ -554,18 +598,19 @@ imagekitio.create_folder(
554598
```
555599

556600

557-
**16. Copy Folder**
601+
**20. Copy Folder**
558602

559603
Copy folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder)
560604

561605
```ruby
562606
imagekitio.copy_folder(
563607
source_folder_path: '/folder/to/copy',
564-
destination_path: '/folder/to/copy/into'
608+
destination_path: '/folder/to/copy/into',
609+
include_file_versions: true #default false
565610
)
566611
```
567612

568-
**17. Move Folder**
613+
**21. Move Folder**
569614

570615
Move folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder)
571616

@@ -576,7 +621,7 @@ imagekitio.move_folder(
576621
)
577622
```
578623

579-
**18. Delete Folder**
624+
**22. Delete Folder**
580625

581626
Delete folder as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder)
582627

@@ -586,7 +631,7 @@ imagekitio.delete_folder(
586631
)
587632
```
588633

589-
**19. Bulk Job Status**
634+
**23. Bulk Job Status**
590635

591636
Get the bulk job status as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status)
592637

@@ -596,7 +641,7 @@ imagekitio.bulk_job_status(
596641
)
597642
```
598643

599-
**20. Create Custom Metadata Fields**
644+
**24. Create Custom Metadata Fields**
600645

601646
Create custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/create-custom-metadata-field)
602647

@@ -613,7 +658,7 @@ imagekitio.create_custom_metadata_field(
613658
)
614659
```
615660

616-
**21. Get Custom Metadata Fields**
661+
**25. Get Custom Metadata Fields**
617662

618663
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)
619664

@@ -623,7 +668,7 @@ imagekitio.get_custom_metadata_fields(
623668
)
624669
```
625670

626-
**22. Update Custom Metadata Fields**
671+
**26. Update Custom Metadata Fields**
627672

628673
Update custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/update-custom-metadata-field)
629674

@@ -635,7 +680,7 @@ imagekitio.update_custom_metadata_field(
635680
)
636681
```
637682

638-
**23. Delete Custom Metadata Fields**
683+
**27. Delete Custom Metadata Fields**
639684

640685
Delete custom metadata fields as per the [API documentation here](https://docs.imagekit.io/api-reference/custom-metadata-fields-api/delete-custom-metadata-field)
641686

@@ -645,6 +690,36 @@ imagekitio.delete_custom_metadata_field(
645690
)
646691
```
647692

693+
## Access request-id, other response headers and HTTP status code
694+
695+
Each file management function returns a hash with response, error, status, headers, raw_body keys with respective values.
696+
697+
```ruby
698+
upload = imagekitio.upload_file(
699+
file: file,
700+
file_name: "default.jpg",
701+
folder: '/test',
702+
response_fields: 'tags,customCoordinates,isPrivateFile,metadata',
703+
tags: %w[abc def],
704+
use_unique_file_name: false,
705+
is_private_file: true
706+
)
707+
puts upload[:status] # 200
708+
puts upload[:headers]
709+
# {
710+
# "access-control-allow-origin"=>["*"],
711+
# "x-ik-requestid"=>["6963194e-014f-8945-b05a-bdb0e088f1bd"],
712+
# "content-type"=>["application/json; charset=utf-8"],
713+
# "content-length"=>["611"],
714+
# "etag"=>["W/\"859-GOeZiRFGOZERjHBgRUhG0EGcODs\""],
715+
# "date"=>["Wed, 29 Jun 2022 07:04:33 GMT"],
716+
# "x-request-id"=>["6963194e-014f-8945-b05a-bdb0e088f1bd"],
717+
# "connection"=>["close"]
718+
# }
719+
#
720+
puts upload[:raw_body]
721+
# "{\"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\"}}"
722+
```
648723

649724
## Utility functions
650725

lib/imagekitio/api_service/file.rb

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,51 @@ def list(**options)
7575
@req_obj.request("get", url, headers, formatted_options)
7676
end
7777

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+
78123
def details(file_identifier: nil)
79124
# Get detail of file by file_identifier
80125
if file_identifier == "" || file_identifier.nil?
@@ -138,12 +183,12 @@ def stream_file(remote_file_url: nil, &block)
138183
@req_obj.request_stream('get', remote_file_url, headers: @req_obj.create_headers, &block)
139184
end
140185

141-
def copy(source_file_path: nil, destination_path: nil)
186+
def copy(source_file_path: nil, destination_path: nil, include_file_versions: false)
142187
if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil?
143188
raise ArgumentError, 'parameters required'
144189
end
145190
url = "#{constants.BASE_URL}/copy"
146-
payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path }
191+
payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path, 'includeFileVersions': include_file_versions }.to_json
147192
@req_obj.request('post', url, @req_obj.create_headers, payload)
148193
end
149194

lib/imagekitio/api_service/folder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def delete(folder_path: nil)
2727
@req_obj.request('delete', url, @req_obj.create_headers, payload)
2828
end
2929

30-
def copy(source_folder_path: nil, destination_path: nil)
30+
def copy(source_folder_path: nil, destination_path: nil, include_file_versions: false)
3131
if source_folder_path == '' || source_folder_path.nil? || destination_path == '' || destination_path.nil?
3232
raise ArgumentError, 'Parameters required'
3333
end
3434
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
3636
@req_obj.request('post', url, @req_obj.create_headers, payload)
3737
end
3838

lib/imagekitio/client.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ def list_files(options = {})
6565
@file_service.list(**options)
6666
end
6767

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+
6884
def get_file_details(file_id: nil)
6985
# Get file detail by file-id or file_url
7086
@file_service.details(file_identifier: file_id)
@@ -104,8 +120,8 @@ def stream_file(file_url: nil, &block)
104120
@file_service.stream_file(remote_file_url: file_url, &block)
105121
end
106122

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)
109125
end
110126

111127
def move_file(source_file_path: nil, destination_path: nil)
@@ -145,8 +161,8 @@ def delete_folder(folder_path: nil)
145161
@folder_service.delete(folder_path: folder_path)
146162
end
147163

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)
150166
end
151167

152168
def move_folder(source_folder_path: nil, destination_path: nil)

lib/imagekitio/request.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,19 @@ def request(method, url, headers = create_headers, payload = nil)
4242
http.use_ssl = (uri.scheme == 'https')
4343
req = Net::HTTP::Post::Multipart.new uri.path, payload, headers
4444
resp = http.request(req)
45-
if resp.code.to_i == 400
46-
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 400, body: resp.body)
47-
end
45+
response[:headers] = resp.to_hash
4846
else
4947
resp = RestClient::Request.new(method: method,
5048
url: url,
5149
headers: headers,
5250
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] })
5358
end
5459
if (resp.code.to_i >= 200) && (resp.code.to_i < 204)
5560
content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type
@@ -63,6 +68,8 @@ def request(method, url, headers = create_headers, payload = nil)
6368
end
6469

6570
rescue RestClient::ExceptionWithResponse => err
71+
response[:status_code] = err.http_code if response[:status_code].nil?
72+
response[:headers] = err.http_headers if response[:headers].nil?
6673
response[:error] = if err.http_code.to_i == 404
6774
{'message': err.response.to_s}
6875
else

0 commit comments

Comments
 (0)