How to determine what phase/action the attachment is in?

For instance, let’s say I use the same storage location for both cache and store. How would I know what phase/action the attachment is in?

Could you clarify what’s your intention behind this? At which point do you need to know the action? The :action value should be available in places like generating location and extracting metadata, I’m not sure where you need it exactly.

Sure! Potentially a couple different areas. But for instance, in particular to know if a file is “processing” (i.e. not yet promoted to store) to show the user that it’s uploaded but processing, or for cleaning out files that never promoted properly, etc.

You can check the attached file directly to see to which storage it’s uploaded:

# without processing:
photo.image.storage_key.to_sym == :store # uploaded to permanent storage

# with processing versions:
photo.image.is_a?(Hash) # versions have been saved

Right, but let’s say I have multiple alternate storages, and let’s say a particular storage is used for cache and store in different instances. The key might be “s3_us_east_1” for cache and the same thing for store, so checking the key wouldn’t tell me what phase it’s in. I can get around this by having duplicates of the storage prefixed with the phase, for instance “store_s3_us_east_1” and “cache_s3_us_east_1” but that’s where having a separate phase/action state in the hash makes more sense, then storage location is independent from the state of the attachment, which seems more intuitive.

The key might be “s3_us_east_1” for cache and the same thing for store, so checking the key wouldn’t tell me what phase it’s in.

That would be the case if you’re using the same storage for temporary and permanent storage. This is not recommended, as you want to periodically clear temporary storage from unused files, which you cannot do if you store finalized attached files there as well.

I can get around this by having duplicates of the storage prefixed with the phase, for instance “store_s3_us_east_1” and “cache_s3_us_east_1”

Yes, this is what you should do.

but that’s where having a separate phase/action state in the hash makes more sense, then storage location is independent from the state of the attachment, which seems more intuitive.

I’m not sure I’m following. Storing the “phase/action” of the attachment flow into the database column would couple persistence with the attachment flow, which adds additional complexity, and I’m not clear on what would be the gain. Since you should use different temporary and permanent storage, all the necessary information should already be there.

Note that, if you want, you can always maintain a separate boolean column that tells you whether the processing is finished.

1 Like

Very helpful! I went with prefixing storages with store or cache to specify the difference. Thank you.