Active Storage: Lessons Learned

I just finished a project to migrate one of our projects from using Anaconda for file attachments to ActiveStorage. These are things I’ve learned, mostly for my own reference, but perhaps they can be of help to others.

Note: This is all in the context of Rails 5.2.0

Direct Uploads Can Be Fussy

We’re using direct-to-S3 uploads for our files, and there are still times where things just don’t work. Almost always everything is fine on a second try, but it feels like the error handling just isn’t all there yet. This is especially true for us on pages with multiple uploads.

AWS Checksums Don’t Always Work for Us

This is likely our own bug, or a bug in our systems, but we’ve run into a few instances now where we get 400 errors back from AWS. After contacting support, they’re all mismatched checksums on the data. We haven’t been able to reproduce this enough to figure out the cause.

Variants Aren’t Stored in the DB

When you’re working with variants, they aren’t stored in the DB. They’re checked for existance every time you try to reference them. This has performance implications.

ActiveStorage Is Not Currently Optimized For Things You Want To Cache

In order to get some decent caching of our files, we’ve had to build our own controller to load and return actual file contents of the variant from S3, with all the cache control headers included. Doing the file-contents-passthru in this way isn’t ideal, but right now ActiveStorage doesn’t support public acls or non-expiring url generation. There are some hacks to get around this, but I know better than to hack around a core piece of Rails. So, for now, we just have code that looks like this, which works wonderfully (but should be built-in functionality):

raise "Invalid Object/Variant combination" unless valid_variant?
find_variant

if @variant.nil?
  render plain: "", status: 404
  return
end

set_headers
send_data open(@variant.service_url).read, filename: "#{@variant.blob.filename}"

I’m confident these features will be improved in the future, but as of 5.2.0, this is still a pain.

I Think There’s Some Kind of Bug in Safari Related to XMLHTTPRequests And Rails ActiveStorage Forms

If you have a form with a file field in it, and that field is using direct upload, and you don’t choose a file but then submit the form, Safari sends a bad request. It seems that it fails to send all the data. Nginx reports client prematurely closed stream: only X out of Y bytes of request body received. It all works perfectly in Chrome every time. Maybe this is just a Safari (11.1) bug, but I have not been able to find a fix. For now, our forms all exist in our admin control panel, so we can work around this. I posted on Stack Overflow about this.

Read More →

Ulysses Journaling Workflow Script

I’ve been using Ulysses to journal for about 4 months now. Prior to that I was using Day One, but I’ve since attemped to move all of my writing into Ulysses.

For a while I was just copy/pasting a previous entry as a starting point for a new post. That was tedious, and I missed the meta data that Day One included in a post by default, primarily location and weather. So, I put together a Workflow script to create a new journal entry in Ulysses for me.

Here’s what it looks like on my iPad:

The version that includes weather requires an API key from Dark Sky. While free, not everyone is up for getting an API key just to create a journal entry, so I’ve created a separate workflow for sharing that doesn’t use weather data in the post template.

In order to use either workflow, you’ll need a top-level group named “Journal” in Ulysses.

You can grab either workflow here, if they’d be of use to you:

Ulysses Journal Template Workflow (With Weather)

Ulysses Journal Template Workflow (Without Weather)

Read More →

The Hornsbury Letters

A few months ago I stumbled across an old Royal Mercury typewriter at a local thrift store for $8. I cleaned it up, bought a new ribbon, and got inspired: I sat down in front of it, started typing, and this came out.

Read More →

Adventures in Buying a Telescope

Buying a telescope, returning it, and finding the right scope for me.

Read More →

Tick, Tock

A poem

Read More →