Fix bug for rails 4.2.

This commit is contained in:
BoHung Chiu 2023-08-15 21:39:42 +08:00
parent 3be81db424
commit b3dfe4981a
1 changed files with 32 additions and 12 deletions

View File

@ -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,7 +516,10 @@ 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 = 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
@ -511,6 +530,7 @@ class Admin::GalleriesController < OrbitAdminController
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