modified gallery import for server timeout

This commit is contained in:
Harry Bomrah 2014-07-09 18:35:55 +08:00
parent 44adbf57b2
commit 97d4a94583
3 changed files with 79 additions and 84 deletions
app
controllers/admin
views/admin/import
config

View File

@ -584,55 +584,49 @@ class Admin::ImportController < OrbitAdminController
def rss2_galleries
delete_import_file
uri = URI.parse(params['url'])
# @@thread = Thread.new do
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = response.body
data = JSON.parse(data)
@@import_stats["total_images"] = data["total_images"].to_i
@@import_stats["current_status"] = 0
@@import_stats["success"] = true
write_to_file
albums = data["albums"]
current_locale = I18n.locale
I18n.locale = :en
category = Category.where(:title => "RSS2 Gallery").first
I18n.locale = current_locale
if category.nil?
module_app = ModuleApp.find_by_key("gallery")
category = Category.new
category.title_translations = {"en" => "RSS2 Gallery", "zh_tw" => "RSS2畫廊"}
category.save
module_app.categories << category
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = response.body
data = JSON.parse(data)
albums = data["albums"]
current_locale = I18n.locale
I18n.locale = :en
category = Category.where(:title => "RSS2 Gallery").first
I18n.locale = current_locale
if category.nil?
module_app = ModuleApp.find_by_key("gallery")
category = Category.new
category.title_translations = {"en" => "RSS2 Gallery", "zh_tw" => "RSS2畫廊"}
category.save
module_app.categories << category
end
@data_to_send = []
albums.each do |album|
if Album.where(:rss2_id => album["albumid"]).count == 0
newalbum = Album.new(:name_translations => {"en" => album["name"]["en"], "zh_tw" => album["name"]["zh_tw"]}, :description_translations => {"en" => album["description"]["en"], "zh_tw" => album["description"]["zh_tw"]},"rss2_id" => album["albumid"])
newalbum.category_id = category.id
newalbum.save
else
newalbum = Album.where(:rss2_id => album["albumid"]).first
end
albums.each do |album|
@@import_stats["current_album_id"] = album["albumid"]
write_to_file
if Album.where(:rss2_id => album["albumid"]).count == 0
@@import_stats["current_album_name"] = album["name"]["zh_tw"]
write_to_file
newalbum = Album.new(:name_translations => {"en" => album["name"]["en"], "zh_tw" => album["name"]["zh_tw"]}, :description_translations => {"en" => album["description"]["en"], "zh_tw" => album["description"]["zh_tw"]},"rss2_id" => album["albumid"])
newalbum.category_id = category.id
newalbum.save
images = album["images"]
if !images.blank?
images.each do |image|
if AlbumImage.where(:rss2_id => image["photoid"]).count == 0
img = newalbum.album_images.new
img.description_translations = {"en" => image["description"]["en"], "zh_tw" => image["description"]["zh_tw"]}
img.remote_file_url = image["image"]
img.save
end
@@import_stats["current_status"] = @@import_stats["current_status"] + 1
write_to_file
end
end
end
end
# end
# @@thread.join
# @@thread.abort_on_exception = true
images = album["images"]
images.each do |image|
@data_to_send << {"album_id" => newalbum.id.to_s,"album_name" => newalbum.name_translations["zh_tw"], "url" => image["image"], "description" => {"en" => image["description"]["en"], "zh_tw" => image["description"]["zh_tw"]},"photoid" => image["photoid"]}
end
end
render :json => {"images" => @data_to_send}.to_json
end
def rss2_album_image
album = Album.find(params[:album_id])
if AlbumImage.where(:rss2_id => params["photoid"]).count == 0
img = album.album_images.new
img.description_translations = params["description"]
img.remote_file_url = params["url"]
img.save
end
render :json => {"success" => true}.to_json
end

View File

@ -245,24 +245,56 @@
})
startCheckinglinksImportStatus();
}
var gallery_data = null;
var importGallery = function(){
$("#import-progress .bar").css('width','0%');
$("#import-progress-text").html("Importing Galleries...<br/>");
$("#import-progress-text").html("Importing Galleries...<br/>").delay(1000).html("Creating albums...<br />");
$.ajax({
type: "post",
dataType: "json",
url: '<%= admin_import_rss2_galleries_path %>',
data : {"url" : url+'?module='+module}
}).done(function(){
}).done(function(data){
$("#import-progress-text").html("Importing Images...<br/>");
gallery_data = data.images;
importImagesForAlbums(0);
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
setTimeout("startCheckingGalleryImportStatus()",700);
}
var importImagesForAlbums = function(number){
var image = gallery_data[number],
total = gallery_data.length;
$("#import-progress-text").html("Importing Album " + image.album_name + "..." + (number + 1) + "/" + total + "<br/>");
$.ajax({
type: "post",
url: "<%= admin_import_rss2_album_image_path %>",
dataType : "json",
data : image
}).done(function(status){
if(status.success == true){
var percent = ((number + 1) * 100) / total;
$("#import-progress .bar").css('width',percent + "%");
if(number != (total - 1)){
setTimeout(importImagesForAlbums(number + 1),300)
}else{
$("#import-progress-text").html("Finished importing albums.");
}
}
}).fail(function(){
$("#import-progress-text").html("Error importing album " + image.album_name);
$("#alert-msg").text("Error importing album " + image.album_name);
$(".alert-danger").removeClass("hide").show();
})
}
var importMembers = function(){
$("#import-progress .bar").css('width', '0%');
$("#import-progress-text").html("Importing Member..<br/>");
@ -383,38 +415,6 @@
})
}
var startCheckingGalleryImportStatus = function(){
$.ajax({
type : "get",
dataType : "json",
data : {"module" : module},
url : "<%= admin_import_get_import_status_path %>"
}).done(function(status){
if(status.success == true){
if(status.total_images != null){
var percent = (status.current_status * 100) / status.total_images;
$("#import-progress .bar").css('width',percent + "%");
$("#import-progress-text").html("Importing album " + status.current_album_name + " .. " + status.current_status + "/" + status.total_images + "Images <br/>");
}
if(status.total_images != status.current_status){
setTimeout("startCheckingGalleryImportStatus()",700);
}else if(status.total_images == null){
setTimeout("startCheckingGalleryImportStatus()",700);
}else{
$("#import-progress-text").html("Finished importing gallery.<br/>");
}
}else if(status.success == false){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("Error importing album " + status.current_album_name + " ID : " + status.current_album_id)
$(".alert-danger").removeClass("hide").show();
}
}).fail(function(){
$("#import-progress-text").html("Importing failed, click on start to try again.");
$("#alert-msg").text("There was an unknown error");
$(".alert-danger").removeClass("hide").show();
})
}
var importModule = function(m){
module = m;
progress = 0;

View File

@ -220,6 +220,7 @@ Orbit::Application.routes.draw do
post "import/rss2_pages" => "import#rss2_pages"
get "import/get_import_status" => "import#get_import_status"
post "import/rss2_galleries" => "import#rss2_galleries"
post "import/rss2_album_image" => "import#rss2_album_image"
post "import/rss2_links" => "import#rss2_links"
post "import/rss2_members" => "import#rss2_members"
end