In order to remove metadata from PDF documents we will use the exiftool_vendored gem. The following line: Exiftool.new(attachment_path, "-overwrite_original -all:all=") Overwrites the original file with another file without metadata. So far this is the best solution we have found to perform this metadata deletion. When using Exiftool an exception is thrown, so we added a rescue to handle it. Here is a task created where this problem is discussed in issue 28 in the https://github.com/exiftool-rb/exiftool.rb/ repository. We'll wait to see if this will be fixed in future versions.
51 lines
1.2 KiB
Ruby
51 lines
1.2 KiB
Ruby
class Document < ApplicationRecord
|
|
include Attachable
|
|
|
|
belongs_to :user
|
|
belongs_to :documentable, polymorphic: true, touch: true
|
|
|
|
validates :title, presence: true
|
|
validates :user_id, presence: true
|
|
validates :documentable_id, presence: true, if: -> { persisted? }
|
|
validates :documentable_type, presence: true, if: -> { persisted? }
|
|
|
|
before_save :remove_metadata
|
|
|
|
scope :admin, -> { where(admin: true) }
|
|
|
|
def self.humanized_accepted_content_types
|
|
Setting.accepted_content_types_for("documents").join(", ")
|
|
end
|
|
|
|
def humanized_content_type
|
|
attachment_content_type.split("/").last.upcase
|
|
end
|
|
|
|
def max_file_size
|
|
documentable_class.max_file_size
|
|
end
|
|
|
|
def accepted_content_types
|
|
documentable_class.accepted_content_types
|
|
end
|
|
|
|
private
|
|
|
|
def association_name
|
|
:documentable
|
|
end
|
|
|
|
def documentable_class
|
|
association_class
|
|
end
|
|
|
|
def remove_metadata
|
|
return unless attachment.attached?
|
|
|
|
attachment_path = ActiveStorage::Blob.service.path_for(attachment.key)
|
|
Exiftool.new(attachment_path, "-all:all=")
|
|
rescue Exiftool::ExiftoolNotInstalled, Exiftool::NoSuchFile
|
|
nil
|
|
end
|
|
end
|