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) {
//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){

View File

@ -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
@@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

View File

@ -3,9 +3,7 @@ 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

View File

@ -1,7 +1,18 @@
class AlbumUnprocess
include Mongoid::Document
include Mongoid::Timestamps
field :album_id
field :file
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

View File

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

View File

@ -45,7 +45,7 @@
<input type="hidden" value="<%= @album.id.to_s %>" name="album_id" id="fileupload_aid" />
</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>
<span><%= t('gallery.start_upload') %></span>
</button>
@ -200,7 +200,7 @@
<div class="bar" style="width:0%;"></div>
</div>
{% 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>
</button>
{% } %}