From f7ab0b4bfe3958a164fc572634d8355cbba9508f Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 17 Mar 2020 17:47:28 +0800 Subject: [PATCH] fix recreate image failed error when too many images --- app/controllers/admin/galleries_controller.rb | 252 +++++++++--------- 1 file changed, 127 insertions(+), 125 deletions(-) diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index f867e10..1568466 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -142,49 +142,49 @@ class Admin::GalleriesController < OrbitAdminController count = cords.length images.each_with_index do |image,index| cord = cords[index] - 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]) - else - image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3]) - end - end - variable = AlbumVariable.first - if variable.nil? - variable = AlbumVariable.new - end - variable.finish = false - variable.save! + 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]) + else + image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3]) + end + end + variable = AlbumVariable.first + if variable.nil? + variable = AlbumVariable.new + end + variable.finish = false + variable.save! Thread.new do - variable = AlbumVariable.first + variable = AlbumVariable.first images.each_with_index do |image,index| variable.progress_percent = (index*100.0/count).floor.to_s+'%' - all_version = image.file.versions.map{|k,v| k} - begin - #org_fname = image.file.path - #org_extname = File.extname(org_fname) - #org_fname.slice! org_extname - #FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname) - variable.progress_filename = image[:file].to_s - 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 => e - variable.progress_filename = e.inspect.to_s - puts e.inspect - end - variable.save! - end - variable.finish = true - variable.save! - end - rescue => e - puts e.inspect - end - redirect_url = "/admin/galleries/crop_process" - render :json => {'href' => redirect_url}.to_json + all_version = image.file.versions.map{|k,v| k} + begin + #org_fname = image.file.path + #org_extname = File.extname(org_fname) + #org_fname.slice! org_extname + #FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname) + variable.progress_filename = image[:file].to_s + 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 => e + variable.progress_filename = e.inspect.to_s + puts e.inspect + end + variable.save! + end + variable.finish = true + variable.save! + end + rescue => e + puts e.inspect + end + redirect_url = "/admin/galleries/crop_process" + render :json => {'href' => redirect_url}.to_json end def call_translate text = params['text'] @@ -193,73 +193,75 @@ class Admin::GalleriesController < OrbitAdminController def recreate_image notalive = ((AlbumVariable.first.notalive.nil? ? true : AlbumVariable.first.notalive) rescue true) if notalive - if !params['album_id'].to_s.empty? - variable = AlbumVariable.first - if variable.nil? - 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! + if !params['album_id'].to_s.empty? + variable = AlbumVariable.first + if variable.nil? + 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 + 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 def finish_recreate - variable = AlbumVariable.first - variable.progress_percent = '0%' - variable.progress_filename = '' - variable.notalive = true - variable.save! + variable = AlbumVariable.first + variable.progress_percent = '0%' + variable.progress_filename = '' + variable.notalive = true + variable.save! render :text => '' end def recreate_progress @@ -512,9 +514,9 @@ class Admin::GalleriesController < OrbitAdminController File.delete file.path un_image.delete variable = AlbumVariable.first - variable.progress_filename = album[:file] - variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%' - variable.save! + variable.progress_filename = album[:file] + variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%' + variable.save! rescue => e puts ['err',un_image.inspect,e.inspect] un_image.destroy @@ -536,10 +538,10 @@ class Admin::GalleriesController < OrbitAdminController render :json => {}.to_json end def init_upload - variable = AlbumVariable.first - if variable.nil? - variable = AlbumVariable.new - end + variable = AlbumVariable.first + if variable.nil? + variable = AlbumVariable.new + end variable.count = params['all_length'].to_i variable.finish = false variable.progress_percent = '0%' @@ -640,20 +642,20 @@ class Admin::GalleriesController < OrbitAdminController end private - def crop_but_no_backup image - fname = image.file.path - extension = File.extname(fname) - base_name = fname.chomp(extension) - base_name += ('_resized'+ extension) - File.file?(base_name) - end - def setup_vars - @module_app = ModuleApp.where(:key=>"gallery").first - end + def crop_but_no_backup image + fname = image.file.path + extension = File.extname(fname) + base_name = fname.chomp(extension) + base_name += ('_resized'+ extension) + File.file?(base_name) + end + def setup_vars + @module_app = ModuleApp.where(:key=>"gallery").first + end - def album_params - p = params.require(:album).permit! - p["tags"] = p["tags"] || [] - p - end + def album_params + p = params.require(:album).permit! + p["tags"] = p["tags"] || [] + p + end end