User Experience (UX)
Validating and Processing the Content of a File with Active Storage

Summarized using AI

Validating and Processing the Content of a File with Active Storage

Claudio Baccigalupo • January 31, 2020 • Birmingham, AL

The video presented by Claudio B at the Birmingham on Rails 2020 event focuses on validating and processing file content using Active Storage in Ruby on Rails. The talk is structured into three main parts, each outlining different functionalities of Active Storage.

Key Points Discussed:

- Introduction to Active Storage: Claudio introduces Active Storage as a library that simplifies file uploads and downloads in Rails applications.

- Basic File Uploads: The initial segment deals with the straightforward aspects of file uploads within a Rails application. Claudio demonstrates how to create a file upload interface, which includes:
- Implementing a file uploader in the HTML view.
- Configuring the controller to permit specific parameters.
- Using the has_one_attached method in the model to link a file to a record.

- File Storage and Retrieval: He explains how files can be stored either locally or on cloud services like Amazon S3, emphasizing how Active Storage handles the intricacies of file management.

- Validations for User Experience: Claudio discusses the importance of validations, such as ensuring a file is uploaded and controlling file size and type, to enhance user experience. He showcases simple code snippets for these validations.

- Analyzing Uploaded Files: The second part of the talk dives deeper into file processing. Claudio explains how Active Storage can automatically analyze files (e.g., extracting metadata from images) and how developers can implement custom analyzers for specific file types, such as iOS packages (IPAs). He provides a structured example of how to analyze an iOS package and store relevant metadata in the database.

- Generating a Manifest File: Claudio explains the necessity of generating an XML manifest file containing information about the uploaded iOS package for installation via devices, detailing the steps required to create and upload this file.

- Real-time Validations during Upload: The final section emphasizes how to extract metadata from uploaded packages, like the app version, to prevent users from making mistakes during submission. He provides techniques to validate uploaded files before processing, offering insights into improving user interactions.

Conclusions and Takeaways:

- Active Storage provides a powerful set of tools for handling file uploads and streamlining user experience in Rails applications.

- Implementing validations and analyzing file content can prevent user errors and enhance overall application functionality.

- Leveraging built-in Active Storage features can greatly reduce the need for external libraries, leading to more maintainable code.

- Continual updates to Rails and Active Storage require developers to stay informed about new tools and best practices to maximize their applications' capabilities.

Validating and Processing the Content of a File with Active Storage
Claudio Baccigalupo • January 31, 2020 • Birmingham, AL

Birmingham on Rails 2020 - Validating and Processing the Content of a File with Active Storage by Claudio B.

Birmingham on Rails 2020

