Use different path to store ajax uploaded cached attachments

This commit is contained in:
Senén Rodero Rodríguez
2017-08-31 12:37:41 +02:00
parent 6d66077a94
commit 5581445fff
3 changed files with 23 additions and 6 deletions

View File

@@ -115,7 +115,7 @@ module CommentableActions
def recover_documents_from_cache(resource) def recover_documents_from_cache(resource)
return false unless resource.try(:documents) return false unless resource.try(:documents)
resource.documents = resource.documents.each do |document| resource.documents = resource.documents.each do |document|
document.set_attachment_from_cache if document.cached_attachment.present? document.set_attachment_from_cached_attachment if document.cached_attachment.present?
end end
end end

View File

@@ -15,6 +15,7 @@ class DocumentsController < ApplicationController
def create def create
recover_attachments_from_cache recover_attachments_from_cache
if @document.save if @document.save
flash[:notice] = t "documents.actions.create.notice" flash[:notice] = t "documents.actions.create.notice"
redirect_to params[:from] redirect_to params[:from]
@@ -26,6 +27,7 @@ class DocumentsController < ApplicationController
def destroy def destroy
respond_to do |format| respond_to do |format|
format.html do format.html do
if @document.destroy if @document.destroy
flash[:notice] = t "documents.actions.destroy.notice" flash[:notice] = t "documents.actions.destroy.notice"
@@ -34,6 +36,7 @@ class DocumentsController < ApplicationController
end end
redirect_to params[:from] redirect_to params[:from]
end end
format.js do format.js do
if @document.destroy if @document.destroy
flash.now[:notice] = t "documents.actions.destroy.notice" flash.now[:notice] = t "documents.actions.destroy.notice"
@@ -41,12 +44,13 @@ class DocumentsController < ApplicationController
flash.now[:alert] = t "documents.actions.destroy.alert" flash.now[:alert] = t "documents.actions.destroy.alert"
end end
end end
end end
end end
def destroy_upload def destroy_upload
@document = Document.new(cached_attachment: params[:path]) @document = Document.new(cached_attachment: params[:path])
@document.set_attachment_from_cache @document.set_attachment_from_cached_attachment
@document.documentable = @documentable @document.documentable = @documentable
if @document.attachment.destroy if @document.attachment.destroy
@@ -60,6 +64,7 @@ class DocumentsController < ApplicationController
def upload def upload
@document = Document.new(document_params.merge(user: current_user)) @document = Document.new(document_params.merge(user: current_user))
@document.documentable = @documentable @document.documentable = @documentable
if @document.valid? if @document.valid?
@document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}" @document.attachment_file_name = "#{Time.now.to_i} - #{@document.attachment_file_name}"
@document.attachment.save @document.attachment.save
@@ -92,7 +97,7 @@ class DocumentsController < ApplicationController
def recover_attachments_from_cache def recover_attachments_from_cache
if @document.attachment.blank? && @document.cached_attachment.present? if @document.attachment.blank? && @document.cached_attachment.present?
@document.set_attachment_from_cache @document.set_attachment_from_cached_attachment
end end
end end

View File

@@ -1,7 +1,7 @@
class Document < ActiveRecord::Base class Document < ActiveRecord::Base
include DocumentsHelper include DocumentsHelper
include DocumentablesHelper include DocumentablesHelper
has_attached_file :attachment has_attached_file :attachment, path: ":rails_root/public/system/:class/:attachment/:prefix/:style/:filename"
attr_accessor :cached_attachment attr_accessor :cached_attachment
belongs_to :user belongs_to :user
@@ -28,7 +28,7 @@ class Document < ActiveRecord::Base
end end
end end
def set_attachment_from_cache def set_attachment_from_cached_attachment
self.attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem self.attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
File.open(cached_attachment) File.open(cached_attachment)
else else
@@ -36,6 +36,18 @@ class Document < ActiveRecord::Base
end end
end end
Paperclip.interpolates :prefix do |attachment, style|
attachment.instance.prefix(attachment, style)
end
def prefix(attachment, style)
if !attachment.instance.persisted?
"cached_attachments/user/#{attachment.instance.user_id}"
else
Paperclip::Interpolations.id_partition(attachment, style)
end
end
private private
def validate_attachment_size def validate_attachment_size