1 class OriginalPicture < Peta::Content
4 belongs_to :original_picture_license_group
5 has_one :resource_picture
7 has_many :resource_picture_pictures
9 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
10 validates :width, :presence => true, :numericality => true, :natural_number => true
11 validates :height, :presence => true, :numericality => true, :natural_number => true
12 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
13 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
14 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
16 scope :find_index, -> (operators) do
17 where(artist_id: operators.artist.id)
20 scope :find_history, -> (id) do
21 Picture.find_by_original_picture(id).order(Picture.arel_table[:revision].desc)
28 def overwrite operators
29 self.uploaded_at = Time.now
30 return unless operators.artist
31 self.artist_id = operators.artist.id
34 def visible? operators
38 "#{self.id}.#{self.ext}"
46 '/original_pictures/' + filename
50 {:src => self.url, :width => self.width, :height => self.height}
54 tw, th = PettanImager.thumbnail_size(self.width, self.height)
55 {:src => self.url, :width => tw, :height => th}
67 head = self.class.find_history(self.id).first
68 head ? head.revision : 'unpublished'
72 self.published_at == nil and self.stopped_at == nil
76 self.stopped_at != nil
80 dt = self.published_at || self.stopped_at
81 return false unless dt
86 self.published_at != nil
90 {:include => {:resource_picture => {}, :pictures => {}}}
95 self.errors.add :base, I18n.t('errors.invalid_image')
99 self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
100 OriginalPicture.transaction do
103 res = PictureIO.original_picture_io.put(imager.binary, self.filename)
104 rescue PictureIO::Error
106 self.errors.add :base, I18n.t('picture_io.error')
107 raise ActiveRecord::Rollback
114 def restore(subdir = nil)
115 PictureIO.original_picture_io.get self.filename, subdir
118 def self.export(dt = nil)
119 original_pictures = OriginalPicture.includes(:resource_picture, :artist).where('artists.author_id is not null')
120 original_pictures = original_pictures.where(['original_pictures.updated_at >= ?', dt]) if dt
121 original_pictures.order('original_pictures.id').references(:artist)
124 def list_as_json_with_resource_picture
125 self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
128 def self.list_as_json_text ary
129 '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
132 def destroy_with_resource_picture
134 OriginalPicture.transaction do
136 PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
137 rescue PictureIO::Error
139 raise ActiveRecord::Rollback
141 if self.resource_picture
142 res = self.resource_picture.unpublish
143 raise ActiveRecord::Rollback unless res
145 self.pictures.each do |picture|
146 res = picture.unpublish
147 raise ActiveRecord::Rollback unless res
148 if picture.resource_picture_picture
149 picture.resource_picture_picture.original_picture_id = nil
150 picture.resource_picture_picture.resource_picture_id = nil
151 raise ActiveRecord::Rollback unless picture.resource_picture_picture.save
155 raise ActiveRecord::Rollback unless res
160 def self.publish oid, lsname, attr
161 l = License.find_by(name: lsname)
162 op = OriginalPicture.find oid
164 attr[:license_id] = l.id
166 ctl = lg.module_name.pluralize + '::Attribute'
167 le = ctl.constantize.new attr
169 rp = ResourcePicture.new
170 rp.attributes = le.resource_picture_attributes op
173 imager = PettanImager.load op.restore
177 def self.upload fn, auth
178 b = Base64.encode64(File.open(fn, 'rb').read)
179 u = 'http://localhost:3000/original_pictures'
180 r = RestClient.post(u,
181 {:original_picture => {:file => b}, :auth_token => auth}.to_json,
182 :content_type => :json, :accept => :json
189 def self.auto_publish dirname, auth
190 Dir.glob File.expand_path(dirname) + '/*' do |filename|
191 if File.directory?(filename)
195 Dir.glob(filename + '/*') do |fn|
196 ext = File.extname(fn).downcase
199 json = JSON.parse(File.open(fn).read)
200 lsname = json["license_name"]
201 attr = json["attributes"]
202 when '.png', '.gif', '.jpeg'
206 oid = OriginalPicture.upload img, auth
207 OriginalPicture.publish oid, lsname, attr