Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Granular 'public' settings on uploaded files with Fog and Carrierwave

I am creating a rails app that lets an administrator upload photos that are optionally publicly displayed. For the upload / storage process I am using the Carrierwave gem along with the Fog gem and S3. The issue is that in order to get this all working, I have to make every file uploaded to the s3 bucket public. Is there a way to make files public / private on a file-by-file basis? Also, if this file-by-file granularity is possible, can it extend down to versions of images (created by automatic Carrierwave resizing)?

Currently, I have the following line in my carrierwave initializer:

  config.fog_public = true
like image 598
dcb Avatar asked Jul 18 '11 15:07

dcb


2 Answers

Actually, it's dead simple in Carrierwave.

You can do this:

class PrivateUploader < StandardUploader  

  @fog_public = false

Or (untested but should work perfectly) this:

class PrivateUploader < StandardUploader  


  def fog_public
    if local_condition
      true
    else
      false
    end
  end

:-)

I haven't tried DragonFly, but now that a couple of issues have been fixed in the last 2 months with Carrierwave, it's far superior to anything else I've seen. Insanely flexible.

//matt

like image 122
Matt Powell Avatar answered Nov 12 '22 22:11

Matt Powell


Just have to make your uploader class override the base class. I tore my hair out today too.. :( This worked for me:

Using Carrierwave 0.8.0 (in May 2013) /app/uploaders/whatever_uploader.rb

class WhateverUploader < CarrierWave::Uploader::Base
  def fog_public
    true # or false
  end
end
like image 5
pctj101 Avatar answered Nov 12 '22 22:11

pctj101