merged/app/models/merged/image.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2022-11-28 22:58:17 +01:00
module Merged
class Image
include ActiveModel::API
include ActiveModel::Conversion
extend ActiveModel::Naming
cattr_reader :all
@@all = {}
def self.load_images()
glob = Rails.root.join Image.asset_root + "/*.*"
Dir[glob].each do |f|
file = f.split("/").last
Image.new( file )
end
end
2022-11-28 22:58:17 +01:00
attr_reader :name , :type , :size , :created_at , :updated_at
def initialize(filename)
@name , @type = filename.split(".")
file = File.new(Rails.root.join(Image.asset_root,filename))
2022-11-28 22:58:17 +01:00
@created_at = file.birthtime
@updated_at = file.ctime
@size = (file.size/1024).to_i
@@all[@name] = self
2022-11-28 22:58:17 +01:00
end
#save an io with given name (without ending, that is taken from io)
def self.create_new(filename , io)
original , ending = io.original_filename.split("/").last.split(".")
filename = original if( filename.blank? )
full_filename = filename + "." + ending
File.open(Rails.root.join(Image.asset_root, full_filename), "wb") do |f|
2022-11-28 22:58:17 +01:00
f.write( io.read )
end
Image.new( full_filename )
2022-11-28 22:58:17 +01:00
end
def self.asset_root
"app/assets/images/" + image_root
end
2022-11-29 11:07:10 +01:00
def self.image_root
"cms"
2022-11-28 22:58:17 +01:00
end
end
end