2017-05-22 6 views
-1

アップロードされたファイルを保護したいのですが、それらはpublic/uploadディレクトリに残っていて、アクセス可能であるため、ユーザは次のようにログインしていませんhttp://localhost:3000/uploads/video/1/test.mp4。私はそれを防ぐために、コンテンツを見るユーザーがログオンしていて、それを見る権限がある場合にのみビデオを見ることができます。レールでアップロードされたログインファイルによって保護されますcarreirwave 5

私のアプリケーションは、ユーザーがワークショップを持ち、これらのセッションのそれぞれにビデオがあります。ここで私は私のモデル現在のファイルがcarrierwaveのデフォルトルートに格納されている

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :recoverable, 
     :rememberable, :trackable, :validatable 

    belongs_to :workshop 

    mount_uploader :avatar, AvatarUploader 

    enum role: [:student, :teacher] 

end 

class Workshop < ApplicationRecord 
    has_many :sessions 
    has_many :users 

    validates :name, presence: true 
end 

class Session < ApplicationRecord 
    belongs_to :workshop 

    mount_uploader :video, WsvideoUploader 

    before_create :default_name 

    def default_name 
    self.video ||= File.basename(video.filename, '.*').titleize if video 
    end 

end 

**enter code here**の内容を残して、私は彼らがcarrierwaveから継承したクラスのコードを残します。

class WsvideoUploader < CarrierWave::Uploader::Base 

    # Choose what kind of storage to use for this uploader: 
    storage :file 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

end 

答えて

0

コントローラアクションは、before_filterを使用して認証されたユーザーによって呼び出されるように保護することができます。しかし、carrierwaveでアップロードされたファイルにアクセスするとコントローラのアクションは呼び出されないので、before_filterは使用できません。 あなたがしたいことをするには、アップロードされたファイルを公開されていないディレクトリに保存してから、コントローラーを使ってファイルにアクセスする必要があります。

あなたが工夫を使用しているので、あなたは、単により多くの詳細な説明はCarrierwave Wikiページに記載してありbefore_action :authenticate_user!

を追加することで、コントローラのアクションに認証を追加することができます。あなたはそれをここに見つけることができますsecure-upload-using-carrierwave

関連する問題