Skip to content

Commit

Permalink
Write first version of ImageCleaner for #1502
Browse files Browse the repository at this point in the history
Write to file and add worker
  • Loading branch information
marcaltmann committed Dec 27, 2016
1 parent d0d30dc commit 043ea2c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/objects/service/image_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2012-2016, Fairmondo eG. This file is
# licensed under the GNU Affero General Public License version 3 or later.
# See the COPYRIGHT file for details.

class ImageCleaner
def initialize
end

def show(range)
files_found = 0

File.open('image_cleaner.txt', 'w') do |file|
for id in range
path = path(id)

begin
img = Image.find(id)
file.puts("#{path}: #{img.image_file_name}")
rescue ActiveRecord::RecordNotFound
if File.exist?(path)
files_found += 1
file.puts("#{path}: no image")
end
end
end

file.puts("Extranous files total: #{files_found}")
end
end

def path(id)
id_str = padded_id(id)
Rails.root.join('public', 'system', 'images', id_str[0..2], id_str[3..5], id_str[6..8])
end

def padded_id(id)
sprintf('%09d', id)
end
end
16 changes: 16 additions & 0 deletions app/workers/image_cleaner_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2012-2016, Fairmondo eG. This file is
# licensed under the GNU Affero General Public License version 3 or later.
# See the COPYRIGHT file for details.

class ImageCleanerWorker
include Sidekiq::Worker

sidekiq_options queue: :default,
retry: 20,
backtrace: true

def perform(range)
cleaner = ImageCleaner.new
cleaner.show(range)
end
end

0 comments on commit 043ea2c

Please sign in to comment.