2015-02-20 07:13:48 -05:00
|
|
|
class UploadsController < ApplicationController
|
2015-03-10 09:50:42 -04:00
|
|
|
skip_before_filter :authenticate_user!
|
|
|
|
before_filter :find_model, :authorize_access!
|
2015-02-23 22:35:42 -05:00
|
|
|
|
2015-02-20 07:13:48 -05:00
|
|
|
def show
|
2015-03-10 09:50:42 -04:00
|
|
|
uploader = @model.send(upload_mount)
|
2015-02-20 09:37:37 -05:00
|
|
|
|
2015-03-02 21:11:50 -05:00
|
|
|
unless uploader.file_storage?
|
|
|
|
return redirect_to uploader.url
|
|
|
|
end
|
|
|
|
|
2015-03-10 09:50:42 -04:00
|
|
|
unless uploader.file && uploader.file.exists?
|
2015-03-02 21:11:50 -05:00
|
|
|
return not_found!
|
|
|
|
end
|
2015-02-20 09:37:37 -05:00
|
|
|
|
|
|
|
disposition = uploader.image? ? 'inline' : 'attachment'
|
|
|
|
send_file uploader.file.path, disposition: disposition
|
2015-02-20 07:13:48 -05:00
|
|
|
end
|
2015-02-23 22:35:42 -05:00
|
|
|
|
2015-03-02 21:11:50 -05:00
|
|
|
private
|
|
|
|
|
2015-03-10 09:50:42 -04:00
|
|
|
def find_model
|
|
|
|
unless upload_model && upload_mount
|
|
|
|
return not_found!
|
|
|
|
end
|
|
|
|
|
|
|
|
@model = upload_model.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_access!
|
|
|
|
authorized =
|
|
|
|
case @model
|
|
|
|
when Project
|
|
|
|
can?(current_user, :read_project, @model)
|
|
|
|
when Group
|
|
|
|
can?(current_user, :read_group, @model)
|
|
|
|
when Note
|
|
|
|
can?(current_user, :read_project, @model.project)
|
|
|
|
else
|
|
|
|
# No authentication required for user avatars.
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
return if authorized
|
|
|
|
|
|
|
|
if current_user
|
|
|
|
not_found!
|
|
|
|
else
|
|
|
|
authenticate_user!
|
2015-02-23 22:35:42 -05:00
|
|
|
end
|
|
|
|
end
|
2015-03-02 21:11:50 -05:00
|
|
|
|
|
|
|
def upload_model
|
|
|
|
upload_models = {
|
|
|
|
user: User,
|
|
|
|
project: Project,
|
|
|
|
note: Note,
|
|
|
|
group: Group
|
|
|
|
}
|
|
|
|
|
|
|
|
upload_models[params[:model].to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_mount
|
|
|
|
upload_mounts = %w(avatar attachment file)
|
|
|
|
|
|
|
|
if upload_mounts.include?(params[:mounted_as])
|
|
|
|
params[:mounted_as]
|
|
|
|
end
|
|
|
|
end
|
2015-02-20 07:13:48 -05:00
|
|
|
end
|