fix recreate image failed error when too many images

This commit is contained in:
chiu 2020-03-17 17:47:28 +08:00
parent f0f5419e07
commit f7ab0b4bfe
1 changed files with 127 additions and 125 deletions

View File

@ -142,49 +142,49 @@ class Admin::GalleriesController < OrbitAdminController
count = cords.length count = cords.length
images.each_with_index do |image,index| images.each_with_index do |image,index|
cord = cords[index] cord = cords[index]
if image.album_crops.first.nil? if image.album_crops.first.nil?
image.album_crops.create(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3]) image.album_crops.create(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
else else
image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3]) image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
end end
end end
variable = AlbumVariable.first variable = AlbumVariable.first
if variable.nil? if variable.nil?
variable = AlbumVariable.new variable = AlbumVariable.new
end end
variable.finish = false variable.finish = false
variable.save! variable.save!
Thread.new do Thread.new do
variable = AlbumVariable.first variable = AlbumVariable.first
images.each_with_index do |image,index| images.each_with_index do |image,index|
variable.progress_percent = (index*100.0/count).floor.to_s+'%' variable.progress_percent = (index*100.0/count).floor.to_s+'%'
all_version = image.file.versions.map{|k,v| k} all_version = image.file.versions.map{|k,v| k}
begin begin
#org_fname = image.file.path #org_fname = image.file.path
#org_extname = File.extname(org_fname) #org_extname = File.extname(org_fname)
#org_fname.slice! org_extname #org_fname.slice! org_extname
#FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname) #FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname)
variable.progress_filename = image[:file].to_s variable.progress_filename = image[:file].to_s
all_version.each do |version| all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image)) if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version image.file.recreate_versions! version
image.save! image.save!
end end
end end
rescue => e rescue => e
variable.progress_filename = e.inspect.to_s variable.progress_filename = e.inspect.to_s
puts e.inspect puts e.inspect
end end
variable.save! variable.save!
end end
variable.finish = true variable.finish = true
variable.save! variable.save!
end end
rescue => e rescue => e
puts e.inspect puts e.inspect
end end
redirect_url = "/admin/galleries/crop_process" redirect_url = "/admin/galleries/crop_process"
render :json => {'href' => redirect_url}.to_json render :json => {'href' => redirect_url}.to_json
end end
def call_translate def call_translate
text = params['text'] text = params['text']
@ -193,73 +193,75 @@ class Admin::GalleriesController < OrbitAdminController
def recreate_image def recreate_image
notalive = ((AlbumVariable.first.notalive.nil? ? true : AlbumVariable.first.notalive) rescue true) notalive = ((AlbumVariable.first.notalive.nil? ? true : AlbumVariable.first.notalive) rescue true)
if notalive if notalive
if !params['album_id'].to_s.empty? if !params['album_id'].to_s.empty?
variable = AlbumVariable.first variable = AlbumVariable.first
if variable.nil? if variable.nil?
variable = AlbumVariable.new variable = AlbumVariable.new
end
variable.finish = false
variable.save!
choice_ids = params['album_id'].split(',')
albums = Album.all.select { |value| (choice_ids.include? value.id.to_s)}
if !(params['use_default']=='true')
color = params['color_choice'].to_s.empty? ? 'transparent' : params['color_choice']
albums.each do |album|
if album.album_colors.first.nil?
album.album_colors.create('color' => color)
else
album.album_colors.first.update_attributes('color' => color, 'updated_at' => Time.now)
end
end
end
count = 0
albums.each{|album| count+=album.album_images.count }
Thread.new do
i = 0
albums.each do |album|
album.album_images.each do |image|
error = nil
all_version = image.file.versions.map{|k,v| k}
begin
all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => error
end
variable = AlbumVariable.first
variable.progress_percent = (i*100.0/count).floor.to_s+'%'
if !error.nil?
variable.progress_filename = error.inspect.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
sleep(1)
else
variable.progress_filename = image[:file].to_s
end
variable.save!
i+=1
end
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
end
else
variable = AlbumVariable.first
variable.finish = true
variable.progress_filename = ''
variable.progress_percent = '100%'
variable.save!
end end
variable.finish = false
variable.save!
choice_ids = params['album_id'].split(',')
albums = Album.all.select { |value| (choice_ids.include? value.id.to_s)}
if !(params['use_default']=='true')
color = params['color_choice'].to_s.empty? ? 'transparent' : params['color_choice']
albums.each do |album|
if album.album_colors.first.nil?
album.album_colors.create('color' => color)
else
album.album_colors.first.update_attributes('color' => color, 'updated_at' => Time.now)
end
end
end
count = 0
albums.each{|album| count+=album.album_images.count }
Thread.new do
i = 0
album_ids = albums.map{|v| v.id}.clone
album_ids.each do |album_id|
album = Album.find(album_id) rescue Album.new()
album.album_images.each do |image|
error = nil
all_version = image.file.versions.map{|k,v| k}
begin
all_version.each do |version|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => error
end
variable = AlbumVariable.first
variable.progress_percent = (i*100.0/count).floor.to_s+'%'
if !error.nil?
variable.progress_filename = error.inspect.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
sleep(1)
else
variable.progress_filename = image[:file].to_s
end
variable.save!
i+=1
end
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
end
else
variable = AlbumVariable.first
variable.finish = true
variable.progress_filename = ''
variable.progress_percent = '100%'
variable.save!
end
end end
end end
def finish_recreate def finish_recreate
variable = AlbumVariable.first variable = AlbumVariable.first
variable.progress_percent = '0%' variable.progress_percent = '0%'
variable.progress_filename = '' variable.progress_filename = ''
variable.notalive = true variable.notalive = true
variable.save! variable.save!
render :text => '' render :text => ''
end end
def recreate_progress def recreate_progress
@ -512,9 +514,9 @@ class Admin::GalleriesController < OrbitAdminController
File.delete file.path File.delete file.path
un_image.delete un_image.delete
variable = AlbumVariable.first variable = AlbumVariable.first
variable.progress_filename = album[:file] variable.progress_filename = album[:file]
variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%' variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%'
variable.save! variable.save!
rescue => e rescue => e
puts ['err',un_image.inspect,e.inspect] puts ['err',un_image.inspect,e.inspect]
un_image.destroy un_image.destroy
@ -536,10 +538,10 @@ class Admin::GalleriesController < OrbitAdminController
render :json => {}.to_json render :json => {}.to_json
end end
def init_upload def init_upload
variable = AlbumVariable.first variable = AlbumVariable.first
if variable.nil? if variable.nil?
variable = AlbumVariable.new variable = AlbumVariable.new
end end
variable.count = params['all_length'].to_i variable.count = params['all_length'].to_i
variable.finish = false variable.finish = false
variable.progress_percent = '0%' variable.progress_percent = '0%'
@ -640,20 +642,20 @@ class Admin::GalleriesController < OrbitAdminController
end end
private private
def crop_but_no_backup image def crop_but_no_backup image
fname = image.file.path fname = image.file.path
extension = File.extname(fname) extension = File.extname(fname)
base_name = fname.chomp(extension) base_name = fname.chomp(extension)
base_name += ('_resized'+ extension) base_name += ('_resized'+ extension)
File.file?(base_name) File.file?(base_name)
end end
def setup_vars def setup_vars
@module_app = ModuleApp.where(:key=>"gallery").first @module_app = ModuleApp.where(:key=>"gallery").first
end end
def album_params def album_params
p = params.require(:album).permit! p = params.require(:album).permit!
p["tags"] = p["tags"] || [] p["tags"] = p["tags"] || []
p p
end end
end end