DEV Community

Discussion on: Handling ActiveStorage direct uploads and server side form validations

Collapse
 
nameisvijay profile image
Vijay Meena • Edited

It won't stop creating orphaned file on cloud storage for some cases. For exmample, user has to upload their profile picture during registration to my website. If a user upload profile pic but doesn't submit form then it will still create orphaned file on storage.

I haven't used active storage yet but I used javascript and aws-sdk to direct-upload files few months back on rails4 app. I faced same problem and implemented same hack as you suggested. But, I modified design a bit. I created two buckets on my amazon s3 storage. First "cache" and second "production". My app direct-uploaded all the stuff to "cache" bucket and then my model checked required validation at form-submission and used before_save filter which copied image from "cache" bucket to "production" bucket using aws-sdk if validation succeeded.

I also set expiry time limit to "cache" file so all these temporary files were auto-purged. This way, all orphaned uploads were only saved to "cache" and actual records were saved to "production" and "cache" was auto-purged in 24 hours by s3.

I don't know if it can work with active-storage (because i heard that we can only use single bucket with active-storage) I also don't know if what i did was a proper way to do it but it worked for me.

Collapse
 
bkspurgeon profile image
Ben Koshy

This is the exact method employed by the Shrine gem - which is a masterpiece IMO. Takes away all the headaches of validations with the "cache" and "storage" sequestrations. However if you are using ActionText, and want the equivalent functionality using Shrine - then it looks like you're out of luck if you want something out of the box.