From df13ec28e257d0838a7da7e5bf7f50bd54f8809c Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 22 Apr 2026 10:50:57 +0000 Subject: [PATCH] To avoid timeouts caused by excessive data --- app/controllers/admin/images_controller.rb | 68 +++++++++++++--------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/app/controllers/admin/images_controller.rb b/app/controllers/admin/images_controller.rb index d0a0455..d42ecf7 100644 --- a/app/controllers/admin/images_controller.rb +++ b/app/controllers/admin/images_controller.rb @@ -21,34 +21,46 @@ class Admin::ImagesController < OrbitAdminController @images = @album.album_images.asc(:order) end - def changeorder - type = params[:type] - if type == "imgholder" - album = Album.find(params[:album_id]) rescue nil - images = params[:imageids] - images.each_with_index.reverse_each do |image, i| - img = AlbumImage.find(image) rescue nil - if !img.nil? - img.order = i - img.save - end - end - if album - album.update(:min_order=>-1) - end - elsif type == "orbit_gallery" - albums = params[:imageids] - albums.each_with_index.reverse_each do |albumid, i| - album = Album.find(albumid) rescue nil - if !album.nil? - album.order = i - album.save - end - end - AlbumSetting.update_all(:min_order=>-1) - end - render :json => {"success" => true}.to_json - end + def changeorder + type = params[:type] + if type == "imgholder" + album = Album.find(params[:album_id]) rescue nil + images = params[:imageids] + operations = [] + images.each_with_index.reverse_each do |image, i| + next if image.blank? + oid = begin; BSON::ObjectId(image); rescue; nil; end + next if oid.nil? + operations << { + update_one: { + filter: { _id: oid }, + update: { "$set" => { order: i } } + } + } + end + AlbumImage.collection.bulk_write(operations) if operations.any? + if album + album.update(:min_order=>-1) + end + elsif type == "orbit_gallery" + albums = params[:imageids] + operations = [] + albums.each_with_index.reverse_each do |albumid, i| + next if albumid.blank? + oid = begin; BSON::ObjectId(albumid); rescue; nil; end + next if oid.nil? + operations << { + update_one: { + filter: { _id: oid }, + update: { "$set" => { order: i } } + } + } + end + Album.collection.bulk_write(operations) if operations.any? + AlbumSetting.update_all(:min_order=>-1) + end + render :json => {"success" => true}.to_json + end def delete_photos images = params['images']