jnicklas/carrierwave — Issue #545: Retrieve a file & cache it (process! called twice)
Author: elmatou
Created: 2011-11-30 10:56:50+00:00
State: open

Hi,
I'm building an online safe based on carrierwave and a personal cipher library.
My needs are quite simple, I want a user to upload a file, my Rails app encrypt it, then store it (using Fog) in any datacenter.
Then, when retrieving the file, my app needs to decrypt it from cache, and send it to the user.

My uploader :

````ruby
class EncryptorUploader < CarrierWave::Uploader::Base
storage :fog

before :store, :remember_cache_id
after :store, :delete_tmp_dir

process :encrypt

def encrypt
cache_stored_file! if !cached?
#.... Encryption logic altering the cached file in place
end

...
end
````

How I want to use it (here without activerecord for now):

```ruby
@uploader = EncryptorUploader.new(FakeClass.new, :fake)
@uploader.store!(File.open(__FILE__))
...
#... Years later, I need to get the file back
@uploader.retrieve_from_store!(@uploader.identifier)
@uploader.cache_stored_file!
#... Decryption logic altering the cached file in place
@uploader.read
```

But `process!` is called twice, once on `store!`, which is quite normal, but also on `cache_stored_file!` (which call `cache!` too)
Meaning that when I want to retrieve the file, it is double encrypted...

I'm not understanding, if I misuse Carrierwave, or if there is an issue in the `cache_stored_file!` feature/code.
BTW `cache_stored_file!` have no test, so I was unable to check what was the point of this method.

Anybody have any though on my case ?

I'm ready to help writing code and test case on this feature or in a new feature matching my needs.

PS : I checked for issue #380 & #422 but it seems to be a different case.