diff --git a/app/assets/javascripts/gallery.js b/app/assets/javascripts/gallery.js index cc6c9d1..26f7ca5 100644 --- a/app/assets/javascripts/gallery.js +++ b/app/assets/javascripts/gallery.js @@ -1,6 +1,4 @@ -function form_submit(e) { - //var form_submit1 = $('form#fileupload'); - //var url_submit = form_submit1.attr('action'); +function form_submit() { var temp_length = $('#file-list').find('li.template-upload').length $.ajax({ type : "post", @@ -26,9 +24,6 @@ function form_submit(e) { else{ count_upload ++ } - console.log('l') - console.log(length_upload) - console.log(count_upload) if (count_upload === length_upload){ send_start = undefined $.ajax({ @@ -45,9 +40,7 @@ function form_submit(e) { } }); } -function form_only_one_submit(e) { - //var form_submit1 = $('form#fileupload'); - //var url_submit = form_submit1.attr('action'); +function form_only_one_submit() { var temp_length = 1 $.ajax({ type : "post", @@ -62,32 +55,6 @@ function form_only_one_submit(e) { 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){ diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 4868f6d..0f70ba9 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -349,37 +349,43 @@ class Admin::GalleriesController < OrbitAdminController def upload_process - if @@upload_success == true - count = @@image_unprocessed.length + if AlbumUnprocess.first.upload_success + album_unprocess = AlbumUnprocess.all + count = album_unprocess.count Thread.new do - begin + 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.each_with_index do |un_image,i| + 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 + @@progress_filename = album[:file] @@progress_percent = ((i+1)*100.0/count).floor.to_s + '%' end rescue => e - puts e.inspect + puts ['err',e.inspect] end - @@file = [] - @@image_unprocessed = [] - @upload_success = false @@finish = true end + album_temp = AlbumUnprocess.first + album_temp.upload_success = false + album_temp.save! end end def start_upload_process - @@upload_success = true - sleep 0.01 + album_temp = AlbumUnprocess.first + album_temp.upload_success = true + album_temp.save! render :json => {}.to_json end def init_upload - @@image_unprocessed = [] + Thread.current['count'] = params['all_length'].to_i @@start = false @@finish = false - @@file = [] @@progress_percent = '0%' @@progress_filename = 'processing!!' render :json => {}.to_json @@ -387,10 +393,19 @@ class Admin::GalleriesController < OrbitAdminController def upload_image album = Album.find(params[:album_id]) files = params['files'] + album_unprocess = AlbumUnprocess.new() files.each do |file| image = album.album_images.new - image.file = file image.tags = (album.tags rescue []) + if Thread.current['count']==1 + image.file = file + else + album_unprocess.image_id = image.id + album_unprocess.upload_success = false + album_unprocess.save_var = file + album_unprocess.save! + ObjectSpace.undefine_finalizer(file.tempfile) + end image.save! end render :json=>{"files"=>[{}]}.to_json diff --git a/app/models/album_image.rb b/app/models/album_image.rb index b08ccc5..17cb56a 100644 --- a/app/models/album_image.rb +++ b/app/models/album_image.rb @@ -1,11 +1,9 @@ require 'net/http' -class AlbumImage +class AlbumImage include Mongoid::Document include Mongoid::Timestamps include OrbitTag::Taggable - mount_uploader :file, GalleryUploader - field :title field :description, localize: true field :rss2_id, type: String diff --git a/app/models/album_unprocess.rb b/app/models/album_unprocess.rb index d588346..1cfa701 100644 --- a/app/models/album_unprocess.rb +++ b/app/models/album_unprocess.rb @@ -1,7 +1,18 @@ -class AlbumUnprocess - include Mongoid::Document - include Mongoid::Timestamps - - field :album_id - field :file +class AlbumUnprocess + include Mongoid::Document + field :image_id + field :save_var, type: String + field :upload_success, type: Boolean + def save_var + temp = YAML.load(self[:save_var]) + temp[:tempfile] = File.open(temp[:tempfile]) + ActionDispatch::Http::UploadedFile.new(temp) + end + def save_var=(value) + temp = {:filename => value.original_filename, + :type => value.content_type, + :head => value.headers, + :tempfile => value.tempfile.path} + self[:save_var] = temp.to_yaml + end end \ No newline at end of file diff --git a/app/uploaders/gallery_uploader.rb b/app/uploaders/gallery_uploader.rb index 25b0901..f66b08a 100644 --- a/app/uploaders/gallery_uploader.rb +++ b/app/uploaders/gallery_uploader.rb @@ -10,7 +10,6 @@ module CarrierWave end class GalleryUploader < CarrierWave::Uploader::Base - # Include RMagick or ImageScience support: # include CarrierWave::RMagick # include CarrierWave::ImageScience diff --git a/app/views/admin/galleries/show.html.erb b/app/views/admin/galleries/show.html.erb index 35b2615..19a6312 100644 --- a/app/views/admin/galleries/show.html.erb +++ b/app/views/admin/galleries/show.html.erb @@ -45,7 +45,7 @@