Today I would like to show you how to implement global preprocessing of uploaded files in Rails. Unfortunately, ActiveStorage does not make it possible to do this simply and explicitly, or I just did not find any information about it.
Sometimes we need to get an extension by content mime type. There are many ways to manage this but in Rails (or rack-based app) you can use this trick:
Rack::Mime::MIME_TYPES.invert['image/jpeg'] # => '.jpg'A few weeks ago I faced with a requirement to optimize a huge JSON response from Rails-based API.
For example, we want to respond large set of data as JSON collection. In our controller, we have some action for that, but rails Live API couldn't stream JSON or XML directly. But we can use a little trick.