OSDN Git Service

fix: view
[pettanr/pettanr.git] / app / models / picture.rb
1 #実素材
2 class Picture < Peta::Content
3   load_manifest
4   belongs_to :original_picture
5   belongs_to :license
6   belongs_to :system_picture
7   belongs_to :artist
8   has_one :resource_picture
9   has_one :resource_picture_picture
10   
11   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
12   validates :revision, :presence => true, :numericality => true
13   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
14   validates :width, :presence => true, :numericality => true, :natural_number => true
15   validates :height, :presence => true, :numericality => true, :natural_number => true
16   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
17   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
18   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
19   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
21   validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
22   
23   scope :find_index, -> do
24     self
25   end
26   
27   scope :find_by_artist, -> (artist_id) do 
28     find_index.where(artist_id: artist_id)
29   end
30   
31   scope :find_by_original_picture, -> (original_picture_id) do 
32     find_index.where(original_picture_id: original_picture_id)
33   end
34   
35   def supply_default
36   end
37   
38   def overwrite rp
39     attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize, 
40       :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, 
41       :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id, 
42       :md5 => rp.md5, 
43       :license_group_module_name => rp.license_group_module_name, 
44       :license_group_settings => rp.license_group_settings, 
45       :credit_picture_settings => rp.credit_picture_settings,
46       :license_settings => rp.license_settings
47     }
48     self.attributes = attr
49     self.revision = self.new_revision   #Do not move to attr. new_revision reffernces self.original_picture_id
50   end
51   
52   def visible? operators
53     return true
54   end
55   
56   def showable? operators = nil
57     return false unless self.original_picture
58     return true if self.own?(operators)
59     self.enable? and self.head?
60   end
61   
62   def filename
63     "#{self.id}.#{self.ext}"
64   end
65   
66   def gifname
67     "#{self.id}.gif"
68   end
69   
70   def mime_type
71     "image/#{self.ext}"
72   end
73   
74   def url
75     '/pictures/' + filename
76   end
77   
78   def opt_img_tag
79     {:src => self.url, :width => self.width, :height => self.height}
80   end
81   
82   def tmb_opt_img_tag
83     tw, th = PettanImager.thumbnail_size(self.width, self.height)
84     {:src => self.url, :width => tw, :height => th}
85   end
86   
87   def tail_opt_img_tag img
88     {:src => img, :width => self.width, :height => self.height}
89   end
90   
91   def tail_tmb_opt_img_tag img
92     tw, th = PettanImager.thumbnail_size(self.width, self.height)
93     {:src => img, :width => tw, :height => th}
94   end
95   
96   def alt_name
97     self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
98   end
99   
100   def symbol_option
101     self.tmb_opt_img_tag
102   end
103   
104   def new_revision
105     Picture.where(original_picture_id: self.original_picture_id).maximum(:revision).to_i + 1
106   end
107   
108   def enable?
109     r = self.head.resource_picture
110     r ? true : false
111   end
112   
113   def self.head opid
114     Picture.where(original_picture_id: opid).order('pictures.revision desc').first
115   end
116   
117   def head
118     Picture.head(self.original_picture_id)
119   end
120   
121   def head?
122     self == head
123   end
124   
125   def to_gif?
126     self.ext == 'png' and self.license_extend.gif_convert >= 0
127   end
128   
129   def subdirs
130     self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
131   end
132   
133   def self.find_by_md5 md5
134     r = Picture.where(md5: md5).order(updated_at: :desc)
135   end
136   
137   def self.list_by_md5 md5, opid = nil
138     cond = if opid.blank?
139       {md5: md5}
140     else
141       ['md5 = :md5 and original_picture_id <> :opid', {md5: md5, opid: opid}]
142     end
143     r = Picture.where(cond).order(updated_at: :desc)
144   end
145   
146   def self.exist_by_md5 md5, opid
147     Picture.list_by_md5(md5, opid).empty? ? false : true
148   end
149   
150   def self.list_by_original_picture_where original_picture_id
151     ['pictures.original_picture_id = ?', original_picture_id]
152   end
153   
154   def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
155     self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('pictures.revision desc').offset((page -1) * page_size).limit(page_size)
156   end
157   
158   def self.list_opt
159     {:license => {}, :artist => {}}
160   end
161   
162   def self.list_json_opt
163     {:include => {:license => {}, :artist => {}} }
164   end
165   
166   def store(imager)
167     res = false
168     if res = self.save
169       if res = self.store_picture(imager, self.filename)
170         if self.to_gif?
171           if gifimager = imager.to_gif
172             res = self.store_picture(gifimager, self.gifname)
173           else
174             res = false
175           end
176         end
177       end
178     end
179     res
180   end
181   
182   def store_picture(imager, fn)
183     res = false
184     subdirs.each do |d|
185       picdata = d.empty? ? imager.binary : imager.__send__(d)
186       res = PictureIO.picture_io.put(picdata, fn, d)
187       break unless res
188     end
189     res
190   end
191   
192   def restore(subdir = nil)
193     PictureIO.picture_io.get self.filename, subdir
194   end
195   
196   def self.export(dt = nil)
197     pictures = Picture.includes(:artist).where('artists.author_id is not null')
198     pictures = pictures.where(['pictures.updated_at >= ?', dt]) if dt
199     pictures.order('pictures.updated_at desc').references(:artist)
200   end
201   
202   def self.list_as_json_text ary
203     '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
204   end
205   
206   def picture_data
207     Base64.encode64(self.restore)
208   end
209   
210   def to_json_with_picture_data
211     self.to_json({:methods => :picture_data})
212   end
213   
214   def unpublish
215     self.boosts 'post'
216     imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
217     return false unless imager
218     self.store imager
219   end
220   
221   def credit_template
222     "#{self.license_group_module_name.tableize}/attributes/credit"
223   end
224   
225   def full_credit_template
226     "#{self.license_group_module_name.tableize}/attributes/full_credit"
227   end
228   
229 end