OSDN Git Service

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