Remove carrierwave
This commit is contained in:
parent
4bc0006ca0
commit
fe84e8e0f7
6 changed files with 0 additions and 182 deletions
|
@ -1,51 +0,0 @@
|
|||
require Rails.root.join("lib", "tasks", "task_helper")
|
||||
|
||||
namespace :'2017_10_30_copy_commentaire_piece_justificative_to_file' do
|
||||
task set: :environment do
|
||||
commentaires_to_process = Commentaire.where(file: nil).where.not(piece_justificative_id: nil).reorder(id: :desc)
|
||||
|
||||
rake_puts "#{commentaires_to_process.count} commentaires to process..."
|
||||
|
||||
commentaires_to_process.each do |c|
|
||||
process_commentaire(c)
|
||||
end
|
||||
end
|
||||
|
||||
task fix: :environment do
|
||||
commentaires_to_fix = Commentaire.where.not(file: nil).where.not(piece_justificative_id: nil).reorder(id: :desc)
|
||||
|
||||
rake_puts "#{commentaires_to_fix.count} commentaires to fix..."
|
||||
|
||||
commentaires_to_fix.each do |c|
|
||||
process_commentaire(c)
|
||||
end
|
||||
end
|
||||
|
||||
def sanitize_name(name) # from https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/sanitized_file.rb#L323
|
||||
name = name.gsub(/[^[:word:]\.\-\+]/, "_")
|
||||
name = "_#{name}" if name.match?(/\A\.+\z/)
|
||||
name = "unnamed" if name.empty?
|
||||
return name.mb_chars.to_s
|
||||
end
|
||||
|
||||
def process_commentaire(commentaire)
|
||||
rake_puts "Processing commentaire #{commentaire.id}"
|
||||
if commentaire.piece_justificative.present?
|
||||
# https://github.com/carrierwaveuploader/carrierwave#uploading-files-from-a-remote-location
|
||||
commentaire.remote_file_url = commentaire.piece_justificative.content_url
|
||||
|
||||
if commentaire.piece_justificative.original_filename.present?
|
||||
commentaire.file.define_singleton_method(:filename) { sanitize_name(commentaire.piece_justificative.original_filename) }
|
||||
end
|
||||
|
||||
if commentaire.body.blank?
|
||||
commentaire.body = commentaire.piece_justificative.original_filename || "."
|
||||
end
|
||||
|
||||
commentaire.save
|
||||
if commentaire.file.blank?
|
||||
rake_puts "Failed to save file for commentaire #{commentaire.id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,113 +0,0 @@
|
|||
require Rails.root.join("lib", "tasks", "task_helper")
|
||||
|
||||
namespace :cloudstorage do
|
||||
task init: :environment do
|
||||
os_config = (YAML.load_file(Fog.credentials_path))['default']
|
||||
@os = OpenStack::Connection.create(
|
||||
{
|
||||
username: os_config['openstack_username'],
|
||||
api_key: os_config['openstack_api_key'],
|
||||
auth_method: "password",
|
||||
auth_url: "https://auth.cloud.ovh.net/v2.0/",
|
||||
authtenant_name: os_config['openstack_tenant'],
|
||||
service_type: "object-store",
|
||||
region: os_config['openstack_region']
|
||||
}
|
||||
)
|
||||
@cont = @os.container(CarrierWave::Uploader::Base.fog_directory)
|
||||
end
|
||||
|
||||
desc 'Move local attestations on cloud storage'
|
||||
task migrate: :environment do
|
||||
puts 'Starting migration'
|
||||
|
||||
Rake::Task['cloudstorage:init'].invoke
|
||||
|
||||
error_count = 0
|
||||
[Cerfa, PieceJustificative, Procedure].each do |c|
|
||||
c.all.each do |entry|
|
||||
content = (c == Procedure) ? entry.logo : entry.content
|
||||
if !(content.current_path.nil? || File.exist?(File.dirname(content.current_path) + '/uploaded'))
|
||||
secure_token = SecureRandom.uuid
|
||||
filename = "#{entry.class.to_s.underscore}-#{secure_token}#{File.extname(content.current_path)}"
|
||||
rake_puts "Uploading #{content.current_path}"
|
||||
begin
|
||||
@cont.create_object(filename, {}, File.open(content.current_path))
|
||||
|
||||
File.open(File.dirname(content.current_path) + '/uploaded', "w+") { |f| f.write(File.basename(content.current_path)) }
|
||||
File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "w+") { |f| f.write(filename) }
|
||||
File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "w+") { |f| f.write(secure_token) }
|
||||
|
||||
entry.update_column(c == Procedure ? :logo : :content, filename)
|
||||
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
|
||||
rescue Errno::ENOENT
|
||||
rake_puts "ERROR: #{content.current_path} does not exist!"
|
||||
File.open('upload_errors.report', "a+") { |f| f.write(content.current_path) }
|
||||
error_count += 1
|
||||
end
|
||||
else
|
||||
if content.current_path.present? && File.exist?(File.dirname(content.current_path) + '/uploaded')
|
||||
filename = File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read
|
||||
secure_token = File.open(File.dirname(content.current_path) + '/secure_token_cloudstorage', "r").read
|
||||
|
||||
entry.update_column(c == Procedure ? :logo : :content, filename)
|
||||
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, secure_token)
|
||||
|
||||
rake_puts "RESTORE IN DATABASE: #{filename} "
|
||||
elsif content.current_path.present?
|
||||
rake_puts "Skipping #{content.current_path}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
rake_puts "There were #{error_count} errors while uploading files. See upload_errors.report file for details."
|
||||
puts 'Enf of migration'
|
||||
end
|
||||
|
||||
desc 'Clear documents in tenant and revert file entries in database'
|
||||
task :revert do
|
||||
Rake::Task['cloudstorage:init'].invoke
|
||||
|
||||
[Cerfa, PieceJustificative, Procedure].each do |c|
|
||||
c.all.each do |entry|
|
||||
content = (c == Procedure) ? entry.logo : entry.content
|
||||
if content.current_path.present?
|
||||
if File.exist?(File.dirname(content.current_path) + '/uploaded')
|
||||
previous_filename = File.read(File.dirname(content.current_path) + '/uploaded')
|
||||
|
||||
entry.update_column(c == Procedure ? :logo : :content, previous_filename)
|
||||
entry.update_column(c == Procedure ? :logo_secure_token : :content_secure_token, nil)
|
||||
|
||||
rake_puts "restoring #{content.current_path} db data to #{previous_filename}"
|
||||
|
||||
@cont.delete_object(File.open(File.dirname(content.current_path) + '/filename_cloudstorage', "r").read)
|
||||
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/uploaded')
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/filename_cloudstorage')
|
||||
FileUtils.rm(File.dirname(content.current_path) + '/secure_token_cloudstorage')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Clear old documents in tenant'
|
||||
task :clear do
|
||||
Rake::Task['cloudstorage:init'].invoke
|
||||
|
||||
@cont.objects.each do |object|
|
||||
rake_puts "Removing #{object}"
|
||||
@cont.delete_object(object)
|
||||
end
|
||||
end
|
||||
|
||||
task :clear_old_objects do
|
||||
Rake::Task['cloudstorage:init'].invoke
|
||||
|
||||
@cont.objects_detail.each do |object, details|
|
||||
last_modified = Time.zone.parse(details[:last_modified])
|
||||
@cont.delete_object(object) if last_modified.utc <= (Time.zone.now - 2.years).utc
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue