Skip to content

Commit e4bbb29

Browse files
committed
Add support for cdn_base,use_subdomains, and custom_cname
1 parent 8770f37 commit e4bbb29

File tree

11 files changed

+785
-44
lines changed

11 files changed

+785
-44
lines changed

CHANGELOG.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# Changelog
22

3+
## 4.4.4 — 2025-06-24
4+
### Added
5+
* **CDN Subdomain Support**: Added support for automatic subdomain generation to improve CDN performance and caching.
6+
- New `CnameGenerator` class for generating CNAME prefixes based on public key using SHA256 hashing
7+
- Configuration options:
8+
- `use_subdomains` - Enable automatic subdomain generation (default: `false`)
9+
- `cdn_base_postfix` - Base domain for subdomain generation (default: `https://ucarecd.net/`)
10+
- `default_cdn_base` - Original CDN base URL (default: `https://ucarecdn.com/`)
11+
- `cdn_base` - Dynamic CDN base selection based on subdomain configuration
12+
- New `cdn_url` method for `File` and `Group` entities to get CDN URLs using configured base
13+
- New `file_cdn_urls` method for `Group` entities to get CDN URLs of all files in a group without API requests
14+
* New `Uploadcare::Exception::ConfigurationError` for configuration-related errors
15+
316
## 4.4.3 — 2024-07-06
417

518
### Added
619
* Multi page conversion parameter (`save_in_group`) added to `DocumentConverter#convert` options.
720

8-
### Fixed
9-
* Fixed that signed URLs now work with ~ in the path. This also fixes signed URLs with grouped file URLs.
10-
1121
## 4.4.2 — 2024-05-29
1222

1323
### Fixed

README.md

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
Uploadcare Ruby integration handles uploads and further operations with files by
1515
wrapping Upload and REST APIs.
1616

