Store AJAX uploaded documents with timestamp prefix to avoid file name collision. Remove cached_attachments after document save.
This commit is contained in:
@@ -14,7 +14,7 @@ class DocumentsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
recover_attachment_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]
|
||||||
@@ -60,6 +60,7 @@ class DocumentsController < ApplicationController
|
|||||||
@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.save
|
@document.attachment.save
|
||||||
@document.cached_attachment = @document.attachment.path
|
@document.cached_attachment = @document.attachment.path
|
||||||
else
|
else
|
||||||
@@ -88,7 +89,7 @@ class DocumentsController < ApplicationController
|
|||||||
@document.user = current_user
|
@document.user = current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
def recover_attachment_from_cache
|
def recover_attachments_from_cache
|
||||||
if @document.attachment.blank? && @document.cached_attachment.present?
|
if @document.attachment.blank? && @document.cached_attachment.present?
|
||||||
@document.attachment = File.open(@document.cached_attachment)
|
@document.attachment = File.open(@document.cached_attachment)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,28 +20,36 @@ class Document < ActiveRecord::Base
|
|||||||
validates :documentable_id, presence: true, if: -> { persisted? }
|
validates :documentable_id, presence: true, if: -> { persisted? }
|
||||||
validates :documentable_type, presence: true, if: -> { persisted? }
|
validates :documentable_type, presence: true, if: -> { persisted? }
|
||||||
|
|
||||||
def validate_attachment_size
|
after_save :remove_cached_document, if: -> { valid? && persisted? && cached_attachment.present? }
|
||||||
if documentable.present? &&
|
|
||||||
attachment_file_size > documentable.class.max_file_size
|
|
||||||
errors[:attachment] = I18n.t("documents.errors.messages.in_between",
|
|
||||||
min: "0 Bytes",
|
|
||||||
max: "#{max_file_size(documentable)} MB")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_attachment_content_type
|
private
|
||||||
if documentable.present? &&
|
|
||||||
!accepted_content_types(documentable).include?(attachment_content_type)
|
|
||||||
errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type",
|
|
||||||
content_type: attachment_content_type,
|
|
||||||
accepted_content_types: humanized_accepted_content_types(documentable))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def attachment_presence
|
def validate_attachment_size
|
||||||
if attachment.blank? && cached_attachment.blank?
|
if documentable.present? &&
|
||||||
errors[:attachment] = I18n.t("errors.messages.blank")
|
attachment_file_size > documentable.class.max_file_size
|
||||||
|
errors[:attachment] = I18n.t("documents.errors.messages.in_between",
|
||||||
|
min: "0 Bytes",
|
||||||
|
max: "#{max_file_size(documentable)} MB")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_attachment_content_type
|
||||||
|
if documentable.present? &&
|
||||||
|
!accepted_content_types(documentable).include?(attachment_content_type)
|
||||||
|
errors[:attachment] = I18n.t("documents.errors.messages.wrong_content_type",
|
||||||
|
content_type: attachment_content_type,
|
||||||
|
accepted_content_types: humanized_accepted_content_types(documentable))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def attachment_presence
|
||||||
|
if attachment.blank? && cached_attachment.blank?
|
||||||
|
errors[:attachment] = I18n.t("errors.messages.blank")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_cached_document
|
||||||
|
File.delete(cached_attachment) if File.exists?(cached_attachment)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user