2015-02-20 07:13:48 -05:00
|
|
|
class UploadsController < ApplicationController
|
2015-04-16 08:03:37 -04:00
|
|
|
skip_before_action :authenticate_user!
|
|
|
|
before_action :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-10-09 13:07:29 -04:00
|
|
|
return render_404
|
2015-03-02 21:11:50 -05:00
|
|
|
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
|
2015-10-09 13:07:29 -04:00
|
|
|
return render_404
|
2015-03-10 09:50:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@model = upload_model.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_access!
|
2015-04-16 08:03:37 -04:00
|
|
|
authorized =
|
2015-03-10 09:50:42 -04:00
|
|
|
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
|
2015-10-09 13:07:29 -04:00
|
|
|
render_404
|
2015-03-10 09:50:42 -04:00
|
|
|
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 = {
|
2015-05-11 05:55:02 -04:00
|
|
|
"user" => User,
|
|
|
|
"project" => Project,
|
|
|
|
"note" => Note,
|
2016-02-22 14:49:16 -05:00
|
|
|
"group" => Group,
|
|
|
|
"appearance" => Appearance
|
2015-03-02 21:11:50 -05:00
|
|
|
}
|
|
|
|
|
2015-05-11 05:55:02 -04:00
|
|
|
upload_models[params[:model]]
|
2015-03-02 21:11:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_mount
|
2016-02-22 14:49:16 -05:00
|
|
|
upload_mounts = %w(avatar attachment file logo header_logo)
|
2015-03-02 21:11:50 -05:00
|
|
|
|
|
|
|
if upload_mounts.include?(params[:mounted_as])
|
|
|
|
params[:mounted_as]
|
|
|
|
end
|
|
|
|
end
|
2015-02-20 07:13:48 -05:00
|
|
|
end
|