17-
* [Installation](#installation)
18-
* [Usage](#usage)
19-
* [Uploading files](#uploading-files)
20-
* [Uploading and storing a single file](#uploading-and-storing-a-single-file)
21-
* [Multiple ways to upload files](#multiple-ways-to-upload-files)
22-
* [Uploading options](#uploading-options)
23-
* [File management](#file-management)
24-
* [File](#file)
25-
* [FileList](#filelist)
26-
* [Pagination](#pagination)
27-
* [Custom File Metadata](#custom-file-metadata)
28-
* [Group](#group)
29-
* [GroupList](#grouplist)
30-
* [Webhook](#webhook)
31-
* [Add-Ons](#add-ons)
32-
* [Project](#project)
33-
* [Conversion](#conversion)
34-
* [Useful links](#useful-links)
17+
- [Installation](#installation)
18+
- [Usage](#usage)
19+
- [Uploading files](#uploading-files)
20+
- [Uploading and storing a single file](#uploading-and-storing-a-single-file)
21+
- [Multiple ways to upload files](#multiple-ways-to-upload-files)
22+
- [Uploading options](#uploading-options)
23+
- [File management](#file-management)
24+
- [File](#file)
25+
- [FileList](#filelist)
26+
- [Pagination](#pagination)
27+
- [Custom File Metadata](#custom-file-metadata)
28+
- [Group](#group)
29+
- [GroupList](#grouplist)
30+
- [Webhook](#webhook)
31+
- [Add-Ons](#add-ons)
32+
- [Project](#project)
33+
- [Conversion](#conversion)
34+
- [Useful links](#useful-links)
3535

3636
## Requirements
37-
* ruby 3.0+
37+
38+
- ruby 3.0+
3839

3940
## Compatibility
4041

@@ -73,15 +74,18 @@ puts Uploadcare::File.info(uuid).inspect
7374
```
7475

7576
If you use `api_struct` gem in your project, replace it with `uploadcare-api_struct`:
77+
7678
```ruby
7779
gem 'uploadcare-api_struct'
7880
```
81+
7982
and run `bundle install`
8083

8184
If already not, create your project in [Uploadcare dashboard](https://app.uploadcare.com/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby) and copy
8285
its [API keys](https://app.uploadcare.com/projects/-/api-keys/) from there.
8386

8487
Set your Uploadcare keys in config file or through environment variables:
88+
8589
```bash
8690
export UPLOADCARE_PUBLIC_KEY=your_public_key
8791
export UPLOADCARE_SECRET_KEY=your_private_key
@@ -107,6 +111,7 @@ and [Upload](https://uploadcare.com/api-refs/upload-api/) and [REST](https://upl
107111
You can also find an example project [here](https://github.com/uploadcare/uploadcare-rails-example).
108112

109113
### Uploading files
114+
110115
#### Uploading and storing a single file
111116

112117
Using Uploadcare is simple, and here are the basics of handling files.
@@ -122,9 +127,17 @@ Using Uploadcare is simple, and here are the basics of handling files.
122127
# URL for the file, can be used with your website or app right away
123128
@uc_file.original_file_url
124129
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/your-file.png"
130+
131+
# CDN URL for the file
132+
@uc_file.cdn_url
133+
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/your-file.png"
134+
# With subdomains enabled:
135+
# Uploadcare.config.use_subdomains = true
136+
# => "https://a1b2c3d4e5.ucarecdn.net/a771f854-c2cb-408a-8c36-71af77811f3b/"
125137
```
126138

127139
The `store` option can have these possible values:
140+
128141
- `true`: mark the uploaded file as stored.
129142
- `false`: do not mark the uploaded file as stored and remove it after 24 hours.
130143
- `"auto"`: defers the choice of storage behavior to the [auto-store setting](https://app.uploadcare.com/projects/-/settings/#storage) for your Uploadcare project. This is the default behavior.
@@ -160,6 +173,7 @@ Uploadcare::Uploader.upload_files(files, store: 'auto')
160173

161174
Uploadcare::Uploader.upload_from_url("https://placekitten.com/96/139", store: "auto")
162175
```
176+
163177
It is possible to track progress of the upload-from-URL process. To do that, you should specify the `async` option and get a token:
164178

165179
```ruby
@@ -192,14 +206,18 @@ Uploadcare::Uploader.multipart_upload(file, store: true) do |options|
192206
puts "PROGRESS = #{progress}"
193207
end
194208
```
209+
195210
Output of the code above looks like:
211+
196212
```console
197213
PROGRESS = 4.545454545454546
198214
PROGRESS = 9.090909090909092
199215
PROGRESS = 13.636363636363637
200216
...
201217
```
218+
202219
Options available in a block:
220+
203221
- **:chunk_size** - size of each chunk in bytes;
204222
- **:object** - file object which is going to be uploaded;
205223
- **:offset** - offset from the beginning of a File object in bytes;
@@ -430,13 +448,15 @@ options = {
430448
```
431449

432450
To simply get all associated objects:
451+
433452
```ruby
434453
@list.all # => returns Array of Files
435454
```
436455

437456
#### Pagination
438457

439458
Initially, `FileList` is a paginated collection. It can be navigated using following methods:
459+
440460
```ruby
441461
@file_list = Uploadcare::FileList.file_list
442462
# Let's assume there are 250 files in cloud. By default, UC loads 100 files. To get next 100 files, do:
@@ -446,6 +466,7 @@ Initially, `FileList` is a paginated collection. It can be navigated using follo
446466
```
447467

448468
Alternatively, it's possible to iterate through full list of groups or files with `each`:
469+
449470
```ruby
450471
@list.each do |file|
451472
p file.url
@@ -493,9 +514,20 @@ Uploadcare::Group.rest_info(group.id)
493514
# group can be deleted by group ID.
494515
Uploadcare::Group.delete(group.id)
495516
# Note: This operation only removes the group object itself. All the files that were part of the group are left as is.
517+
518+
# Returns group's CDN URL
519+
@group.cdn_url
520+
# => "https://ucarecdn.com/group-id~2/"
521+
# With subdomains: "https://a1b2c3d4e5.ucarecdn.net/group-id~2/"
522+
523+
# Returns CDN URLs of all files from group without API requesting
524+
@group.file_cdn_urls
525+
# => 'https://ucarecdn.com/0513dda0-582f-447d-846f-096e5df9e2bb~2/nth/0/'
526+
# # With subdomains: 'https://a1b2c3d4e5.ucarecdn.net/0513dda0-582f-447d-846f-096e5df9e2bb~2/nth/0/'
496527
```
497528

498529
#### GroupList
530+
499531
`GroupList` is a list of `Group`
500532

501533
```ruby
@@ -507,6 +539,7 @@ Uploadcare::Group.delete(group.id)
507539
This is a paginated list, so [pagination](#Pagination) methods apply
508540

509541
#### Webhook
542+
510543
https://uploadcare.com/docs/api_reference/rest/webhooks/
511544

512545
You can use webhooks to provide notifications about your uploads to target urls.
@@ -663,6 +696,7 @@ Uploadcare::VideoConverter.convert(
663696
store: false
664697
)
665698
```
699+
666700
This method accepts options to set properties of an output file:
667701

668702
- **uuid** — the file UUID-identifier.
@@ -700,7 +734,9 @@ This method accepts options to set properties of an output file:
700734
:problems=>{}
701735
}
702736
```
737+
703738
Params in the response:
739+
704740
- **result** - info related to your transformed output(-s):
705741
- **original_source** - built path for a particular video with all the conversion operations and parameters.
706742
- **token** - a processing job token that can be used to get a [job status](https://uploadcare.com/docs/transformations/video-encoding/#status) (see below).
@@ -719,13 +755,13 @@ Uploadcare::VideoConverter.convert(
719755
)
720756
```
721757

722-
723758
To check a status of a video processing job you can simply use appropriate method of `Uploadcare::VideoConverter`:
724759

725760
```ruby
726761
token = 911933811
727762
Uploadcare::VideoConverter.status(token)
728763
```
764+
729765
`token` here is a processing job token, obtained in a response of a convert video request.
730766

731767
```ruby
@@ -741,12 +777,13 @@ Uploadcare::VideoConverter.status(token)
741777
```
742778

743779
Params in the response:
780+
744781
- **status** - processing job status, can have one of the following values:
745-
- *pending* — video file is being prepared for conversion.
746-
- *processing* — video file processing is in progress.
747-
- *finished* — the processing is finished.
748-
- *failed* — we failed to process the video, see error for details.
749-
- *canceled* — video processing was canceled.
782+
- _pending_ — video file is being prepared for conversion.
783+
- _processing_ — video file processing is in progress.
784+
- _finished_ — the processing is finished.
785+
- _failed_ — we failed to process the video, see error for details.
786+
- _canceled_ — video processing was canceled.
750787
- **error** - holds a processing error if we failed to handle your video.
751788
- **result** - repeats the contents of your processing output.
752789
- **thumbnails_group_uuid** - holds :uuid-thumb-group, a UUID of a file group with thumbnails for an output video, based on the thumbs operation parameters.
@@ -759,6 +796,7 @@ More examples and options can be found [here](https://uploadcare.com/docs/transf
759796
After each document file upload you obtain a file identifier in UUID format.
760797

761798
You can use file identifier to determine the document format and possible conversion formats.
799+
762800
```ruby
763801
Uploadcare::DocumentConverter.info("dc99200d-9bd6-4b43-bfa9-aa7bfaefca40")
764802

@@ -784,7 +822,9 @@ Uploadcare::DocumentConverter.convert(
784822
store: false
785823
)
786824
```
825+
787826
or create an image of a particular page (if using image format):
827+
788828
```ruby
789829
Uploadcare::DocumentConverter.convert(
790830
[
@@ -817,7 +857,9 @@ This method accepts options to set properties of an output file:
817857
:problems=>{}
818858
}
819859
```
860+
820861
Params in the response:
862+
821863
- **result** - info related to your transformed output(-s):
822864
- **original_source** - source file identifier including a target format, if present.
823865
- **token** - a processing job token that can be used to get a [job status](https://uploadcare.com/docs/transformations/document-conversion/#status) (see below).
@@ -841,6 +883,7 @@ To check a status of a document processing job you can simply use appropriate me
841883
token = 21120220
842884
Uploadcare::DocumentConverter.status(token)
843885
```
886+
844887
`token` here is a processing job token, obtained in a response of a convert document request.
845888

846889
```ruby
@@ -855,12 +898,13 @@ Uploadcare::DocumentConverter.status(token)
855898
```
856899

857900
Params in the response:
901+
858902
- **status** - processing job status, can have one of the following values:
859-
- *pending* — document file is being prepared for conversion.
860-
- *processing* — document file processing is in progress.
861-
- *finished* — the processing is finished.
862-
- *failed* — we failed to process the document, see error for details.
863-
- *canceled* — document processing was canceled.
903+
- _pending_ — document file is being prepared for conversion.
904+
- _processing_ — document file processing is in progress.
905+
- _finished_ — the processing is finished.
906+
- _failed_ — we failed to process the document, see error for details.
907+
- _canceled_ — document processing was canceled.
864908
- **error** - holds a processing error if we failed to handle your document.
865909
- **result** - repeats the contents of your processing output.
866910
- **uuid** - a UUID of your processed document file.
@@ -895,13 +939,14 @@ generator.generate_url("a7d5645e-5cd7-4046-819f-a6a2933bafe3")
895939
# This generates a URL that expires in 10 seconds
896940
# https://example.com/a7d5645e-5cd7-4046-819f-a6a2933bafe3/?token=exp=1714233277~acl=/a7d5645e-5cd7-4046-819f-a6a2933bafe3/~hmac=f25343104aeced3004d2cc4d49807d8d7c732300b54b154c319da5283a871a71
897941
```
942+
898943
## Useful links
899944

900-
* [Development](https://github.com/uploadcare/uploadcare-ruby/blob/main/DEVELOPMENT.md)
901-
* [Uploadcare documentation](https://uploadcare.com/docs/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
902-
* [Upload API reference](https://uploadcare.com/api-refs/upload-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
903-
* [REST API reference](https://uploadcare.com/api-refs/rest-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
904-
* [Changelog](./CHANGELOG.md)
905-
* [Contributing guide](https://github.com/uploadcare/.github/blob/master/CONTRIBUTING.md)
906-
* [Security policy](https://github.com/uploadcare/uploadcare-ruby/security/policy)
907-
* [Support](https://github.com/uploadcare/.github/blob/master/SUPPORT.md)
945+
- [Development](https://github.com/uploadcare/uploadcare-ruby/blob/main/DEVELOPMENT.md)
946+
- [Uploadcare documentation](https://uploadcare.com/docs/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
947+
- [Upload API reference](https://uploadcare.com/api-refs/upload-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
948+
- [REST API reference](https://uploadcare.com/api-refs/rest-api/?utm_source=github&utm_medium=referral&utm_campaign=uploadcare-ruby)
949+
- [Changelog](./CHANGELOG.md)
950+
- [Contributing guide](https://github.com/uploadcare/.github/blob/master/CONTRIBUTING.md)
951+
- [Security policy](https://github.com/uploadcare/uploadcare-ruby/security/policy)
952+
- [Support](https://github.com/uploadcare/.github/blob/master/SUPPORT.md)

lib/uploadcare.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'exception/request_error'
99
require 'exception/retry_error'
1010
require 'exception/auth_error'
11+
require 'exception/configuration_error'
1112

1213
# Entities
1314
require 'entity/entity'
@@ -29,6 +30,9 @@
2930
require 'signed_url_generators/akamai_generator'
3031
require 'signed_url_generators/base_generator'
3132

33+
# CNAME generator
34+
require 'cname_generator'
35+
3236
# Ruby wrapper for Uploadcare API
3337
#
3438
# @see https://uploadcare.com/docs/api_reference
@@ -51,4 +55,16 @@ module Uploadcare
5155
setting :framework_data, default: ''
5256
setting :file_chunk_size, default: 100
5357
setting :logger, default: Logger.new($stdout)
58+
setting :default_cdn_base, default: ENV.fetch('UPLOADCARE_DEFAULT_CDN_BASE', 'https://ucarecdn.com/')
59+
setting :cdn_base_postfix, default: ENV.fetch('UPLOADCARE_CDN_BASE', 'https://ucarecd.net/')
60+
# Enable automatic *.ucarecdn.net subdomains and CNAME generation
61+
setting :use_subdomains, default: false
62+
setting :custom_cname, default: -> { CnameGenerator.generate_cname } # CNAME domain
63+
setting :cdn_base, default: lambda {
64+
if config.use_subdomains && config.public_key
65+
CnameGenerator.cdn_base_postfix
66+
else
67+
config.default_cdn_base
68+
end
69+
}
5470
end

0 commit comments

Comments
 (0)