#1396 the task can be run repeatedly

This commit is contained in:
Karel Pičman 2022-08-26 10:44:29 +02:00
parent dc2ee263cb
commit 77564178c2

View File

@ -21,53 +21,41 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
desc <<-END_DESC desc <<-END_DESC
Convert project documents to DMSF folder/file structure. Convert projects' Documents to DMSF folder/file structure.
Converted project must have documents and may not have DMSF active! Converted project must have Document module enabled
Available options: Available options:
* project - id or identifier of project (defaults to all projects) * project - id or identifier of a project (default to all projects)
* dry - true or false (default false) to perform just check without any conversion * dry_run - perform just a check without any conversion
* invalid - replace to perform document title invalid characters replacement for '-'
Example: Example:
rake redmine:dmsf_convert_documents project=test RAILS_ENV="production" rake redmine:dmsf_convert_documents project=test RAILS_ENV="production"
rake redmine:dmsf_convert_documents project=test dry=true RAILS_ENV="production" rake redmine:dmsf_convert_documents project=test dry_run=1 RAILS_ENV="production"
END_DESC END_DESC
class DmsfConvertDocuments class DmsfConvertDocuments
def self.convert(options={}) def self.convert(options={})
dry = options[:dry] ? options[:dry] == 'true' : false projects = []
replace = options[:invalid] ? options[:invalid] == 'replace' : false if options[:project]
p = Project.find(options[:project])
projects = options[:project] ? [Project.find(options[:project])] : Project.active.to_a projects << p if p&.module_enabled?('documents')
else
if projects projects = Project.active.has_module('documents').to_a
prjs = projects.reject {|project| project.module_enabled?('dmsf') } end
diff = (projects - prjs) if projects.any?
if diff.count > 0
puts "Projects skipped due to already enabled module DMSF: #{diff.collect{|p| p.name }.join(' ,')}"
end
projects = prjs.reject {|project| !project.module_enabled?('documents') }
diff = (prjs - projects)
if diff.count > 0
puts "Projects skipped due to not enabled module Documents: #{diff.collect{|p| p.name }.join(' ,')}"
end
projects.each do |project| projects.each do |project|
puts "Processing project: #{project}" STDOUT.puts "Processing project: #{project.name}"
unless options[:dry_run]
project.enabled_module_names = (project.enabled_module_names << 'dmsf').uniq unless dry project.enable_module! 'dmsf'
project.save! unless dry #project.save!
end
fail = false fail = false
folders = [] folders = []
project.documents.each do |document| project.documents.each do |document|
puts "Processing document: #{document.title}" STDOUT.puts "Processing document: #{document.title}"
folder = DmsfFolder.new folder = DmsfFolder.new
folder.project = project folder.project = project
attachment = document.attachments.reorder(created_on: :asc).first attachment = document.attachments.reorder(created_on: :asc).first
if attachment if attachment
@ -75,46 +63,36 @@ class DmsfConvertDocuments
else else
folder.user = User.active.where(admin: true).first folder.user = User.active.where(admin: true).first
end end
folder.title = DmsfFolder.get_valid_title(document.title)
folder.title = document.title
folder.title.gsub!(/[\/\\\?":<>]/, '-') if replace
i = 1 i = 1
suffix = '' suffix = ''
while folders.index{|f| f.title == (folder.title + suffix)} while folders.index{|f| f.title == (folder.title + suffix)}
i+=1 i+=1
suffix = "_#{i}" suffix = "_#{i}"
end end
folder.title = folder.title + suffix folder.title = folder.title + suffix
folder.description = document.description folder.description = document.description
if options[:dry_run]
if dry STDOUT.puts "Dry run folder: #{folder.title}"
puts "Dry check folder: #{folder.title}" STDERR.puts(folder.errors.full_messages.to_sentence) if folder.invalid?
if folder.invalid?
folder.errors.each {|e,msg| puts "#{e}: #{msg}"}
end
else else
begin begin
folder.save! folder.save!
puts "Created folder: #{folder.title}" STDOUT.puts "Created folder: #{folder.title}"
rescue => e rescue => e
puts "Creating folder: #{folder.title} failed" STDERR.puts "Creating folder: #{folder.title} failed"
puts e STDERR.puts e.message
fail = true fail = true
next next
end end
end end
folders << folder folders << folder
files = [] files = []
document.attachments.each do |attachment| document.attachments.each do |attachment|
begin begin
file = DmsfFile.new file = DmsfFile.new
file.project_id = project.id file.project_id = project.id
file.dmsf_folder = folder file.dmsf_folder = folder
file.name = attachment.filename file.name = attachment.filename
i = 1 i = 1
suffix = '' suffix = ''
@ -122,27 +100,21 @@ class DmsfConvertDocuments
i += 1 i += 1
suffix = "_#{i}" suffix = "_#{i}"
end end
# Need to save file first to generate id for it in case of creation. # Need to save file first to generate id for it in case of creation.
# File id is needed to properly generate revision disk filename # File id is needed to properly generate revision disk filename
file.name = DmsfFileRevision.remove_extension(file.name) + suffix + File.extname(file.name) file.name = DmsfFileRevision.remove_extension(file.name) + suffix + File.extname(file.name)
unless File.exist?(attachment.diskfile) unless File.exist?(attachment.diskfile)
puts "Creating file: #{attachment.filename} failed, attachment file #{attachment.diskfile} doesn't exist" STDERR.puts "Creating file: #{attachment.filename} failed, attachment file #{attachment.diskfile} doesn't exist"
fail = true fail = true
next next
end end
if options[:dry_run]
if dry
file.id = attachment.id # Just to have an ID there file.id = attachment.id # Just to have an ID there
puts "Dry check file: #{file.name}" STDOUT.puts "Dry run file: #{file.name}"
if file.invalid? STDERR.puts(file.errors.full_messages.to_sentence) if file.invalid?
file.errors.each {|e, msg| puts "#{e}: #{msg}"}
end
else else
file.save! file.save!
end end
revision = DmsfFileRevision.new revision = DmsfFileRevision.new
revision.dmsf_file = file revision.dmsf_file = file
revision.name = file.name revision.name = file.name
@ -153,43 +125,36 @@ class DmsfConvertDocuments
revision.updated_at = attachment.created_on revision.updated_at = attachment.created_on
revision.major_version = 0 revision.major_version = 0
revision.minor_version = 1 revision.minor_version = 1
revision.comment = 'Converted from documents' revision.comment = 'Converted from Documents'
revision.mime_type = attachment.content_type revision.mime_type = attachment.content_type
revision.disk_filename = revision.new_storage_filename
revision.disk_filename = revision.new_storage_filename unless options[:dry_run]
unless dry
FileUtils.cp attachment.diskfile, revision.disk_file(false) FileUtils.cp attachment.diskfile, revision.disk_file(false)
revision.size = File.size(revision.disk_file(false)) revision.size = File.size(revision.disk_file(false))
end end
if options[:dry_run]
if dry STDOUT.puts "Dry run revision: #{revision.title}"
puts "Dry check revision: #{revision.title}" STDERR.puts(revision.errors.full_messages.to_sentence) if revision.invalid?
if revision.invalid?
revision.errors.each {|e, msg| puts "#{e}: #{msg}"}
end
else else
revision.save! revision.save!
end end
files << file files << file
attachment.destroy unless options[:dry_run]
attachment.destroy unless dry STDOUT.puts "Created file: #{file.name}" unless options[:dry_run]
puts "Created file: #{file.name}" unless dry
rescue => e rescue => e
puts "Creating file: #{attachment.filename} failed" STDERR.puts "Creating file: #{attachment.filename} failed"
puts e STDERR.puts e.message
fail = true fail = true
end end
end end
document.destroy unless options[:dry_run] || fail
document.destroy unless dry || fail
end end
project.enabled_module_names = project.enabled_module_names.reject {|mod| mod == 'documents'} unless dry || fail unless options[:dry_run] || fail
project.save! unless dry project.disable_module!('documents')
end end
end
else
STDERR.puts 'No project(s) with Documents module enabled found.'
end end
end end
@ -198,10 +163,9 @@ end
namespace :redmine do namespace :redmine do
task :dmsf_convert_documents => :environment do task :dmsf_convert_documents => :environment do
options = {} options = {}
options[:project] = ENV['project'] if ENV['project'] options[:project] = ENV['project']
options[:dry] = ENV['dry'] if ENV['dry'] options[:dry_run] = ENV['dry_run']
options[:invalid] = ENV['invalid'] if ENV['invalid'] options[:invalid] = ENV['invalid']
DmsfConvertDocuments.convert options
DmsfConvertDocuments.convert(options)
end end
end end