Skip to content

Commit

Permalink
Add example for multipage conversion parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulnsward committed Jun 30, 2024
1 parent 0af7bce commit eec05ae
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 25 deletions.
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,5 @@ end
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]

# Uploadcare-rails provides unified API interface to Uploadcare API
# gem 'uploadcare-rails', git: 'https://github.com/uploadcare/uploadcare-rails.git', branch: 'version_3.0.0'
# gem 'uploadcare-ruby', git: 'https://github.com/uploadcare/uploadcare-ruby.git', branch: 'version_4.0.0'

gem "uploadcare-rails", ">=3.4.3"
gem "uploadcare-rails", git: "https://github.com/uploadcare/uploadcare-rails.git", branch: "main"
gem "uploadcare-ruby", git: "https://github.com/uploadcare/uploadcare-ruby.git", branch: "main"
33 changes: 23 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
GIT
remote: https://github.com/uploadcare/uploadcare-rails.git
revision: eb273134e95d9bfafc8953b63db41639b934bbf1
branch: main
specs:
uploadcare-rails (3.4.3)
rails (>= 6)
uploadcare-ruby (>= 4.4.2)

GIT
remote: https://github.com/uploadcare/uploadcare-ruby.git
revision: e0db13dde90f41dfbd082875d8185bf9c5f8945a
branch: main
specs:
uploadcare-ruby (4.4.2)
mimemagic (~> 0.4)
parallel (~> 1.22)
retries (~> 0.0)
uploadcare-api_struct (>= 1.1, < 2)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -350,14 +370,6 @@ GEM
dry-monads (~> 1.6)
hashie (~> 5.0)
http (~> 5.1)
uploadcare-rails (3.4.3)
rails (>= 6)
uploadcare-ruby (>= 4.4.2)
uploadcare-ruby (4.4.2)
mimemagic (~> 0.4)
parallel (~> 1.22)
retries (~> 0.0)
uploadcare-api_struct (>= 1.1, < 2)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -405,12 +417,13 @@ DEPENDENCIES
spring
turbo-rails
tzinfo-data
uploadcare-rails (>= 3.4.3)
uploadcare-rails!
uploadcare-ruby!
web-console (>= 4.1.0)
webdrivers

RUBY VERSION
ruby 3.3.0p0

BUNDLED WITH
2.5.4
2.5.10
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
This example project demonstrates the uploadcare-rails capabilities.
The project is based on Ruby 3.3.0, Rails 7.1 and PostgreSQL.

---
**NOTE**
The project points to the `main` branch of the `uploadcare-rails` and `uploadcare-ruby` gems. If you want to use the latest release, please specify the version in the `Gemfile` file.

---

