2022-12-29 23:41:52 +01:00
|
|
|
require "mini_magick"
|
|
|
|
|
2022-11-28 22:58:17 +01:00
|
|
|
module Merged
|
|
|
|
class ImagesController < MergedController
|
2023-01-01 10:56:11 +01:00
|
|
|
before_action :set_image, only: %i[ update destroy show scale crop copy ]
|
2022-11-28 22:58:17 +01:00
|
|
|
|
|
|
|
def index
|
2023-01-23 13:53:04 +01:00
|
|
|
@images = get_images
|
2022-12-17 20:22:31 +01:00
|
|
|
@image_data = @images.collect{|i|
|
|
|
|
data = i.attributes.dup
|
|
|
|
data[:url] = view_context.asset_path(i.asset_name)
|
2023-01-09 19:48:14 +01:00
|
|
|
data[:link] = build_select_link_for(i)
|
2022-12-27 16:04:53 +01:00
|
|
|
data[:updated_at] = i.updated_at.to_date if i.updated_at
|
|
|
|
data[:created] = i.updated_at.to_i
|
2022-12-17 20:22:31 +01:00
|
|
|
data[:aspect_ratio] = i.aspect_ratio.join("/")
|
2022-12-17 21:15:59 +01:00
|
|
|
data[:ratio] = i.aspect_ratio
|
2022-12-17 20:22:31 +01:00
|
|
|
data
|
|
|
|
}
|
2022-11-28 22:58:17 +01:00
|
|
|
end
|
|
|
|
|
2022-12-19 20:15:46 +01:00
|
|
|
def destroy
|
2023-10-23 19:20:31 +02:00
|
|
|
@image.destroy(current_member_email)
|
2022-12-29 23:41:52 +01:00
|
|
|
redirect_to :images , notice: "Image #{@image.name} deleted"
|
2022-12-19 20:15:46 +01:00
|
|
|
end
|
|
|
|
|
2022-12-29 23:41:52 +01:00
|
|
|
def update
|
2023-01-01 10:56:11 +01:00
|
|
|
@image.name = params[:name]
|
|
|
|
@image.tags = params[:tags]
|
2023-10-23 19:20:31 +02:00
|
|
|
@image.edit_save(current_member_email)
|
2023-01-01 10:56:11 +01:00
|
|
|
redirect_to image_path(@image) , notice: "Image updated"
|
|
|
|
end
|
|
|
|
|
|
|
|
def scale
|
2023-01-16 22:18:49 +01:00
|
|
|
@image.scale( params[:scale] )
|
2023-10-23 19:20:31 +02:00
|
|
|
@image.edit_save(current_member_email)
|
2023-01-01 10:56:11 +01:00
|
|
|
redirect_to image_path(@image) , notice: "Image was scaled"
|
|
|
|
end
|
|
|
|
|
|
|
|
def crop
|
|
|
|
size = "#{params[:size_x]}x#{params[:size_y]}+#{params[:off_x]}+#{params[:off_y]}"
|
2023-01-16 22:18:49 +01:00
|
|
|
@image.crop( size )
|
2023-10-23 19:20:31 +02:00
|
|
|
@image.edit_save(current_member_email)
|
2023-01-01 10:56:11 +01:00
|
|
|
redirect_to image_path(@image) , notice: "Image was resized"
|
2022-12-29 23:41:52 +01:00
|
|
|
end
|
2022-12-30 20:10:33 +01:00
|
|
|
|
2022-12-19 08:45:47 +01:00
|
|
|
def show
|
2022-12-19 17:55:11 +01:00
|
|
|
@sections = Section.where(image_id: params[:id].to_i)
|
|
|
|
@cards = Card.where(image_id: params[:id].to_i)
|
|
|
|
@used = ((@cards.length > 0) || (@sections.length > 0))
|
2022-12-29 20:20:23 +01:00
|
|
|
@image_data = @image.attributes
|
2022-12-19 08:45:47 +01:00
|
|
|
end
|
|
|
|
|
2022-12-30 20:10:33 +01:00
|
|
|
def copy
|
|
|
|
image = @image.copy
|
2023-10-23 19:20:31 +02:00
|
|
|
image.add_save(current_member_email)
|
2022-12-30 20:10:33 +01:00
|
|
|
redirect_to image_path(image.id) , notice: "Image copied"
|
|
|
|
end
|
|
|
|
|
2022-11-28 22:58:17 +01:00
|
|
|
def create
|
2022-12-27 16:04:53 +01:00
|
|
|
image = Image.create_new(params['filename'] ,params['tags'], params['image_file'])
|
2023-10-23 19:20:31 +02:00
|
|
|
image.add_save current_member_email
|
2022-12-19 17:26:46 +01:00
|
|
|
where_to = determine_redirect(image)
|
|
|
|
redirect_to where_to , notice: "New image created: #{image.name}"
|
2022-11-28 22:58:17 +01:00
|
|
|
end
|
|
|
|
|
2022-12-19 08:45:47 +01:00
|
|
|
private
|
2022-12-19 17:26:46 +01:00
|
|
|
|
2023-01-23 13:53:04 +01:00
|
|
|
def get_images
|
|
|
|
images = Image.all
|
|
|
|
return images unless params[:unused]
|
|
|
|
hash = images.collect{|i| [i.id , i ] }.to_h
|
|
|
|
Section.all.each{ |s| hash.delete(s.image&.id)}
|
|
|
|
Card.all.each{ |s| hash.delete(s.image&.id)}
|
|
|
|
hash.values
|
|
|
|
end
|
2022-12-30 20:10:33 +01:00
|
|
|
def set_image
|
|
|
|
@image = Image.find(params[:id] || params[:image_id])
|
|
|
|
end
|
2022-12-19 17:26:46 +01:00
|
|
|
def determine_redirect(image)
|
|
|
|
if(params[:section_id])
|
|
|
|
view_context.section_set_image_url(params[:section_id],image_id: image.id )
|
|
|
|
elsif(params[:card_id])
|
|
|
|
view_context.card_set_image_url(params[:card_id],image_id: image.id )
|
|
|
|
else
|
|
|
|
:images
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 19:48:14 +01:00
|
|
|
def build_select_link_for(image)
|
2022-12-19 08:45:47 +01:00
|
|
|
if(params[:section_id])
|
|
|
|
return view_context.section_set_image_url(params[:section_id] , image_id: image.id)
|
|
|
|
end
|
2022-12-19 08:53:25 +01:00
|
|
|
if(params[:card_id])
|
|
|
|
return view_context.card_set_image_url(params[:card_id] , image_id: image.id)
|
|
|
|
end
|
2022-12-19 08:45:47 +01:00
|
|
|
view_context.image_url(image.id)
|
|
|
|
end
|
2022-11-28 22:58:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|