00:00:43.140 This website is called speakerback.com, speakerback.com/forever. Just in case you need it.
00:01:11.240 This talk is about Rails, specifically in Birmingham on Rails, and it focuses on a library called Active Storage. This library simplifies the process of uploading and downloading files. As Brian mentioned at the beginning, you should all learn something if possible. If you haven't used Active Storage before, you're going to see how easy it is to use. If you have used Active Storage, I'm going to delve a bit deeper into some advanced features. And even if you're not interested in Active Storage, you might still learn something about iOS development.
00:01:39.570 This talk is structured in three parts, like every great talk or movie. I'll take a short break after each part and I was going to see if you wanted to ask questions, but logistically that might be difficult, so please hold your questions until after the talk.
00:02:02.960 The first part will cover the basics: how to use Active Storage to upload files through the browser. The second part is the core of this talk, where we go beyond just uploading and downloading files. We'll explore how to analyze these files and extract information from them. Finally, in the last part, I will discuss validation, including how we can create a better user experience by notifying users if they are uploading the wrong file. Let's get started.
00:03:12.410 For this talk, I'll use a real Rails application that is actually open source. It's hosted at GitHub: darkrum/cradle/MDMA, which stands for Mobile Deployment Management Agents.
00:03:31.420 Swift is built with Xcode to create an iOS app. At the end, we have this package, which is an IPA file. So, when you have this package, it can be distributed so that you can use these apps. It has various features, but I'm focusing on the main two components: first, the page where an iOS developer uploads a package using a file uploader; and second, where they specify which version of the iOS app it is.
00:04:14.720 Typically, a developer might go to the second page to see if there is a test version of the app available for download. This is just a basic example to demonstrate how you can upload and download files.
00:04:35.420 Let me break down these two pages for you. The first is the 'create' action in the Rails application. I created a model called 'Build,' which has two fields: the file itself, referred to as the package, and the version. When you submit the form, you'll see parameters for both the package and the version.
00:05:10.850 Now, if you've used file uploads with other libraries, you'll notice that Active Storage simplifies this process significantly. Let's look at how you can implement Active Storage for your file uploader.
00:05:35.180 First, in the view (HTML), you need to add the file uploader to display in the browser. You can add a file field for the package, which allows users to click 'Choose file.' Next, in the controller, you need to whitelist the parameters, ensuring that the package can be submitted. Finally, you add a single line of code to your model to specify that it has one attached package. This is done using the 'has_one_attached' method from Active Storage, enabling the use of file uploads.
00:07:12.849 When it comes to downloading the file, you'll want to create a link in your index view. You can use a helper method called 'url_for' in conjunction with the package object to create a download link.
00:07:56.080 You may wonder where the uploaded files are stored. This depends on your application's configuration. For instance, if you want to upload files to Amazon S3, you can specify this in your application's production environment configuration. Default options include Microsoft Azure, Google Cloud Storage, and local disk storage for testing purposes.
00:08:31.360 This basic structure is straightforward compared to other libraries. However, you might want to add some validations. For example, if a user forgets to attach a file, we should notify them that the package can't be blank. You can accomplish this with a simple validation check.
00:09:11.440 If again, you'd like to add more specific validations, such as ensuring that a file is not too large or has the correct content type, you can also follow a similar syntax to validate these criteria. There is an ongoing discussion about adding these validations to the Rails codebase.
00:09:33.960 Now, let's transition to the second part of my talk. Uploading and downloading files are relatively straightforward, but can we do more?
00:10:08.600 This is precisely what the functionality is intended to achieve: an iOS developer uploads a package, and a QA developer can open the website on an iPhone, click on the package, and directly install the app on the phone. This process eliminates the need to use the App Store for application distribution.
00:10:40.920 However, directly sharing an IPA file link might not work as intended. When opened on Safari on the phone, you'll be prompted to choose where to download the file. Instead, we want to create a direct install link, which involves additional processing.
00:11:30.510 In detail, you need to open the package and locate an XML file containing essential information about the iOS app, such as its name, version, and minimum iOS version. You then create a new file called a manifest containing this information, upload it, and link it to your primary file.
00:12:05.580 Active Storage can facilitate all these processes seamlessly. We're able to analyze the uploaded file and extract the necessary data.
00:12:48.700 Consider an example: if you upload a JPEG file using Active Storage, it can automatically extract the image's width and height using an Image Analyzer built into Active Storage. A similar function exists for videos, extracting the resolution and duration.
00:13:38.240 To create a custom analyzer for iOS packages, we follow a similar structure. After creating a build, we call our package analysis job. This job analyzes the package file and retrieves its details, which are then stored in the database.
00:14:48.300 We can extract information from the package files using the CFBundle APIs, which allow us to open an IPA file and extract various properties. Once we have that data, we can store it in our database for future access.
00:15:55.950 As we move forward refining our process, we will ultimately want to generate an XML file containing all the metadata extracted from our package. We create this XML using a temporary file, writing the XML declaration and interpolating the relevant metadata.
00:17:04.150 Once the XML file is ready, we can upload it using Active Storage's built-in capabilities. So not only do we attach the main package, but we also attach the manifest. This flexible approach allows us to generate and manage multiple related files effectively.
00:17:39.500 After all this processing, we replace the URL of the manifest file using the appropriate format that Apple provides. This step is crucial because it allows the application to be installed correctly on an iPhone without any hassle.
00:18:27.570 Now that we have everything in place, it's time to think about user experience once again. During the initial upload process, we need to extract the version number from the package to enhance the submit form.
00:19:22.120 The version number is critical for the correct functioning of the app download process. We will validate it before submission, so the user does not encounter any roadblocks during the installation.
00:19:54.210 We ensure that we're providing a smooth user experience this way, paving the way for a faster and easier installation process.
00:20:39.480 In the section on validations, we also make sure to account for what happens if a user forgets to attach a file. There should not be multiple errors displayed at once, so we'll handle those cases effectively.
00:21:17.930 Finally, it is essential to account for any errors during the analysis of the uploaded package. This feedback helps users know if something went wrong with their upload before further processing.
00:22:17.940 In conclusion, I've shared a lot about working with Active Storage and the various ways it can enhance your development process. There is much more to explore, including JavaScript components to facilitate file uploads from browsers directly to S3 without the overhead of spending time in Rails.
00:23:01.110 There's always something new evolving within Active Storage, so I encourage everyone to keep an eye on the latest updates and enhancements coming in future versions. I hope this talk was insightful and that you learned something valuable today.
00:23:58.280 Thank you, and I'm happy to answer any questions you might have.
Explore all talks recorded at Birmingham on Rails 2020
+1