Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add download link to spree admin #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/controllers/spree/admin/digitals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Spree
module Admin
class DigitalsController < ResourceController
include Downloadable

belongs_to "spree/product", :find_by => :slug

def create
Expand All @@ -16,6 +18,10 @@ def create
end
end

def show
send_file attachment.path, filename: attachment.original_filename, type: attachment.content_type, status: :ok and return
end

protected
def location_after_save
spree.admin_product_digitals_path(@product)
Expand All @@ -28,6 +34,16 @@ def permitted_resource_params
def permitted_digital_attributes
[:variant_id, :attachment]
end

private

def attachment
@attachment = Digital.find(params[:id]).attachment
end

def authorized?
true
end
end
end
end
end
38 changes: 10 additions & 28 deletions app/controllers/spree/digitals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
module Spree
class DigitalsController < Spree::StoreController
rescue_from ActiveRecord::RecordNotFound, with: :render_404

def show
if attachment.present?
# don't authorize the link unless the file exists
# the logger error will help track down customer issues easier
if attachment_is_file?
if digital_link.authorize!
if Paperclip::Attachment.default_options[:storage] == :s3
redirect_to attachment.expiring_url(Spree::DigitalConfiguration[:s3_expiration_seconds]) and return
else
send_file attachment.path, filename: attachment.original_filename, type: attachment.content_type, status: :ok and return
end
end
else
Rails.logger.error "Missing Digital Item: #{attachment.path}"
end
end
include Downloadable

render :unauthorized
end
rescue_from ActiveRecord::RecordNotFound, with: :render_404

private

def attachment_is_file?
if Paperclip::Attachment.default_options[:storage] == :s3
attachment.exists?
else
File.file?(attachment.path)
end
end

def digital_link
@link ||= DigitalLink.find_by!(secret: params[:secret])
end

def authorized?
digital_link.authorize!
end

def unauthorized_action
render :unauthorized
end

def attachment
@attachment ||= digital_link.digital.try(:attachment) if digital_link.present?
end
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/spree/downloadable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Spree
module Downloadable
def show
if attachment.present?
# don't authorize the link unless the file exists
# the logger error will help track down customer issues easier
if attachment_is_file?
if authorized?
if Paperclip::Attachment.default_options[:storage] == :s3
redirect_to attachment.expiring_url(Spree::DigitalConfiguration[:s3_expiration_seconds]) and return
else
send_file attachment.path, filename: attachment.original_filename, type: attachment.content_type, status: :ok and return
end
end
else
Rails.logger.error "Missing Digital Item: #{attachment.path}"
end
end

unauthorized_action
end

private

def unauthorized_action
end

def attachment_is_file?
if Paperclip::Attachment.default_options[:storage] == :s3
attachment.exists?
else
File.file?(attachment.path)
end
end
end
end
6 changes: 5 additions & 1 deletion app/views/spree/admin/digitals/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
<tbody>
<% variant.digitals.each do |digital| %>
<tr>
<td><%= render digital %></td>
<td>
<%= link_to admin_product_digital_path(@product, digital) do %>
<%= render digital %>
<% end %>
</td>
<td class="actions text-right">
<%#= link_to_with_icon 'delete', Spree.t(:delete_file, scope: 'digitals'), admin_product_digital_url(@product, digital), data: {confirm: Spree.t(:delete_file_confirmation, scope: 'digitals', filename: digital.attachment_file_name)}, method: :delete %>
<%= link_to_with_icon 'delete', Spree.t(:delete_file, scope: 'digitals'), admin_product_digital_url(@product, digital), data: {confirm: Spree.t(:delete_file_confirmation, scope: 'digitals', filename: digital.attachment_file_name)}, method: :delete, class: 'btn btn-danger btn-sm delete-resource' %>
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/admin/digitals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@
end
end

context '#show' do
let(:digital) { create(:digital) }
let!(:variant) { create(:variant, product: product, digitals: [digital]) }

it 'calls send_file' do
expect(controller).to receive(:send_file).with(digital.attachment.path,
filename: digital.attachment.original_filename,
type: digital.attachment.content_type, status: :ok){controller.head :ok,
content_type: digital.attachment.content_type, status: :ok }
spree_get :show, product_id: product.slug, id: digital.id
end
end

context '#destroy' do
let(:digital) { create(:digital) }
let!(:variant) { create(:variant, product: product, digitals: [digital]) }
Expand Down