This is part two of our multipart series exploring Action Text, In part 1, we looked at basic of how we can get started with providing WYSIWYG support in our Apps using ActionText.
This blog post will deal with file attachments in Action Text.
Quick setup
We have discussed setup steps in Part 1. Here we will just list a set of commands to setup a new rails app and enable Action Text which one can execute so as to follow along this post.
Create a new rails app:
Switch to app directory:
Create database:
Enable support for action_text:
Add scaffold for post resource:
Update models and controller files for post:
Update the post form to edit content:
Update post show view to render content:
We can test the setup by opening the following URL in our browser.
Add/Uncomment image processing gem. We can skip this if we don’t want to resize images when displaying the rich text.
We can now perform CRUD on post.
Click on New post
or go to http://localhost:3000/posts/new
. We should see the Trix editor rendered.
Now let’s create a file app/javascript/trix-editor-overrides.js
and
import it in app/javascript/packs/application.js
as shown below.
Control which file can be attached in Trix editor
The default setup of Action Text allows attaching any kind of file. We can test it out. Try attaching a zip file to a new post. Due to business, performance or security reason, this may not be desirable behaviour. Let’s see how can we gain more control.
Trix event trix-file-accept
Trix fire events for every action that can be performed in trix. One of the event is trix-file-accept
.
This event is fired before a file gets attached in Trix.
If we call preventDefault()
on this event then the file attachment/upload would not happen
and
Trix would ignore the file.
Blacklist all file attachments:
Change the trix-editor-overrides.js
as shown below.
Reload the http://localhost:3000/posts/new
page and try attaching any file we should get an alert
and
the file wont get attached.
We can use a
File
object’s properties in combination with trix-file-accept
event to fine tune which files we want to upload
and
which we want to reject.
Let’s see few examples.
Only whitelist certain files based type:
Change the trix-editor-overrides.js
as shown below.
This will only allow upload of jpeg
and png
file types. Every other file type will be discarded with an alert.
Only whitelist certain files based size:
Change the trix-editor-overrides.js
as shown below.
This will prevent upload of any file which is more then 1MB in size.
If we reload and try attaching a file > 1MB, we will get alert but any file under 1MB will get attached properly.
Understanding _blob.html.erb
Located under app/views/active_storage/blobs/
,
this partial is auto created by Rails when were run the rails action_text:install
script.
This gets called for every attachment that is part of the Action Text content we try to render and is responsible for how a attachment is rendered.
In our blog app create a new post with 2 attachments and save the post.
Now, lets replace all the content of app/views/active_storage/blobs/_blob.html.erb
with the following
Reload the show page of the latest post.
A variable named blob
which is a
ActiveStorage::Blob
object is always passed to this partial.
This blob object is how we can access all the properties of the attached file.
For Example; if we replace the content of app/views/active_storage/blobs/_blob.html.erb
with the following
Reload the show page of the latest post.
Now let’s try and understand the default _blob.html.erb
that Rails generates.
Let’s break this code down to understand it further.
The above line checks if attachment blob is of type image or can be converted and previewed as image.
If yes, then it will fetch the image representation of specific size and display it.
By default the statement blob.representable?
will return true for images and false for any other media type.
This block is pretty straight forward. If we had specified caption for attachment during upload then it will display caption along with the attachment, else it will display the attachment filename and byte size.
Let’s see this code in action by trying to PDFs and Videos
Image preview for PDF.
According to Rails guide,
Extracting previews requires third-party applications, FFmpeg for video and muPDF for PDFs, and on macOS also XQuartz and Poppler. These libraries are not provided by Rails. You must install them yourself to use the built-in previewers. Before you install and use the third-party software, make sure you understand the licensing implications of doing so.
Let create a new post with PDF attachment without installing third-party support software.
Below is how Active Text content is rendered.
Now let’s install the software. For mac that would be Poppler. We are going to use Homebrew for that.
Restart the rails server after the 3rd part library is installed.
Let’s reload the show page for the above post. Now we should see a preview of the attachment.
We can add support for previewing all kinds of attachment with the help of Active Storage Previewers.
Read more about them here