OSDN Git Service

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