gallery/app/controllers/admin/galleries_controller.rb

681 lines
21 KiB
Ruby
Raw Normal View History

2016-12-20 11:43:54 +00:00
require 'rubyXL'
2014-07-31 09:12:57 +00:00
class Admin::GalleriesController < OrbitAdminController
2016-12-20 11:43:54 +00:00
include Admin::GalleriesHelper
before_filter :setup_vars
2014-10-20 11:46:03 +00:00
before_action :authenticate_user, :except => "imgs"
before_action :log_user_action
2020-02-04 08:18:36 +00:00
layout :compute_layout
find_tag = Tag.all.select{|value| value.name==I18n.t('gallery.not_show_desc')}
2022-10-20 01:55:10 +00:00
RackTempMiddleExist = defined?(Rack::TempfileReaper)
2023-08-15 13:39:42 +00:00
RACK_TEMPFILES = defined?(Rack::RACK_TEMPFILES) ? Rack::RACK_TEMPFILES : 'rack.tempfiles'
Is_Rails5 = (Rails.version.to_f >= 5)
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
elsif find_tag.length>1
show_tags = Tag.all.select{|value| value.name==I18n.t('gallery.show_desc')}
show_tags.each_with_index do |show_tag,index1|
if index1>0
if show_tag.taggings.count==0
show_tag.delete
end
end
end
find_tag.each_with_index do |not_show_tag,index1|
if index1>0
if not_show_tag.taggings.count==0
not_show_tag.delete
end
end
end
end
2023-08-15 13:39:42 +00:00
if RackTempMiddleExist # Avoid Rack::TempfileReaper Middleware to unlink temp files automatically.
if Is_Rails5
def fix_tempfile_reaper
request.set_header(RACK_TEMPFILES, [])
end
else
def fix_tempfile_reaper
env[RACK_TEMPFILES] = nil
end
end
else
def fix_tempfile_reaper #do nothing for old rack
end
end
2020-02-04 08:18:36 +00:00
def compute_layout
if action_name== 'index' && !params['page_no'].nil?
false
else
'back_end'
end
end
def get_tag
album = Album.find(params['ele_id']) rescue nil
if !album.nil?
text = album.tags.collect do |tag|
"<li class=\"label\"> #{tag.name}</li>"
end.join
2022-10-04 10:43:24 +00:00
render :plain => text
else
2022-10-04 10:43:24 +00:00
render :plain => ''
end
end
def rotate_images
begin
image_ids = params['image_ids'].split(',')
2020-04-25 02:48:14 +00:00
count = image_ids.count
rot_ang = params[:rotate_angle].to_i
2020-04-25 02:48:14 +00:00
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
Thread.new do
variable = AlbumVariable.first
image_ids.each_with_index do |image_id,index|
image = AlbumImage.find(image_id) rescue nil
next if image.nil?
if !(image.album_crops.first.nil?)
w_and_h = image.file.get_w_and_h
cords = image.album_crops.map{|v| [[v.crop_x.to_f,v.crop_y.to_f],
[v.crop_x.to_f,v.crop_y.to_f+v.crop_h.to_f],
[v.crop_w.to_f+v.crop_x.to_f,v.crop_y.to_f],
[v.crop_w.to_f+v.crop_x.to_f,v.crop_y.to_f+v.crop_h.to_f]]}[0]
rot_diff = rot_ang/90
if (rot_diff) % 2 == 1
new_w = (w_and_h[1]).to_f
new_h = (w_and_h[0]).to_f
else
new_w = (w_and_h[0]).to_f
new_h = (w_and_h[1]).to_f
end
rot_times = rot_diff % 4
deg = (90.0*rot_times/180*Math::PI)
cords = cords.collect do |cord|
x = cord[0]-w_and_h[0].to_f/2
y = cord[1]-w_and_h[1].to_f/2
[x*Math.cos(deg)-y*Math.sin(deg)+new_w/2,x*Math.sin(deg)+y*Math.cos(deg)+new_h/2]
end
all_x = cords.collect do |cord|
cord[0]
end
all_y = cords.collect do |cord|
cord[1]
end
image.album_crops.first.update_attributes(crop_x: all_x.min,crop_y: all_y.min,crop_w: all_x.max - all_x.min,crop_h: all_y.max - all_y.min)
end
variable.progress_percent = (index*100.0/count).floor.to_s+'%'
image.file.rotate_ang(rot_ang)
all_version = image.file.versions.map{|k,v| k}
begin
variable.progress_filename = image[:file].to_s
all_version.each do |version|
2023-04-24 02:44:50 +00:00
if !(version.to_s == 'resized' && crop_but_no_backup(image))
image.file.recreate_versions! version
image.save!
end
end
rescue => e
variable.progress_filename = e.inspect.to_s
puts e.inspect
end
variable.save!
end
variable.finish = true
variable.save!
2023-04-24 02:44:50 +00:00
end
rescue => e
2023-04-24 02:44:50 +00:00
puts e.inspect
end
2024-06-26 15:50:27 +00:00
redirect_to admin_galleries_crop_process_path
end
def save_crop
begin
2020-04-25 02:48:14 +00:00
images = AlbumImage.where(:id.in => Array(params[:id]))
x = params['x']
y = params['y']
w = params['w']
h = params['h']
cords = x.zip(y,w,h)
count = cords.length
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
Thread.new do
variable = AlbumVariable.first
2023-04-24 02:44:50 +00:00
image_ids= images.pluck(:id)
image_ids.each_with_index do |image_id,index|
image = AlbumImage.find(image_id)
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
variable.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)
variable.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
2023-04-24 02:44:50 +00:00
rescue => e
variable.progress_filename = e.inspect.to_s
puts e.inspect
end
variable.save!
end
variable.finish = true
variable.save!
2023-04-24 02:44:50 +00:00
end
rescue => e
2023-04-24 02:44:50 +00:00
puts e.inspect
end
2024-06-26 15:50:27 +00:00
redirect_url = admin_galleries_crop_process_path
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
2019-11-05 13:14:34 +00:00
notalive = ((AlbumVariable.first.notalive.nil? ? true : AlbumVariable.first.notalive) rescue true)
if notalive
if !params['album_id'].to_s.empty?
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
variable.finish = false
variable.save!
choice_ids = params['album_id'].split(',')
2020-04-25 02:48:14 +00:00
albums = Album.where(:id.in => choice_ids)
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
2020-04-25 02:48:14 +00:00
count = albums.reduce(0){|x,i| i.album_images.count+x}
Thread.new do
i = 0
2020-04-25 02:48:14 +00:00
album_ids = albums.pluck(:id)
album_ids.each do |album_id|
album = Album.find(album_id) rescue Album.new()
2020-04-25 02:48:14 +00:00
album_image_ids = album.album_images.pluck(:id)
album_image_ids.each do |image_id|
image = AlbumImage.find(image_id)
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
variable = AlbumVariable.first
variable.progress_percent = (i*100.0/count).floor.to_s+'%'
if !error.nil?
variable.progress_filename = error.inspect.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
sleep(1)
else
variable.progress_filename = image[:file].to_s
end
variable.save!
i+=1
end
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
end
else
variable = AlbumVariable.first
variable.finish = true
variable.progress_filename = ''
variable.progress_percent = '100%'
variable.save!
end
end
end
def finish_recreate
variable = AlbumVariable.first
variable.progress_percent = '0%'
variable.progress_filename = ''
variable.notalive = true
variable.save!
2022-10-04 10:43:24 +00:00
render :plain => ''
end
def recreate_progress
2019-11-05 13:14:34 +00:00
progress_percent = AlbumVariable.first.progress_percent rescue '0%'
progress_filename = AlbumVariable.first.progress_filename rescue ''
finish = AlbumVariable.first.finish rescue false
render :json => {'percent' => progress_percent, 'filename' => progress_filename, 'finish' => finish }.to_json
end
2020-02-04 08:18:36 +00:00
def filter_album
@tags = @module_app.tags
2024-08-03 18:58:04 +00:00
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
2020-02-04 08:18:36 +00:00
@filter_fields.delete(:status)
2022-12-03 16:26:47 +00:00
albums = Album.all.asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
@fiter_albums = search_data(albums,[:name])
2020-02-04 08:18:36 +00:00
end
def update_album_setting
2021-05-17 13:31:39 +00:00
if params[:album_setting_limit]
params_album_setting = {:limit=>params[:album_setting_limit]}
else
params_album_setting = params.require(:album_setting).permit!
end
2021-04-04 05:03:24 +00:00
AlbumSetting.first.update_attributes(params_album_setting)
2024-06-26 15:50:27 +00:00
redirect_to admin_galleries_path
end
def setting
2021-04-04 05:03:24 +00:00
@album_setting = AlbumSetting.first
end
2014-05-01 07:06:48 +00:00
def index
2020-02-04 08:18:36 +00:00
album_length = Album.all.count
album_sort = Album.all.asc(:created_at)
2020-02-26 11:34:55 +00:00
if (album_sort.first.album_colors.first.nil? rescue false)
2020-02-04 08:18:36 +00:00
album_sort.each_with_index do |album|
if album.album_colors.first.nil?
album.album_colors.create('color' => 'transparent')
end
2020-02-04 08:18:36 +00:00
end
end
@url = request.original_fullpath
@limit_count = AlbumSetting.first.limit.to_i
@limit_count = album_length if @limit_count == 0
2020-02-04 08:18:36 +00:00
if params['page_no'].nil?
@show_script = true
@albums = filter_album.take @limit_count
2020-02-04 08:18:36 +00:00
else
@show_script = false
start = (params['page_no'].to_i - 1)*@limit_count
@albums = filter_album[start...start+@limit_count]
end
if AlbumColor.count!=0
2020-02-04 08:18:36 +00:00
if AlbumColor.all.desc('updated_at').first[:color] == 'transparent'
@color_save = ''
else
@color_save = AlbumColor.desc('updated_at').first[:color]
end
else
@color_save = 'transparent'
end
2016-09-02 10:21:00 +00:00
if request.xhr?
2020-10-16 06:44:08 +00:00
render :partial => "albums"
2016-09-02 10:21:00 +00:00
end
end
def show
@album = Album.find(params[:id])
2022-12-03 16:26:47 +00:00
@images = @album.album_images.asc(:order)
image_content = []
@images.each do |image|
2022-12-03 16:26:47 +00:00
image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&:to_s)}
end
@tags = @module_app.tags
@photos_data = {"galleries" => image_content}.to_json
end
def new
@album = Album.new
@tags = @module_app.tags
2024-08-03 18:58:04 +00:00
@categories = @module_app.categories.enabled.authorized(current_user)
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
2023-04-24 02:44:50 +00:00
@album = Album.find(params[:id])
if can_edit_or_delete?(@album)
@tags = @module_app.tags
2024-08-03 18:58:04 +00:00
@categories = @module_app.categories.enabled.authorized(current_user)
2023-04-24 02:44:50 +00:00
else
render_401
end
end
2016-12-20 11:43:54 +00:00
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
2021-03-30 07:45:22 +00:00
v = row.cells.first.value rescue nil
next if v.blank?
2016-12-20 11:43:54 +00:00
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])
2022-12-03 16:26:47 +00:00
@images = @album.album_images.asc(:order)
image_content = []
@images.each do |image|
2022-12-03 16:26:47 +00:00
image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&: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}",
2023-04-24 02:44:50 +00:00
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
2014-10-20 12:29:45 +00:00
@album = Album.find(params[:gallery_id])
@tag_names = Array.new
2015-04-16 12:07:54 +00:00
@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,
2024-04-09 04:50:00 +00:00
file: values.file.as_json,
gallery_album_id: values.album_id,
2015-04-16 12:07:54 +00:00
order: values.order,
tags: values.tags}
end
2023-04-24 02:44:50 +00:00
render :json=>{"images" => @output, "tags" => @album.tags}.to_json
end
def upload_process
if (AlbumUnprocess.first.upload_success rescue false)
album_unprocess = Array(AlbumUnprocess.all)
count = album_unprocess.count
Thread.new do
album_unprocess.each_with_index do |un_image,i|
begin
2023-08-15 13:39:42 +00:00
album = AlbumImage.where(:id => un_image.image_id).first
if album.nil?
un_image.delete
else
album.file = un_image.save_var
album.save!
file = un_image.save_var.tempfile
file.close
File.delete file.path
un_image.delete
variable = AlbumVariable.first
variable.progress_filename = album[:file]
variable.progress_percent = ((i+1)*100.0/count).floor.to_s + '%'
variable.save!
end
rescue => e
puts ['err',un_image.inspect,e.inspect]
un_image.destroy
end
end
variable = AlbumVariable.first
variable.finish = true
variable.save!
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
variable = AlbumVariable.first
if variable.nil?
variable = AlbumVariable.new
end
2019-11-05 13:14:34 +00:00
variable.count = params['all_length'].to_i
variable.finish = false
variable.progress_percent = '0%'
variable.progress_filename = 'processing!!'
variable.notalive = true
variable.save!
render :json => {}.to_json
end
def upload_image
2019-10-23 05:54:31 +00:00
album = Album.find(params[:album_id])
files = params['files']
album_unprocess = AlbumUnprocess.new()
2019-10-23 05:54:31 +00:00
files.each do |file|
2023-04-24 02:44:50 +00:00
image = album.album_images.new
image.tags = (album.tags rescue [])
if(AlbumVariable.first.count==1 rescue true)
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)
2023-08-15 13:39:42 +00:00
fix_tempfile_reaper
2023-04-24 02:44:50 +00:00
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 ? lastimage.id.to_s : nil)}.to_json
end
def new_images
@album = Album.find(params[:album_id])
if params[:last_image_id].present?
2022-12-03 16:26:47 +00:00
lastimage = AlbumImage.find(params[:last_image_id]) rescue nil
if lastimage
@newimages = @album.album_images.where(:order.lt=>lastimage.order).desc(:created_at)
else
@newimages = @album.album_images.where(:id.gt=>params[:last_image_id]).desc(:created_at)
end
else
2022-12-03 16:26:47 +00:00
@newimages = @album.album_images.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
2016-03-02 04:31:47 +00:00
album = Album.find(params[:id])
tagsToDestroy = []
tagsToAdd = []
new_tags = params[:album][:tags] || []
2022-12-03 16:26:47 +00:00
old_tags = album.tag_ids.map(&: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
2016-03-02 04:31:47 +00:00
update_children_image_tag(album,tagsToDestroy,tagsToAdd)
album.update_attributes(album_params)
2024-06-26 15:50:27 +00:00
redirect_to admin_gallery_path(album.id)
end
2016-03-02 04:31:47 +00:00
def update_children_image_tag(album,tagsToDestroy,tagsToAdd)
2023-04-24 02:44:50 +00:00
# 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.tag_ids.map(&: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
2023-04-24 02:44:50 +00:00
image.tags = image_tags
image.save
end
end
private
2023-04-24 02:44:50 +00:00
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
2014-05-01 07:06:48 +00:00
end