From d6da2bb587a1ca98587998beb9497c8abee0bb82 Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 23 Oct 2019 13:54:31 +0800 Subject: [PATCH 1/2] fix? --- app/controllers/admin/galleries_controller.rb | 41 ++++++++----------- app/models/album_unprocess.rb | 7 ++++ 2 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 app/models/album_unprocess.rb diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index b5d0a27..8548a22 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -345,52 +345,47 @@ class Admin::GalleriesController < OrbitAdminController def upload_process - if @@upload_success == true - count = @@image_unprocessed.length + if $upload_success == true + count = AlbumUnprocess.all.count Thread.new do begin @@start = true - @@image_unprocessed.each_with_index do |image,i| - @@progress_filename = @@file[i].original_filename - image.file = @@file[i] - image.save! + album_unprocess = AlbumUnprocess.all + album_unprocess.each_with_index do |image,i| + @@progress_filename = image.file.original_filename + album = AlbumImage.where(id: image.album_id) + album.file = image.file + album.save! @@progress_percent = ((i+1)*100.0/count).floor.to_s + '%' end + AlbumUnprocess.delete_all rescue => e puts e.inspect end - @@file = [] - @@image_unprocessed = [] - @upload_success = false + $upload_success = false @@finish = true end end end def start_upload_process - @@upload_success = true - sleep 0.01 + $upload_success = true render :json => {}.to_json end def init_upload - @@image_unprocessed = [] @@start = false @@finish = false - @@file = [] @@progress_percent = '0%' @@progress_filename = 'processing!!' render :json => {}.to_json end def upload_image - sleep 0.01 - if !(@@image_unprocessed.nil?) - album = Album.find(params[:album_id]) - files = params['files'] - files.each do |file| - image = album.album_images.new - @@file << file - image.tags = (album.tags rescue []) - @@image_unprocessed << image - end + album = Album.find(params[:album_id]) + files = params['files'] + files.each do |file| + image = album.album_images.new + image.tags = (album.tags rescue []) + AlbumUnprocess.create(album_id:image.id,file:file) + image.save! end render :json=>{"files"=>[{}]}.to_json end diff --git a/app/models/album_unprocess.rb b/app/models/album_unprocess.rb new file mode 100644 index 0000000..d588346 --- /dev/null +++ b/app/models/album_unprocess.rb @@ -0,0 +1,7 @@ +class AlbumUnprocess + include Mongoid::Document + include Mongoid::Timestamps + + field :album_id + field :file +end \ No newline at end of file From f1cdd98ee72c5ed1eb4919b1273787d412b543da Mon Sep 17 00:00:00 2001 From: chiu Date: Fri, 25 Oct 2019 08:58:51 +0800 Subject: [PATCH 2/2] reverse uplaod feature --- app/assets/javascripts/gallery.js | 131 ++++++++++++------ app/controllers/admin/galleries_controller.rb | 36 +++-- 2 files changed, 113 insertions(+), 54 deletions(-) diff --git a/app/assets/javascripts/gallery.js b/app/assets/javascripts/gallery.js index 7321357..cc6c9d1 100644 --- a/app/assets/javascripts/gallery.js +++ b/app/assets/javascripts/gallery.js @@ -1,3 +1,94 @@ +function form_submit(e) { + //var form_submit1 = $('form#fileupload'); + //var url_submit = form_submit1.attr('action'); + var temp_length = $('#file-list').find('li.template-upload').length + $.ajax({ + type : "post", + url : "/admin/galleries/init_upload", + dataType : "json", + data:{all_length: temp_length}, + async: false, + success: function() + { + }, + error : function(data){ + alert('init upload process failed, please try again later.') + } + }); + var length_upload + var count_upload + var send_start + $( 'form#fileupload' ).ajaxSuccess(function() { + if (typeof length_upload == "undefined"){ + count_upload = 1 + length_upload = $('#file-list').find('li.template-upload').length + } + else{ + count_upload ++ + } + console.log('l') + console.log(length_upload) + console.log(count_upload) + if (count_upload === length_upload){ + send_start = undefined + $.ajax({ + url : "/admin/galleries/start_upload_process", + dataType : "json", + type : "post", + error: function(){ + alert('init upload process failed, please try again later.') + }, + success: function(){ + window.location.href = '/admin/galleries/upload_process' + } + }) + } + }); +} +function form_only_one_submit(e) { + //var form_submit1 = $('form#fileupload'); + //var url_submit = form_submit1.attr('action'); + var temp_length = 1 + $.ajax({ + type : "post", + url : "/admin/galleries/init_upload", + dataType : "json", + data:{all_length: temp_length}, + async: false, + success: function() + { + }, + error : function(data){ + alert('init upload process failed, please try again later.') + } + }); + var length_upload + var count_upload + var send_start + $( 'form#fileupload' ).ajaxSuccess(function() { + if (typeof length_upload == "undefined"){ + count_upload = 1 + length_upload = 1 + } + else{ + count_upload ++ + } + if (count_upload === length_upload){ + send_start = undefined + $.ajax({ + url : "/admin/galleries/start_upload_process", + dataType : "json", + type : "post", + error: function(){ + alert('init upload process failed, please try again later.') + }, + success: function(){ + window.location.href = '/admin/galleries/upload_process' + } + }) + } + }); +} !function ($) { $.fn.checkListLength = function (param){ _defaultSettings = { @@ -81,45 +172,7 @@ function translate(ele,pretext,text){ } } $(function() { - var length_upload - var count_upload - var send_start - $( 'form#fileupload' ).ajaxSuccess(function() { - if (typeof length_upload == "undefined"){ - count_upload = 1 - length_upload = $('#file-list').find('li.template-upload').length - } - else{ - count_upload ++ - } - if (count_upload === length_upload){ - send_start = undefined - $.ajax({ - url : "/admin/galleries/start_upload_process", - dataType : "json", - type : "post", - error: function(){ - alert('init upload process failed, please try again later.') - }, - success: function(){ - window.location.href = '/admin/galleries/upload_process' - } - }) - } - }); - $( 'form#fileupload' ).ajaxSend(function() { - if (typeof send_start == "undefined"){ - send_start = 1 - $.ajax({ - url : "/admin/galleries/init_upload", - dataType : "json", - type : "post", - error : function(data){ - alert('init upload process failed, please try again later.') - } - }) - } - }); + var $container = $('.gallery'), $containerData = $container.data(); $container.data("order-edit","0"); diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 8548a22..4868f6d 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -139,10 +139,14 @@ class Admin::GalleriesController < OrbitAdminController render :text => '' end def recreate_progress - if @@start - render :json => {'percent' => (@@progress_percent rescue '0%'), 'filename' => (@@progress_filename rescue ''), 'finish' => (@@finish rescue false) }.to_json + if self.class.class_variable_defined? :@@start + if @@start + render :json => {'percent' => (@@progress_percent rescue '0%'), 'filename' => (@@progress_filename rescue ''), 'finish' => (@@finish rescue false) }.to_json + else + render :json => {'percent' => '0%', 'filename' => '', 'finish' => false }.to_json + end else - render :json => {'percent' => '0%', 'filename' => '', 'finish' => false }.to_json + render :json => {'percent' => '0%', 'filename' => '', 'finish' => false }.to_json end end def index @@ -345,35 +349,37 @@ class Admin::GalleriesController < OrbitAdminController def upload_process - if $upload_success == true - count = AlbumUnprocess.all.count + if @@upload_success == true + count = @@image_unprocessed.length Thread.new do begin @@start = true - album_unprocess = AlbumUnprocess.all - album_unprocess.each_with_index do |image,i| - @@progress_filename = image.file.original_filename - album = AlbumImage.where(id: image.album_id) - album.file = image.file - album.save! + @@image_unprocessed.each_with_index do |image,i| + @@progress_filename = @@file[i].original_filename + image.file = @@file[i] + image.save! @@progress_percent = ((i+1)*100.0/count).floor.to_s + '%' end - AlbumUnprocess.delete_all rescue => e puts e.inspect end - $upload_success = false + @@file = [] + @@image_unprocessed = [] + @upload_success = false @@finish = true end end end def start_upload_process - $upload_success = true + @@upload_success = true + sleep 0.01 render :json => {}.to_json end def init_upload + @@image_unprocessed = [] @@start = false @@finish = false + @@file = [] @@progress_percent = '0%' @@progress_filename = 'processing!!' render :json => {}.to_json @@ -383,8 +389,8 @@ class Admin::GalleriesController < OrbitAdminController files = params['files'] files.each do |file| image = album.album_images.new + image.file = file image.tags = (album.tags rescue []) - AlbumUnprocess.create(album_id:image.id,file:file) image.save! end render :json=>{"files"=>[{}]}.to_json