fix multiple thread processing picture feature

This commit is contained in:
chiu 2019-10-26 15:36:04 +08:00
parent f1cdd98ee7
commit 03510c38da
6 changed files with 53 additions and 63 deletions

View File

@ -1,6 +1,4 @@
function form_submit(e) { function form_submit() {
//var form_submit1 = $('form#fileupload');
//var url_submit = form_submit1.attr('action');
var temp_length = $('#file-list').find('li.template-upload').length var temp_length = $('#file-list').find('li.template-upload').length
$.ajax({ $.ajax({
type : "post", type : "post",
@ -26,9 +24,6 @@ function form_submit(e) {
else{ else{
count_upload ++ count_upload ++
} }
console.log('l')
console.log(length_upload)
console.log(count_upload)
if (count_upload === length_upload){ if (count_upload === length_upload){
send_start = undefined send_start = undefined
$.ajax({ $.ajax({
@ -45,9 +40,7 @@ function form_submit(e) {
} }
}); });
} }
function form_only_one_submit(e) { function form_only_one_submit() {
//var form_submit1 = $('form#fileupload');
//var url_submit = form_submit1.attr('action');
var temp_length = 1 var temp_length = 1
$.ajax({ $.ajax({
type : "post", type : "post",
@ -62,32 +55,6 @@ function form_only_one_submit(e) {
alert('init upload process failed, please try again later.') 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 ($) { !function ($) {
$.fn.checkListLength = function (param){ $.fn.checkListLength = function (param){

View File

@ -349,37 +349,43 @@ class Admin::GalleriesController < OrbitAdminController
def upload_process def upload_process
if @@upload_success == true if AlbumUnprocess.first.upload_success
count = @@image_unprocessed.length album_unprocess = AlbumUnprocess.all
count = album_unprocess.count
Thread.new do Thread.new do
begin begin
@@start = true @@start = true
@@image_unprocessed.each_with_index do |image,i| album_unprocess.each_with_index do |un_image,i|
@@progress_filename = @@file[i].original_filename album = AlbumImage.all.select{|value| value.id.to_s == un_image.image_id.to_s}[0]
image.file = @@file[i] album.file = un_image.save_var
image.save! 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 + '%' @@progress_percent = ((i+1)*100.0/count).floor.to_s + '%'
end end
rescue => e rescue => e
puts e.inspect puts ['err',e.inspect]
end end
@@file = []
@@image_unprocessed = []
@upload_success = false
@@finish = true @@finish = true
end end
album_temp = AlbumUnprocess.first
album_temp.upload_success = false
album_temp.save!
end end
end end
def start_upload_process def start_upload_process
@@upload_success = true album_temp = AlbumUnprocess.first
sleep 0.01 album_temp.upload_success = true
album_temp.save!
render :json => {}.to_json render :json => {}.to_json
end end
def init_upload def init_upload
@@image_unprocessed = [] Thread.current['count'] = params['all_length'].to_i
@@start = false @@start = false
@@finish = false @@finish = false
@@file = []
@@progress_percent = '0%' @@progress_percent = '0%'
@@progress_filename = 'processing!!' @@progress_filename = 'processing!!'
render :json => {}.to_json render :json => {}.to_json
@ -387,10 +393,19 @@ class Admin::GalleriesController < OrbitAdminController
def upload_image def upload_image
album = Album.find(params[:album_id]) album = Album.find(params[:album_id])
files = params['files'] files = params['files']
album_unprocess = AlbumUnprocess.new()
files.each do |file| files.each do |file|
image = album.album_images.new image = album.album_images.new
image.file = file
image.tags = (album.tags rescue []) 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! image.save!
end end
render :json=>{"files"=>[{}]}.to_json render :json=>{"files"=>[{}]}.to_json

View File

@ -1,11 +1,9 @@
require 'net/http' require 'net/http'
class AlbumImage class AlbumImage
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include OrbitTag::Taggable include OrbitTag::Taggable
mount_uploader :file, GalleryUploader mount_uploader :file, GalleryUploader
field :title field :title
field :description, localize: true field :description, localize: true
field :rss2_id, type: String field :rss2_id, type: String

View File

@ -1,7 +1,18 @@
class AlbumUnprocess class AlbumUnprocess
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps field :image_id
field :save_var, type: String
field :album_id field :upload_success, type: Boolean
field :file 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 end

View File

@ -10,7 +10,6 @@ module CarrierWave
end end
class GalleryUploader < CarrierWave::Uploader::Base class GalleryUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support: # Include RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # include CarrierWave::ImageScience

View File

@ -45,7 +45,7 @@
<input type="hidden" value="<%= @album.id.to_s %>" name="album_id" id="fileupload_aid" /> <input type="hidden" value="<%= @album.id.to_s %>" name="album_id" id="fileupload_aid" />
</li> </li>
<li> <li>
<button type="submit" class="start add-photo"> <button type="submit" class="start add-photo" onclick="form_submit()">
<i class="icon-upload icon-white"></i> <i class="icon-upload icon-white"></i>
<span><%= t('gallery.start_upload') %></span> <span><%= t('gallery.start_upload') %></span>
</button> </button>
@ -200,7 +200,7 @@
<div class="bar" style="width:0%;"></div> <div class="bar" style="width:0%;"></div>
</div> </div>
{% if (!o.options.autoUpload) { %} {% if (!o.options.autoUpload) { %}
<button class="btn btn-primary start"> <button class="btn btn-primary start" onclick="form_only_one_submit()">
<i class="icon-upload icon-white"></i> <i class="icon-upload icon-white"></i>
</button> </button>
{% } %} {% } %}