From b3dfe4981a62b124b26af7aab6e3f5dc533c4507 Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Tue, 15 Aug 2023 21:39:42 +0800 Subject: [PATCH] Fix bug for rails 4.2. --- app/controllers/admin/galleries_controller.rb | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 5d59505..333458e 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -7,6 +7,8 @@ class Admin::GalleriesController < OrbitAdminController layout :compute_layout find_tag = Tag.all.select{|value| value.name==I18n.t('gallery.not_show_desc')} RackTempMiddleExist = defined?(Rack::TempfileReaper) + RACK_TEMPFILES = defined?(Rack::RACK_TEMPFILES) ? Rack::RACK_TEMPFILES : 'rack.tempfiles' + Is_Rails5 = (Rails.version.to_f >= 5) if find_tag.length==0 module_app_id = ModuleApp.where(:key=>"gallery").first[:_id] tags = ModuleApp.where(:key=>"gallery").first.tags @@ -40,6 +42,20 @@ class Admin::GalleriesController < OrbitAdminController end end end + if RackTempMiddleExist # Avoid Rack::TempfileReaper Middleware to unlink temp files automatically. + if Is_Rails5 + def fix_tempfile_reaper + request.set_header(RACK_TEMPFILES, []) + end + else + def fix_tempfile_reaper + env[RACK_TEMPFILES] = nil + end + end + else + def fix_tempfile_reaper #do nothing for old rack + end + end def compute_layout if action_name== 'index' && !params['page_no'].nil? false @@ -500,17 +516,21 @@ class Admin::GalleriesController < OrbitAdminController Thread.new do album_unprocess.each_with_index do |un_image,i| begin - album = AlbumImage.all.select{|value| value.id.to_s == un_image.image_id.to_s}[0] - album.file = un_image.save_var - album.save! - file = un_image.save_var.tempfile - file.close - 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! + album = AlbumImage.where(:id => un_image.image_id).first + if album.nil? + un_image.delete + else + album.file = un_image.save_var + album.save! + file = un_image.save_var.tempfile + file.close + 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! + end rescue => e puts ['err',un_image.inspect,e.inspect] un_image.destroy @@ -559,7 +579,7 @@ class Admin::GalleriesController < OrbitAdminController album_unprocess.save_var = file album_unprocess.save! ObjectSpace.undefine_finalizer(file.tempfile) - request.set_header(Rack::RACK_TEMPFILES, []) if RackTempMiddleExist # Avoid Rack::TempfileReaper Middleware to unlink temp files automatically. + fix_tempfile_reaper end image.save! end