From c4da9ce62f668c1a50c6efa9cec4bb4fe4e52cee Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 14 Jan 2015 21:30:51 +0530 Subject: [PATCH] FIX: old csv's were not getting deleted --- app/jobs/regular/export_csv_file.rb | 31 ++++++++++------------------- app/models/user_export.rb | 2 +- spec/jobs/export_csv_file_spec.rb | 2 +- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index 58ee9d6d9..82cbc1b70 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -17,22 +17,12 @@ module Jobs sidekiq_options retry: false attr_accessor :current_user - def initialize - @file_name = "" - @entity_type = "admin" - end - def execute(args) - entity = args[:entity] - @file_name = entity - - if entity == "user_archive" - @entity_type = "user" - end - + @entity = args[:entity] + @file_name = @entity @current_user = User.find_by(id: args[:user_id]) - export_method = "#{entity}_export".to_sym + export_method = "#{@entity}_export".to_sym data = if respond_to?(export_method) send(export_method) @@ -42,8 +32,7 @@ module Jobs if data && data.length > 0 set_file_path - header = get_header(entity) - write_csv_file(data, header) + write_csv_file(data) end ensure @@ -102,9 +91,9 @@ module Jobs end end - def get_header(entity) + def get_header - case entity + case @entity when 'user_list' header_array = HEADER_ATTRS_FOR['user_list'] + HEADER_ATTRS_FOR['user_stats'] if SiteSetting.enable_sso @@ -118,7 +107,7 @@ module Jobs end header_array.push("group_names") else - header_array = HEADER_ATTRS_FOR[entity] + header_array = HEADER_ATTRS_FOR[@entity] end header_array @@ -271,8 +260,8 @@ module Jobs def set_file_path - @file = UserExport.create(export_type: @entity_type, user_id: @current_user.id) file_name_prefix = @file_name.split('_').join('-') + @file = UserExport.create(export_type: file_name_prefix, user_id: @current_user.id) @file_name = "#{file_name_prefix}-#{@file.id}.csv" # ensure directory exists @@ -280,10 +269,10 @@ module Jobs FileUtils.mkdir_p(dir) unless Dir.exists?(dir) end - def write_csv_file(data, header) + def write_csv_file(data) # write to CSV file CSV.open(File.expand_path("#{UserExport.base_directory}/#{@file_name}", __FILE__), "w") do |csv| - csv << header + csv << get_header data.each do |value| csv << value end diff --git a/app/models/user_export.rb b/app/models/user_export.rb index 9b1e9aadd..8b01198cf 100644 --- a/app/models/user_export.rb +++ b/app/models/user_export.rb @@ -12,7 +12,7 @@ class UserExport < ActiveRecord::Base def self.remove_old_exports expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a expired_exports.map do |expired_export| - file_name = "export_#{expired_export.id}.csv" + file_name = "#{expired_export.export_type}-#{expired_export.id}.csv.gz" file_path = "#{UserExport.base_directory}/#{file_name}" if File.exist?(file_path) diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb index 457c9bcef..e56b56a49 100644 --- a/spec/jobs/export_csv_file_spec.rb +++ b/spec/jobs/export_csv_file_spec.rb @@ -9,7 +9,7 @@ describe Jobs::ExportCsvFile do end let :user_list_header do - Jobs::ExportCsvFile.new.get_header('user_list') + ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address','topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received','external_id','external_email', 'external_username', 'external_name', 'external_avatar_url'] end let :user_list_export do