2025-07-18 17:35:57 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
require "digest/md5"
|
2007-03-12 17:59:02 +00:00
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class Attachment < ApplicationRecord
|
2024-11-19 13:00:20 +01:00
|
|
|
enum :status, {
|
2024-02-07 16:24:35 +01:00
|
|
|
uploaded: 0,
|
2024-02-07 10:50:25 +01:00
|
|
|
prepared: 1,
|
2024-02-07 16:24:35 +01:00
|
|
|
scanned: 2,
|
2024-02-18 21:12:33 +01:00
|
|
|
quarantined: 3,
|
|
|
|
|
rescan: 4
|
2024-11-19 13:00:20 +01:00
|
|
|
}, prefix: true
|
2024-02-07 10:50:25 +01:00
|
|
|
|
2014-11-03 21:10:39 +01:00
|
|
|
belongs_to :container, polymorphic: true
|
2014-11-03 21:35:22 +01:00
|
|
|
belongs_to :author, class_name: "User"
|
2010-08-05 19:05:04 +02:00
|
|
|
|
2024-02-07 10:50:25 +01:00
|
|
|
validates :author, :content_type, :filesize, :status, presence: true
|
2014-11-03 21:10:39 +01:00
|
|
|
validates :description, length: { maximum: 255 }
|
2007-08-29 16:52:35 +00:00
|
|
|
|
2018-05-24 17:16:55 +02:00
|
|
|
validate :filesize_below_allowed_maximum,
|
2021-04-28 08:51:43 +01:00
|
|
|
if: -> { !internal_container? }
|
|
|
|
|
validate :container_changed_more_than_once
|
2012-08-28 17:53:39 +02:00
|
|
|
|
2022-06-29 15:02:05 +02:00
|
|
|
has_paper_trail
|
|
|
|
|
|
2021-07-20 16:05:27 +02:00
|
|
|
# Those columns are currently not displayed in the application and are rarely used
|
|
|
|
|
# at all.
|
|
|
|
|
# Their purpose currently is limited to full text search where the results are not highlighted.
|
|
|
|
|
# As the columns can contain a lot of text (with the exception of file_tsv) and having them included
|
|
|
|
|
# leads to them being loaded when attachments are fetched, including the columns leads to a heavily
|
|
|
|
|
# increased loading time
|
|
|
|
|
# From a production database:
|
|
|
|
|
# SELECT "attachments"."id", "attachments"."fulltext" ...
|
|
|
|
|
# => 2650 ms
|
|
|
|
|
# SELECT "attachments"."id" ...
|
|
|
|
|
# => 1 ms
|
|
|
|
|
self.ignored_columns = %w(fulltext fulltext_tsv file_tsv)
|
|
|
|
|
|
2013-11-27 17:18:02 +01:00
|
|
|
acts_as_journalized
|
2014-12-15 19:01:54 +01:00
|
|
|
acts_as_event title: -> { file.name },
|
2013-11-28 15:33:24 +01:00
|
|
|
url: (Proc.new do |o|
|
2021-07-14 14:43:19 +02:00
|
|
|
{ controller: "/attachments", action: "download", id: o.id, filename: o.filename }
|
|
|
|
|
end)
|
2008-07-27 17:54:09 +00:00
|
|
|
|
2014-12-15 19:01:54 +01:00
|
|
|
mount_uploader :file, OpenProject::Configuration.file_uploader
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2024-02-07 17:17:59 +01:00
|
|
|
after_commit :enqueue_jobs, on: :create, if: -> { !internal_container? }
|
2017-12-04 12:35:51 +01:00
|
|
|
|
2024-02-19 09:51:46 +01:00
|
|
|
scope :pending_direct_upload, -> { status_prepared }
|
|
|
|
|
scope :not_pending_direct_upload, -> { not_status_prepared }
|
2021-11-11 19:50:59 +00:00
|
|
|
|
2015-12-01 17:08:06 +00:00
|
|
|
##
|
|
|
|
|
# Returns an URL if the attachment is stored in an external (fog) attachment storage
|
|
|
|
|
# or nil otherwise.
|
2019-10-18 11:57:03 +01:00
|
|
|
def external_url(expires_in: nil)
|
2021-11-11 09:40:05 +01:00
|
|
|
url = URI.parse file.download_url(external_url_options(expires_in:)) # returns a path if local
|
2015-12-01 17:08:06 +00:00
|
|
|
|
|
|
|
|
url if url.host
|
2016-09-21 16:06:06 +01:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
|
nil
|
2015-12-01 17:08:06 +00:00
|
|
|
end
|
|
|
|
|
|
2021-11-11 09:31:40 +00:00
|
|
|
##
|
|
|
|
|
# Do not include the filename in the content disposition as this may break for Unicode file names
|
|
|
|
|
# specifically when using S3 for attachments. In the case of S3 the file name for the downloaded
|
|
|
|
|
# file will still be correct as it's part of the URL before the query.
|
2021-11-10 15:54:25 +00:00
|
|
|
def external_url_options(expires_in: nil)
|
2026-05-29 10:25:20 +02:00
|
|
|
{ content_disposition: content_disposition(include_filename: false),
|
|
|
|
|
content_type: served_content_type,
|
|
|
|
|
expires_in: }
|
2021-11-10 15:54:25 +00:00
|
|
|
end
|
|
|
|
|
|
2015-12-01 17:08:06 +00:00
|
|
|
def external_storage?
|
|
|
|
|
!external_url.nil?
|
|
|
|
|
end
|
|
|
|
|
|
2007-03-12 17:59:02 +00:00
|
|
|
def increment_download
|
|
|
|
|
increment!(:downloads)
|
|
|
|
|
end
|
2007-05-26 15:42:37 +00:00
|
|
|
|
|
|
|
|
def project
|
2014-11-03 21:35:22 +01:00
|
|
|
# not every container has a project (example: LandingPage)
|
|
|
|
|
container.respond_to?(:project) ? container.project : nil
|
2007-05-26 15:42:37 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2021-11-10 15:54:25 +00:00
|
|
|
def content_disposition(include_filename: true)
|
2021-11-11 09:24:42 +01:00
|
|
|
disposition = inlineable? ? "inline" : "attachment"
|
2021-11-10 15:54:25 +00:00
|
|
|
|
|
|
|
|
if include_filename
|
2021-11-11 09:24:42 +01:00
|
|
|
"#{disposition}; filename=#{filename}"
|
2021-11-10 15:54:25 +00:00
|
|
|
else
|
2021-11-11 09:24:42 +01:00
|
|
|
disposition
|
2021-11-10 15:54:25 +00:00
|
|
|
end
|
2014-04-24 17:44:40 +02:00
|
|
|
end
|
|
|
|
|
|
2026-05-29 10:25:20 +02:00
|
|
|
# Returns the Content-Type to use when serving this file inline in a browser.
|
|
|
|
|
# Text files are normalised to text/plain (prevents script execution) with an
|
|
|
|
|
# explicit charset. Non-inlineable files get application/octet-stream so the
|
|
|
|
|
# browser is forced to download them.
|
|
|
|
|
def served_content_type
|
|
|
|
|
if is_text?
|
|
|
|
|
"text/plain; charset=#{charset.presence || Setting.attachment_default_charset}"
|
|
|
|
|
elsif inlineable?
|
|
|
|
|
content_type
|
|
|
|
|
else
|
|
|
|
|
"application/octet-stream"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-05-28 13:23:22 +02:00
|
|
|
# Returns the content type to use when serving the file to a browser.
|
|
|
|
|
# For text files, ensures a charset is always present so browsers don't
|
2026-05-29 10:25:20 +02:00
|
|
|
# fall back to ISO-8859-1. Preserves the real MIME subtype (e.g. text/x-ruby)
|
|
|
|
|
# unlike served_content_type which normalises to text/plain for security.
|
2026-05-28 13:23:22 +02:00
|
|
|
def serving_content_type
|
|
|
|
|
return content_type unless is_text?
|
|
|
|
|
|
|
|
|
|
"#{content_type}; charset=#{charset.presence || Setting.attachment_default_charset}"
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
def visible?(user = User.current)
|
2018-05-24 17:16:55 +02:00
|
|
|
allowed_or_author?(user) do
|
|
|
|
|
container.attachments_visible?(user)
|
|
|
|
|
end
|
2008-12-09 16:54:46 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2014-11-03 21:35:22 +01:00
|
|
|
def deletable?(user = User.current)
|
2018-05-24 17:16:55 +02:00
|
|
|
allowed_or_author?(user) do
|
|
|
|
|
container.attachments_deletable?(user)
|
|
|
|
|
end
|
2008-12-09 16:54:46 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2021-11-04 09:20:42 +01:00
|
|
|
def prepared?
|
2024-02-07 10:50:25 +01:00
|
|
|
status_prepared?
|
2021-11-04 09:20:42 +01:00
|
|
|
end
|
|
|
|
|
|
2024-02-07 16:24:35 +01:00
|
|
|
def pending_virus_scan?
|
2024-02-14 16:14:30 +01:00
|
|
|
status_uploaded? && Setting::VirusScanning.enabled?
|
2024-02-07 16:24:35 +01:00
|
|
|
end
|
|
|
|
|
|
2024-05-22 09:21:40 +02:00
|
|
|
# Determine mime types that we deem safe for inline content disposition
|
|
|
|
|
# e.g., which will be loaded by the browser without forcing to download them
|
2014-04-24 17:44:40 +02:00
|
|
|
def inlineable?
|
2024-05-22 09:21:40 +02:00
|
|
|
is_text? || is_image? || is_movie? || is_pdf?
|
2019-07-11 08:18:49 +01:00
|
|
|
end
|
|
|
|
|
|
2021-11-04 17:22:37 +01:00
|
|
|
# rubocop:disable Naming/PredicateName
|
2019-07-11 08:18:49 +01:00
|
|
|
def is_plain_text?
|
2021-11-04 17:22:37 +01:00
|
|
|
OpenProject::MimeType.plain_text?(content_type)
|
2014-04-24 17:44:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_image?
|
2021-11-04 17:22:37 +01:00
|
|
|
OpenProject::MimeType.image?(content_type)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_movie?
|
|
|
|
|
OpenProject::MimeType.movie?(content_type)
|
2014-04-24 17:44:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# backwards compatibility for plugins
|
|
|
|
|
alias :image? :is_image?
|
|
|
|
|
|
|
|
|
|
def is_pdf?
|
|
|
|
|
content_type == "application/pdf"
|
2007-08-15 15:36:15 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2024-11-21 07:55:23 +01:00
|
|
|
def is_html?
|
|
|
|
|
content_type == "text/html"
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-09 18:40:59 +00:00
|
|
|
def is_text?
|
2024-11-21 07:55:23 +01:00
|
|
|
content_type.match?(/\Atext\/.+/) && !is_html?
|
2008-06-09 18:40:59 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2008-06-08 18:26:39 +00:00
|
|
|
def is_diff?
|
2014-11-03 21:35:22 +01:00
|
|
|
is_text? && filename =~ /\.(patch|diff)\z/i
|
2008-06-08 18:26:39 +00:00
|
|
|
end
|
2021-11-04 17:22:37 +01:00
|
|
|
# rubocop:enable Naming/PredicateName
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-04-25 09:31:36 +00:00
|
|
|
# Returns true if the file is readable
|
2015-12-01 17:06:38 +00:00
|
|
|
delegate :readable?, to: :file
|
2014-04-24 17:44:40 +02:00
|
|
|
|
2018-05-31 08:23:57 +02:00
|
|
|
def containered?
|
|
|
|
|
container.present?
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-02 16:49:42 +02:00
|
|
|
##
|
|
|
|
|
# Retrieve a local file,
|
|
|
|
|
# this may result in downloading the file first
|
2014-12-15 19:01:54 +01:00
|
|
|
def diskfile
|
|
|
|
|
file.local_file
|
2014-04-24 17:44:40 +02:00
|
|
|
end
|
|
|
|
|
|
2019-04-02 16:49:42 +02:00
|
|
|
##
|
|
|
|
|
# Retrieve the local file path,
|
|
|
|
|
# this may result in downloading the file first to a tmpdir
|
|
|
|
|
def local_path
|
|
|
|
|
diskfile.path
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-15 19:01:54 +01:00
|
|
|
def filename
|
2021-07-14 14:43:19 +02:00
|
|
|
attributes["file"] || super
|
2014-12-15 19:01:54 +01:00
|
|
|
end
|
2013-08-13 15:13:06 +02:00
|
|
|
|
2020-03-24 09:00:59 +01:00
|
|
|
##
|
|
|
|
|
# Returns the file extension name,
|
|
|
|
|
# if any (with leading dot)
|
|
|
|
|
def extension
|
|
|
|
|
File.extname filename
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-15 19:01:54 +01:00
|
|
|
def file=(file)
|
|
|
|
|
super.tap do
|
2019-04-02 16:06:12 +02:00
|
|
|
set_file_size file
|
2019-04-02 16:49:42 +02:00
|
|
|
|
|
|
|
|
set_content_type file
|
|
|
|
|
|
|
|
|
|
if File.readable? file.path
|
|
|
|
|
set_digest file
|
|
|
|
|
end
|
2014-12-15 19:01:54 +01:00
|
|
|
end
|
|
|
|
|
end
|
2007-03-12 17:59:02 +00:00
|
|
|
|
2014-12-15 19:01:54 +01:00
|
|
|
def set_file_size(file)
|
|
|
|
|
self.filesize = file.size
|
2007-03-12 17:59:02 +00:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2014-12-15 19:01:54 +01:00
|
|
|
def set_content_type(file)
|
2026-05-28 13:23:22 +02:00
|
|
|
self.content_type, self.charset = OpenProject::ContentTypeDetector.new(file.path).detect_with_charset
|
2014-12-15 19:01:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_digest(file)
|
|
|
|
|
self.digest = Digest::MD5.file(file.path).hexdigest
|
|
|
|
|
end
|
|
|
|
|
|
2026-01-08 11:13:44 +01:00
|
|
|
##
|
|
|
|
|
# Detects the content type of a file based on its actual content.
|
|
|
|
|
# This method always relies on file content detection (via the `file` command)
|
|
|
|
|
# and never uses filename-based narrowing (MimeType.narrow_type) to ensure
|
|
|
|
|
# security-sensitive types like SVG are correctly identified even when the
|
|
|
|
|
# filename extension doesn't match the actual content.
|
|
|
|
|
#
|
|
|
|
|
# @param file_path [String] Path to the file to analyze
|
|
|
|
|
# @param fallback [String] Default content type if detection fails
|
|
|
|
|
# @return [String] The detected content type
|
2014-12-15 19:01:54 +01:00
|
|
|
def self.content_type_for(file_path, fallback = OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT)
|
2026-01-08 11:13:44 +01:00
|
|
|
# Always use ContentTypeDetector which analyzes file content, not filename
|
|
|
|
|
# Do NOT use MimeType.narrow_type here as it could incorrectly narrow
|
|
|
|
|
# security-sensitive types (e.g., SVG with .png extension -> image/png)
|
|
|
|
|
content_type = OpenProject::ContentTypeDetector.new(file_path).detect
|
2014-12-15 19:01:54 +01:00
|
|
|
content_type || fallback
|
2008-05-17 11:03:43 +00:00
|
|
|
end
|
2017-12-04 12:35:51 +01:00
|
|
|
|
2021-02-11 16:02:18 +01:00
|
|
|
def copy
|
2019-05-21 13:27:35 +01:00
|
|
|
attachment = dup
|
|
|
|
|
attachment.file = diskfile
|
|
|
|
|
|
|
|
|
|
yield attachment if block_given?
|
|
|
|
|
|
|
|
|
|
attachment
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def copy!(&)
|
|
|
|
|
attachment = copy(&)
|
|
|
|
|
|
|
|
|
|
attachment.save!
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-07 16:24:35 +01:00
|
|
|
def enqueue_jobs
|
2024-02-19 09:51:46 +01:00
|
|
|
extract_fulltext
|
2019-08-14 08:40:38 +02:00
|
|
|
|
2024-02-07 16:24:35 +01:00
|
|
|
if pending_virus_scan?
|
|
|
|
|
Attachments::VirusScanJob.perform_later(self)
|
|
|
|
|
end
|
2017-12-04 12:35:51 +01:00
|
|
|
end
|
|
|
|
|
|
2024-02-19 09:51:46 +01:00
|
|
|
def extract_fulltext
|
|
|
|
|
if OpenProject::Database.allows_tsv? && (!container || container.class.attachment_tsv_extracted?)
|
|
|
|
|
Attachments::ExtractFulltextJob.perform_later(id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-01-26 16:16:40 +01:00
|
|
|
# Extract the fulltext of any attachments where fulltext is still nil.
|
2020-01-28 20:54:21 +01:00
|
|
|
# This runs inline and not in an asynchronous worker.
|
2019-06-07 07:17:42 +02:00
|
|
|
def self.extract_fulltext_where_missing(run_now: true)
|
2018-02-02 18:27:43 +01:00
|
|
|
return unless OpenProject::Database.allows_tsv?
|
2019-06-07 07:17:42 +02:00
|
|
|
|
|
|
|
|
Attachment
|
|
|
|
|
.where(fulltext: nil)
|
2020-01-28 20:54:21 +01:00
|
|
|
.where(container_type: tsv_extracted_containers)
|
2019-06-07 07:17:42 +02:00
|
|
|
.pluck(:id)
|
|
|
|
|
.each do |id|
|
|
|
|
|
if run_now
|
2024-02-07 16:24:35 +01:00
|
|
|
Attachments::ExtractFulltextJob.perform_now(id)
|
2019-06-07 07:17:42 +02:00
|
|
|
else
|
2024-02-07 16:24:35 +01:00
|
|
|
Attachments::ExtractFulltextJob.perform_later(id)
|
2019-06-07 07:17:42 +02:00
|
|
|
end
|
2017-12-04 12:35:51 +01:00
|
|
|
end
|
|
|
|
|
end
|
2018-02-01 16:22:37 +01:00
|
|
|
|
|
|
|
|
def self.force_extract_fulltext
|
2018-02-02 18:27:43 +01:00
|
|
|
return unless OpenProject::Database.allows_tsv?
|
2019-06-07 07:17:42 +02:00
|
|
|
|
2018-02-01 16:22:37 +01:00
|
|
|
Attachment.pluck(:id).each do |id|
|
2024-02-07 16:24:35 +01:00
|
|
|
Attachments::ExtractFulltextJob.perform_now(id)
|
2018-02-01 16:22:37 +01:00
|
|
|
end
|
|
|
|
|
end
|
2018-05-24 17:16:55 +02:00
|
|
|
|
2020-02-05 10:41:30 +01:00
|
|
|
def self.tsv_extracted_containers
|
2020-01-28 20:54:21 +01:00
|
|
|
Attachment
|
|
|
|
|
.select(:container_type)
|
|
|
|
|
.distinct
|
|
|
|
|
.pluck(:container_type)
|
|
|
|
|
.compact
|
|
|
|
|
.select do |container_class|
|
2020-02-27 08:56:51 +01:00
|
|
|
klass = container_class.constantize
|
|
|
|
|
|
|
|
|
|
klass.respond_to?(:attachment_tsv_extracted?) && klass.attachment_tsv_extracted?
|
|
|
|
|
rescue NameError
|
|
|
|
|
false
|
2020-01-28 20:54:21 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-12-17 15:41:16 +00:00
|
|
|
##
|
|
|
|
|
# Deletes locally cached files. This is mostly relevant for remote attachments
|
|
|
|
|
# but would also apply for local attachments if things such as carrierwave
|
|
|
|
|
# filters were used.
|
|
|
|
|
#
|
|
|
|
|
# @param age_in_seconds [Integer] Delete all cached files older than this many seconds.
|
|
|
|
|
def self.clean_cached_files!(age_in_seconds: 60 * 60 * 24)
|
|
|
|
|
uploader = OpenProject::Configuration.file_uploader
|
|
|
|
|
cache_storage = uploader.cache_storage
|
|
|
|
|
|
|
|
|
|
cache_storage.new(uploader.new).clean_cache! age_in_seconds
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-03 13:17:16 +01:00
|
|
|
def pending_direct_upload?
|
|
|
|
|
digest == "" && downloads == -1
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-19 09:51:46 +01:00
|
|
|
def internal_container?
|
|
|
|
|
container&.is_a?(Export)
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-24 17:16:55 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def filesize_below_allowed_maximum
|
2021-07-14 14:43:19 +02:00
|
|
|
if filesize.to_i > Setting.attachment_max_size.to_i.kilobytes
|
2018-05-24 17:16:55 +02:00
|
|
|
errors.add(:file, :file_too_large, count: Setting.attachment_max_size.to_i.kilobytes)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def container_changed_more_than_once
|
|
|
|
|
if container_id_changed_more_than_once? || container_type_changed_more_than_once?
|
|
|
|
|
errors.add(:container, :unchangeable)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def container_id_changed_more_than_once?
|
|
|
|
|
container_id_changed? && container_id_was.present? && container_id_was != container_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def container_type_changed_more_than_once?
|
|
|
|
|
container_type_changed? && container_type_was.present? && container_type_was != container_type
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def allowed_or_author?(user)
|
2019-08-14 08:40:38 +02:00
|
|
|
(containered? && !(container.class.attachable_options[:only_user_allowed] && author_id != user.id) && yield) ||
|
2018-05-24 17:16:55 +02:00
|
|
|
(!containered? && author_id == user.id)
|
|
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|