0

我想使它成为我的carrierwave上传器在文件存储时对其进行加密,然后在检索时对其进行解密。使用Carrierwave加密/解密

我的第一个想法是重写CarrierWave :: Uploader :: Store :: store!和CarrierWave :: Uploader :: Store :: retrieve_from_store!方法,包括我的加密和解密代码,但我不太清楚如何做到这一点?

我正在计划使用Blowfish加密。

商店

def store!(new_file=nil) 
    # seems like I should process new_file here 
    cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?) 
    if @file and @cache_id 
    with_callbacks(:store, new_file) do 
     new_file = storage.store!(@file) 
     @file.delete if (delete_tmp_file_after_storage && ! move_to_store) 
     delete_cache_id 
     @file = new_file 
     @cache_id = nil 
    end 
    end 
end 

从商店

def retrieve_from_store!(identifier) 
    with_callbacks(:retrieve_from_store, identifier) do 
    res = storage.retrieve!(identifier) 
    @file = res #process res before I store it to file? 
    end 
end 

任何提示将不胜感激检索。

回答

0

你总是可以做这样的事情,其中​​文件实际上并没有存储在公共目录中。这将防止网络爬虫或有人猜测文件名,并从网上下载文件

def store_dir 
    "/path_to_rails_app/uploads/#{model.user_id}/#{model.id}" 
    end 

从这里,你可以使用一个download方法和授权工具来查看用户是否有权访问下载。任何关于这个的vunerabilities的想法?除了Mass Assignment或物理访问机器外,我认为这不是什么大问题。您还需要将cache_dir设置为公众以外的内容。

def cache_dir 
    "/path_to_rails_app/tmp/uploads/cache/#{model.user_id}/#{model.id}" 
    end 
+0

我喜欢这个想法,尽管我想用Fog存储我的文件,如S3或rackspace。 – Cyrus 2012-03-12 22:43:03

+0

如何使用Carrierwave Encrypter Decrypter Gem? – Matthias 2016-04-06 07:50:33

+0

@Matthias该宝石可能工作,但它似乎不再被维护了,因为它已近两年没有任何提交。 – kobaltz 2016-04-06 12:09:10