502 lines
16 KiB
Ruby
502 lines
16 KiB
Ruby
require 'rubyXL'
|
|
class Admin::GalleriesController < OrbitAdminController
|
|
include Admin::GalleriesHelper
|
|
before_filter :setup_vars
|
|
before_action :authenticate_user, :except => "imgs"
|
|
before_action :log_user_action
|
|
find_tag = Tag.all.select{|value| value.name==I18n.t('gallery.not_show_desc')}
|
|
if find_tag.length==0
|
|
module_app_id = ModuleApp.where(:key=>"gallery").first[:_id]
|
|
tags = ModuleApp.where(:key=>"gallery").first.tags
|
|
tag0 = Tag.new(is_default: false,module_app_ids: [module_app_id])
|
|
tag1 = Tag.new(is_default: false,module_app_ids: [module_app_id])
|
|
nowlocale = I18n.locale
|
|
I18n.available_locales.each do |locale|
|
|
I18n.locale = locale
|
|
tag0.name = I18n.t('gallery.show_desc')
|
|
tag1.name = I18n.t('gallery.not_show_desc')
|
|
end
|
|
I18n.locale = nowlocale
|
|
tag0.save
|
|
tag1.save
|
|
tags << tag0
|
|
tags << tag1
|
|
end
|
|
def save_crop
|
|
begin
|
|
images = AlbumImage.all.select{|value| params[:id].include? value.id.to_s}
|
|
id = images.first.album_id.to_s
|
|
x = params['x']
|
|
y = params['y']
|
|
w = params['w']
|
|
h = params['h']
|
|
cords = x.zip(y,w,h)
|
|
count = cords.length
|
|
images.each_with_index do |image,index|
|
|
cord = cords[index]
|
|
if image.album_crops.first.nil?
|
|
image.album_crops.create(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
|
|
else
|
|
image.album_crops.first.update_attributes(crop_x: cord[0],crop_y: cord[1],crop_w: cord[2],crop_h: cord[3])
|
|
end
|
|
end
|
|
@@finish = false
|
|
@@start = false
|
|
Thread.new do
|
|
@@start = true
|
|
images.each_with_index do |image,index|
|
|
@@progress_percent = (index*100.0/count).floor.to_s+'%'
|
|
all_version = image.file.versions.map{|k,v| k}
|
|
begin
|
|
#org_fname = image.file.path
|
|
#org_extname = File.extname(org_fname)
|
|
#org_fname.slice! org_extname
|
|
#FileUtils.cp(org_fname + org_extname, org_fname + '_resized' + org_extname)
|
|
@@progress_filename = image[:file].to_s
|
|
all_version.each do |version|
|
|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
|
|
image.file.recreate_versions! version
|
|
image.save!
|
|
end
|
|
end
|
|
rescue => e
|
|
@@progress_filename = e.inspect.to_s
|
|
puts e.inspect
|
|
end
|
|
end
|
|
@@finish = true
|
|
end
|
|
rescue => e
|
|
puts e.inspect
|
|
end
|
|
redirect_url = "/admin/galleries/crop_process"
|
|
render :json => {'href' => redirect_url}.to_json
|
|
end
|
|
def call_translate
|
|
text = params['text']
|
|
render :json => {'translate' => "#{t(text.to_s)}" }.to_json
|
|
end
|
|
def recreate_image
|
|
notalive = @@progress_percent.nil? rescue true
|
|
if notalive
|
|
if !params['album_id'].to_s.empty?
|
|
choice_ids = params['album_id'].split(',')
|
|
albums = Album.all.select { |value| (choice_ids.include? value.id.to_s)}
|
|
if !(params['use_default']=='true')
|
|
color = params['color_choice'].to_s.empty? ? 'transparent' : params['color_choice']
|
|
albums.each do |album|
|
|
if album.album_colors.first.nil?
|
|
album.album_colors.create('color' => color)
|
|
else
|
|
album.album_colors.first.update_attributes('color' => color, 'updated_at' => Time.now)
|
|
end
|
|
end
|
|
end
|
|
@@start = false
|
|
count = 0
|
|
albums.each{|album| count+=album.album_images.count }
|
|
@@finish = false
|
|
Thread.new do
|
|
@@start = true
|
|
i = 0
|
|
albums.each do |album|
|
|
album.album_images.each do |image|
|
|
error = nil
|
|
all_version = image.file.versions.map{|k,v| k}
|
|
begin
|
|
all_version.each do |version|
|
|
if !(version.to_s == 'resized' && crop_but_no_backup(image))
|
|
image.file.recreate_versions! version
|
|
image.save!
|
|
end
|
|
end
|
|
rescue => error
|
|
end
|
|
@@progress_percent = (i*100.0/count).floor.to_s+'%'
|
|
if !error.nil?
|
|
@@progress_filename = error.inspect.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
|
|
sleep(1)
|
|
else
|
|
@@progress_filename = image[:file].to_s
|
|
end
|
|
i+=1
|
|
end
|
|
end
|
|
@@finish = true
|
|
end
|
|
else
|
|
@@start = true
|
|
@@progress_filename = ''
|
|
@@finish = true
|
|
@@progress_percent = '100%'
|
|
end
|
|
end
|
|
end
|
|
def finish_recreate
|
|
@@progress_percent = nil
|
|
@@progress_filename = nil
|
|
@@start = false
|
|
render :text => ''
|
|
end
|
|
def recreate_progress
|
|
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
|
|
end
|
|
end
|
|
def index
|
|
Album.each do |album|
|
|
if album.album_colors.first.nil?
|
|
album.album_colors.create('color' => '#000000')
|
|
end
|
|
end
|
|
@tags = @module_app.tags
|
|
categories = @module_app.categories.enabled
|
|
@filter_fields = filter_fields(categories, @tags)
|
|
@filter_fields.delete(:status)
|
|
@albums = Album.where(:order => -1).desc(:created_at).with_categories(filters("category")).with_tags(filters("tag"))
|
|
@albums = search_data(@albums,[:name])
|
|
albums = Album.where(:order.gt => -1).asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
|
|
albums = search_data(albums,[:name])
|
|
@albums = @albums.concat(albums)
|
|
if AlbumColor.count!=0
|
|
if AlbumColor.all.desc('updated_at').first[:color] == 'transparent'
|
|
@color_save = ''
|
|
else
|
|
@color_save = AlbumColor.desc('updated_at').first[:color]
|
|
end
|
|
else
|
|
@color_save = '#000000'
|
|
end
|
|
if request.xhr?
|
|
render :partial => "album", :collection => @albums
|
|
end
|
|
|
|
end
|
|
|
|
def show
|
|
@album = Album.find(params[:id])
|
|
@images = @album.album_images.where(:order => -1).desc(:created_at)
|
|
images = @album.album_images.where(:order.gt => -1).asc(:order)
|
|
@images = @images.concat(images)
|
|
image_content = []
|
|
@images.each do |image|
|
|
image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}}
|
|
end
|
|
@tags = @module_app.tags
|
|
@photos_data = {"galleries" => image_content}.to_json
|
|
end
|
|
|
|
def new
|
|
@album = Album.new
|
|
@tags = @module_app.tags
|
|
@categories = @module_app.categories
|
|
end
|
|
|
|
def create
|
|
album = Album.new(album_params)
|
|
album.save!
|
|
redirect_to admin_galleries_path
|
|
end
|
|
|
|
def destroy
|
|
album = Album.find(params[:id])
|
|
album.destroy
|
|
redirect_to admin_galleries_path
|
|
end
|
|
|
|
def edit
|
|
@album = Album.find(params[:id])
|
|
if can_edit_or_delete?(@album)
|
|
@tags = @module_app.tags
|
|
@categories = @module_app.categories
|
|
else
|
|
render_401
|
|
end
|
|
end
|
|
|
|
def import
|
|
@album = Album.find(params[:id])
|
|
end
|
|
|
|
def importimages
|
|
album = Album.find(params[:id])
|
|
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
|
|
sheet = workbook[0]
|
|
if sheet.count <= 203
|
|
sheet.each_with_index do |row, i|
|
|
next if i < 3
|
|
v = row.cells.first.value
|
|
next if v == "" || v.nil?
|
|
import_this_image(row, album)
|
|
end
|
|
redirect_to admin_gallery_path(album.id)
|
|
else
|
|
redirect_to import_admin_gallery_path(:error => "1")
|
|
end
|
|
end
|
|
|
|
def excel_format
|
|
@album = Album.find(params[:id])
|
|
respond_to do |format|
|
|
format.xlsx {
|
|
response.headers['Content-Disposition'] = 'attachment; filename="gallery_import_format.xlsx"'
|
|
}
|
|
end
|
|
end
|
|
|
|
def set_cover
|
|
if params[:set_cover] == "true"
|
|
album = Album.find(params[:album_id])
|
|
image = AlbumImage.find(params[:image_id])
|
|
album.update_attributes({:cover_path => image.file.thumb.url, :cover=>params[:image_id]})
|
|
else
|
|
album = Album.find(params[:album_id])
|
|
album.update_attributes({:cover_path => nil, :cover=>"default"})
|
|
end
|
|
render :json =>{"success"=>true}.to_json
|
|
end
|
|
|
|
def get_photoData_json
|
|
@album = Album.find(params[:id])
|
|
@images = @album.album_images.where(:order => -1).desc(:created_at)
|
|
images = @album.album_images.where(:order.gt => -1).asc(:order)
|
|
@images = @images.concat(images)
|
|
image_content = []
|
|
@images.each do |image|
|
|
image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}}
|
|
end
|
|
render :json=>{"galleries" => image_content}.to_json
|
|
end
|
|
|
|
def get_album_json
|
|
albums = Album.all
|
|
output = Array.new
|
|
|
|
albums.each do |album|
|
|
tag_names = Array.new
|
|
|
|
album.tag_ids.each do |tag|
|
|
tag_names << get_tags.include?(tag)
|
|
end
|
|
if album.cover_path
|
|
cover_path = album.cover_path
|
|
else
|
|
cover_path = "/assets/gallery/default.jpg"
|
|
end
|
|
output << {
|
|
album_cover_file: "http://#{request.host_with_port}"+cover_path,
|
|
album_name: album.name,
|
|
album_tag_names: tag_names,
|
|
album_link:"http://#{request.host_with_port}#{panel_gallery_back_end_album_get_imgs_json_path(album)}",
|
|
}
|
|
end
|
|
|
|
render :json=>JSON.pretty_generate(output)
|
|
end
|
|
|
|
def get_imgs_json
|
|
album = Album.find(params[:album_id])
|
|
images = album.album_images.all
|
|
output = Array.new
|
|
|
|
images.each do |image|
|
|
tags = Array.new
|
|
|
|
image.tag_ids.each do |tag|
|
|
tags << get_tags.include?(tag)
|
|
end
|
|
|
|
if image.file.theater.present?
|
|
@image_file = image.file.theater.url
|
|
else
|
|
@image_file = image.file.url
|
|
end
|
|
|
|
output << {
|
|
image_title: image.title,
|
|
image_description: image.description,
|
|
image_file: { url: "http://#{request.host_with_port}#{@image_file}",
|
|
thumb: "http://#{request.host_with_port}#{image.file.thumb.to_s}"},
|
|
image_tag_names: tags}
|
|
end
|
|
|
|
render :json=>JSON.pretty_generate(output)
|
|
end
|
|
|
|
def imgs
|
|
@album = Album.find(params[:gallery_id])
|
|
@tag_names = Array.new
|
|
@images = @album.album_images.asc(:order)
|
|
@output = Array.new
|
|
@images.each do |values|
|
|
@output << { _id: values.id.to_s,
|
|
theater_link: admin_image_path(values),
|
|
description: values.description,
|
|
title: values.title,
|
|
file: values.file.as_json[:file],
|
|
gallery_album_id: values.album_id,
|
|
order: values.order,
|
|
tags: values.tags}
|
|
end
|
|
render :json=>{"images" => @output, "tags" => @album.tags}.to_json
|
|
end
|
|
|
|
|
|
def upload_process
|
|
if AlbumUnprocess.first.upload_success
|
|
album_unprocess = AlbumUnprocess.all
|
|
count = album_unprocess.count
|
|
Thread.new do
|
|
begin
|
|
@@start = true
|
|
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 ['err',e.inspect]
|
|
end
|
|
@@finish = true
|
|
end
|
|
album_temp = AlbumUnprocess.first
|
|
album_temp.upload_success = false
|
|
album_temp.save!
|
|
end
|
|
end
|
|
def start_upload_process
|
|
album_temp = AlbumUnprocess.first
|
|
album_temp.upload_success = true
|
|
album_temp.save!
|
|
render :json => {}.to_json
|
|
end
|
|
def init_upload
|
|
Thread.current['count'] = params['all_length'].to_i
|
|
@@start = false
|
|
@@finish = false
|
|
@@progress_percent = '0%'
|
|
@@progress_filename = 'processing!!'
|
|
render :json => {}.to_json
|
|
end
|
|
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.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
|
|
end
|
|
|
|
def last_image_id
|
|
album = Album.find(params[:albumid])
|
|
lastimage = album.album_images.last
|
|
render :json => {"last_image_id" => lastimage.id.to_s}.to_json
|
|
end
|
|
|
|
def new_images
|
|
@album = Album.find(params[:album_id])
|
|
if params[:last_image_id].present?
|
|
lastimage = AlbumImage.find(params[:last_image_id])
|
|
@newimages = @album.album_images.where(:created_at.gt => lastimage.created_at,:order => -1).desc(:created_at)
|
|
else
|
|
@newimages = @album.album_images.where(:order => -1).desc(:created_at)
|
|
end
|
|
render :layout=>false
|
|
end
|
|
|
|
|
|
def images_tags
|
|
album = Album.find(params[:album_id])
|
|
tags = Array.new
|
|
images = album.album_images.all
|
|
images.each do |image|
|
|
tags << {"id"=>image.id, "tags" => image.tags}
|
|
end
|
|
render :json=>tags.to_json
|
|
end
|
|
|
|
def update
|
|
album = Album.find(params[:id])
|
|
tagsToDestroy = []
|
|
tagsToAdd = []
|
|
new_tags = params[:album][:tags] || []
|
|
old_tags = album.tags.collect{|t| t.id.to_s}
|
|
old_tags.each do |tag|
|
|
if !new_tags.include?(tag)
|
|
tagsToDestroy << tag
|
|
end
|
|
end
|
|
if new_tags != nil
|
|
new_tags.each do |tag|
|
|
if !old_tags.include?(tag)
|
|
tagsToAdd << tag
|
|
end
|
|
end
|
|
end
|
|
update_children_image_tag(album,tagsToDestroy,tagsToAdd)
|
|
album.update_attributes(album_params)
|
|
redirect_to "/admin/galleries/"+album.id.to_s
|
|
end
|
|
|
|
def update_children_image_tag(album,tagsToDestroy,tagsToAdd)
|
|
# tagsToDestroy will contain all tag ids which have to be deleted from the galley_images
|
|
# tagsToAdd will contain all tag ids which ve to be added in tall album_images
|
|
images = album.album_images
|
|
images.each do |image|
|
|
image_tags = image.tags.collect{|t| t.id.to_s}
|
|
tagsToAdd.each do |tag|
|
|
image_tags << tag
|
|
end
|
|
tagsToDestroy.each do |tag|
|
|
if image_tags.include?tag
|
|
image_tags.delete(tag)
|
|
end
|
|
end
|
|
image.tags = image_tags
|
|
image.save
|
|
end
|
|
end
|
|
|
|
private
|
|
def crop_but_no_backup image
|
|
fname = image.file.path
|
|
extension = File.extname(fname)
|
|
base_name = fname.chomp(extension)
|
|
base_name += ('_resized'+ extension)
|
|
File.file?(base_name)
|
|
end
|
|
def setup_vars
|
|
@module_app = ModuleApp.where(:key=>"gallery").first
|
|
end
|
|
|
|
def album_params
|
|
p = params.require(:album).permit!
|
|
p["tags"] = p["tags"] || []
|
|
p
|
|
end
|
|
end
|