2015-01-09 16:09:26 +00: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
|
2015-01-09 16:09:26 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
|
|
|
#
|
|
|
|
|
# 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
|
2015-01-09 16:09:26 +00: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.
|
2015-01-09 16:09:26 +00:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
namespace :attachments do
|
2016-12-12 08:49:35 +01:00
|
|
|
desc "Clear all attachments created before yesterday"
|
|
|
|
|
task clear: [:environment] do
|
2021-12-17 15:41:16 +00:00
|
|
|
Attachment.clean_cached_files!
|
2016-12-12 08:49:35 +01:00
|
|
|
end
|
|
|
|
|
|
2015-01-09 16:09:26 +00:00
|
|
|
desc "Copies all attachments from the current to the given storage."
|
2015-06-27 15:29:42 +02:00
|
|
|
task :copy_to, [:to] => :environment do |_task, args|
|
2015-01-09 16:09:26 +00:00
|
|
|
if args.empty?
|
|
|
|
|
puts "rake attachments:copy_to[file|fog]"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-13 13:54:46 +00:00
|
|
|
storage_name = args[:to].to_sym
|
2015-01-09 16:09:26 +00:00
|
|
|
current_uploader = OpenProject::Configuration.file_uploader
|
2015-01-13 13:54:46 +00:00
|
|
|
target_uploader = OpenProject::Configuration.available_file_uploaders[storage_name]
|
2015-01-09 16:09:26 +00:00
|
|
|
|
|
|
|
|
if target_uploader.nil?
|
|
|
|
|
puts "unknown storage: #{args[:to]}"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if target_uploader == current_uploader
|
|
|
|
|
puts "attachments already in #{target_uploader} storage"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-13 13:54:46 +00:00
|
|
|
if target_uploader == :fog && (
|
|
|
|
|
OpenProject::Configuration.fog_credentials.empty? ||
|
|
|
|
|
OpenProject::Configuration.fog_directory.nil?)
|
|
|
|
|
puts "the fog storage is not configured"
|
|
|
|
|
exit 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
target_attachment = Class.new(Attachment) do
|
|
|
|
|
def self.store_all!(attachments)
|
|
|
|
|
attachments.each_with_index do |attachment, index|
|
|
|
|
|
print "Copying attachment #{attachment.id} [#{index + 1}/#{attachments.size}] \
|
|
|
|
|
(#{attachment.file.path}) ... ".squish
|
|
|
|
|
STDOUT.flush
|
|
|
|
|
|
|
|
|
|
if store! attachment
|
|
|
|
|
puts " ok"
|
|
|
|
|
else
|
|
|
|
|
puts " failed (missing file)"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-09 16:09:26 +00:00
|
|
|
|
2015-01-13 13:54:46 +00:00
|
|
|
##
|
|
|
|
|
# Given an attachment using the source uploader creates a TargetAttachment
|
|
|
|
|
# which uses the destination uploader to store the original attachment's
|
|
|
|
|
# file in the target location.
|
|
|
|
|
def self.store!(attachment)
|
|
|
|
|
return nil unless attachment.attributes["file"].present? &&
|
2022-01-24 13:25:19 +01:00
|
|
|
File.exist?(attachment.file.path)
|
2015-01-13 13:54:46 +00:00
|
|
|
|
2015-06-27 15:29:42 +02:00
|
|
|
new.tap do |target|
|
2015-01-13 13:54:46 +00:00
|
|
|
target.id = attachment.id
|
|
|
|
|
target.file = attachment.file.local_file
|
|
|
|
|
target.file.store!
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Pretends to be a plain old Attachment in order not to break store paths.
|
|
|
|
|
def self.to_s
|
|
|
|
|
"attachment"
|
|
|
|
|
end
|
2015-01-09 16:09:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
attachments = Attachment.all
|
|
|
|
|
|
2015-01-13 13:54:46 +00:00
|
|
|
puts "Copying #{attachments.size} attachments to #{storage_name}."
|
|
|
|
|
puts
|
2015-01-09 16:09:26 +00:00
|
|
|
|
2015-01-13 13:54:46 +00:00
|
|
|
target_attachment.mount_uploader :file, target_uploader
|
|
|
|
|
target_attachment.store_all! attachments
|
2015-01-09 16:09:26 +00:00
|
|
|
end
|
2018-01-26 16:16:40 +01:00
|
|
|
|
2019-06-07 07:17:42 +02:00
|
|
|
desc "Extract text content from attachment that were not extracted yet synchronously."
|
2018-02-02 14:24:11 +01:00
|
|
|
task extract_fulltext_where_missing: :environment do
|
2019-06-07 07:17:42 +02:00
|
|
|
Attachment.extract_fulltext_where_missing(run_now: true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc "Extract text content from attachment that were not extracted yet."
|
|
|
|
|
task schedule_fulltext_extraction_where_missing: :environment do
|
|
|
|
|
Attachment.extract_fulltext_where_missing(run_now: false)
|
2018-01-26 16:16:40 +01:00
|
|
|
end
|
|
|
|
|
|
2018-02-02 14:24:11 +01:00
|
|
|
desc 'Extracts fulltext of all attachments and provide it for attachment filter even if that attachment has been \
|
2018-02-02 18:27:43 +01:00
|
|
|
extracted before.'
|
2018-02-02 14:24:11 +01:00
|
|
|
task force_extract_fulltext: :environment do
|
|
|
|
|
Attachment.force_extract_fulltext
|
|
|
|
|
end
|
2015-01-09 16:09:26 +00:00
|
|
|
end
|