* [Installation](#installation)
* [Using docker](#using-docker)
* [Without docker](#without-docker)
Expand Down Expand Up @@ -252,15 +258,19 @@ Remember, to convert files, your account must have this feature enabled. It mean

#### Documents conversion

To convert a document, go to the `Convert document` section, choose a file to convert, target format and page (if the chosen target format is `jpg` or `png` and you want to convert a single page of a multi-paged document). There are two check-boxes. `Store` is responsible for storing files as mentioned above. And the `Throw error` option detects if the app should raise an error instead of rescuing this within a simple flash message.
To convert a document, go to the `Convert document` section, choose a file to convert, target format and page (if the chosen target format is `jpg` or `png` and you want to convert a single page of a multi-paged document). There are three check-boxes. `Store` is responsible for storing files as mentioned above. The `Save in group` option allows conversion of a multi-page document into a file group. And, the `Throw error` option detects if the app should raise an error instead of rescuing this within a simple flash message.

![Convert document](./references/convert-doc.png)
![Convert document](./references/convert-doc-new.png)

After the form is submitted, you'll see a `Conversion result page`, which shows some info about conversion: `Status`, `Error` and output file's UUID.
After the form is submitted, if you have not selected the `Save in group` option, you'll see a `Conversion result page`, which shows some info about conversion: `Status`, `Error` and output file's UUID.
Updating the page will refresh the status as said on the page.

![Convert document result](./references/convert-doc-result.png)

If you have selected the `Save in group` option, you'll be redirected to the `Document conversion formats info` page. Here you can see the group UUID for the converted format in the `Converted Groups` section. It may take some time to convert a document, so you can refresh the page to see the `Converted Groups`.

![Convert document group result](./references/convert-doc-group.png)

#### Video conversion

Video conversion works the same way but the form has some additional parameters to set. As the document form it has `File`, `Target format` and check-boxes — `Throw error` and `Store`. But you can also specify `quality`, `resize`, `cut` and `thumbs` options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ def new
end

def show
result = conversion_formats_from_file
@document_format = result.dig(:format, :name)
@document_conversion_formats = result.dig(:format, :conversion_formats).map { |format| format[:name] }
@result = conversion_formats_from_file
@document_format = @result.dig(:format, :name)
@document_conversion_formats = @result.dig(:format, :conversion_formats).map { |format| format[:name] }
@document_converted_groups = @result.dig(:format, :converted_groups)
end

private
Expand Down
16 changes: 12 additions & 4 deletions app/controllers/conversions/document_conversions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ def new

def create
result = request_conversion
flash[:notice] = "File conversion has been successfully started!"
redirect_to document_conversion_path(result: result.success)
if document_conversion_params[:save_in_group] == "1"
flash[:notice] = "File conversion has successfully started! The group shall be available in the Converted Groups section for selected format. Please refresh the page in some time to see the converted group."
redirect_to document_conversion_information_path(file: document_conversion_params[:file])
else
flash[:notice] = "File conversion has successfully started!"
redirect_to document_conversion_path(result: result.success)
end
rescue Uploadcare::Exception::ConversionError => e
raise e if throw_error?

Expand Down Expand Up @@ -39,12 +44,15 @@ def request_conversion
format: document_conversion_params[:target_format].presence,
page: document_conversion_params[:page].presence
}.compact,
store: document_conversion_params[:store]
{
store: document_conversion_params[:store],
save_in_group: document_conversion_params[:save_in_group]
}.compact
)
end

def document_conversion_params
params.permit(:file, :page, :target_format, :throw_error, :store)
params.permit(:file, :page, :target_format, :throw_error, :store, :save_in_group)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/file_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def file_group_params
end

def obtain_remote_files
@file_groups_data = Uploadcare::GroupApi.get_groups
@file_groups_data = Uploadcare::GroupApi.get_groups(ordering: "-datetime_created")
@file_groups = @file_groups_data[:results]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
<div class="mb-3">
<label class="mb-0" for="file">Current Document Format: <%= @document_format %></label>
</div>
<% if @document_converted_groups.present? %>
<div class="mb-3">
<label class="mb-0" for="file">Converted Groups</label>
<ul>
<% @document_converted_groups.each do |group_format, group_uuid| %>
<li><i><%= group_format %></i> - <%= link_to group_uuid, file_group_path(group_uuid) %></li>
<% end %>
</ul>
</div>
<% end %>
<%= hidden_field_tag(:file, params[:file]) %>
<div class="mb-3">
<label class="mb-0" for="file">Possible Conversion Formats</label>
Expand Down
5 changes: 5 additions & 0 deletions app/views/conversions/document_conversions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<%= number_field_tag 'page', nil, class: 'form-control' %>
</div>

<div class="mb-3">
<%= check_box_tag 'save_in_group', 1, false %>
<label class="mb-0" for="throw_error">Save in group</label>
</div>

<div class="mb-3">
<%= check_box_tag 'throw_error', true, false %>
<label class="mb-0" for="throw_error">Throw error</label>
Expand Down
Binary file added references/convert-doc-group.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added references/convert-doc-new.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eec05ae

Please sign in to comment.