OSDN Git Service

883b1df8817c6381a39062140fb8c71b3be6a2a7
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < Peta::Content
2   load_manifest
3   belongs_to :artist
4   belongs_to :original_picture_license_group
5   has_one :resource_picture
6   has_many :pictures
7   has_many :resource_picture_pictures
8   
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}
15   
16   def supply_default
17     self.artist_id = nil
18   end
19   
20   def overwrite operators
21     self.uploaded_at = Time.now
22     return unless operators.artist
23     self.artist_id = operators.artist.id
24   end
25   
26   def visible? operators
27     self.own? operators
28   end
29   def filename
30     "#{self.id}.#{self.ext}"
31   end
32   
33   def mime_type
34     "image/#{self.ext}"
35   end
36   
37   def url
38     '/original_pictures/' + filename
39   end
40   
41   def opt_img_tag
42     {:src => self.url, :width => self.width, :height => self.height}
43   end
44   
45   def tmb_opt_img_tag
46     tw, th = PettanImager.thumbnail_size(self.width, self.height)
47     {:src => self.url, :width => tw, :height => th}
48   end
49   
50   def symbol_option
51     self.tmb_opt_img_tag
52   end
53   
54   def filer_caption
55     self.revision
56   end
57   
58   def revision
59     head = self.history.first
60     head ? head.revision : 'unpublished'
61   end
62   
63   def unpublished?
64     self.published_at == nil and self.stopped_at == nil
65   end
66   
67   def stopped?
68     self.stopped_at != nil
69   end
70   
71   def unlicensed?
72     dt = self.published_at || self.stopped_at
73     return false unless dt
74     self.uploaded_at > dt
75   end
76   
77   def published?
78     self.published_at != nil
79   end
80   
81   def history 
82     Picture.where(original_picture_id: self.id).order(revision: :desc)
83   end
84   
85   def self.show_opt
86     {:include => {:resource_picture => {}, :pictures => {}}}
87   end
88   
89   def store(imager)
90     unless imager
91       self.errors.add :base, I18n.t('errors.invalid_image')
92       return false
93     end
94     res = false
95     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
96     OriginalPicture.transaction do
97       if res = self.save
98         begin
99           res = PictureIO.original_picture_io.put(imager.binary, self.filename)
100         rescue PictureIO::Error
101           res = false
102           self.errors.add :base, I18n.t('picture_io.error')
103           raise ActiveRecord::Rollback
104         end
105       end
106     end
107     res
108   end
109   
110   def restore(subdir = nil)
111     PictureIO.original_picture_io.get self.filename, subdir
112   end
113   
114   def self.export(dt = nil)
115     original_pictures = OriginalPicture.includes(:resource_picture, :artist).where('artists.author_id is not null')
116     original_pictures = original_pictures.where(['original_pictures.updated_at >= ?', dt]) if dt
117     original_pictures.order('original_pictures.id').references(:artist)
118   end
119   
120   def list_as_json_with_resource_picture
121     self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
122   end
123   
124   def self.list_as_json_text ary
125     '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
126   end
127   
128   def destroy_with_resource_picture
129     res = false
130     OriginalPicture.transaction do
131       begin
132         PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
133       rescue PictureIO::Error
134         res = false
135         raise ActiveRecord::Rollback
136       end
137       if self.resource_picture
138         res = self.resource_picture.unpublish
139         raise ActiveRecord::Rollback unless res
140       end
141       self.pictures.each do |picture|
142         res = picture.unpublish
143         raise ActiveRecord::Rollback unless res
144         if picture.resource_picture_picture
145           picture.resource_picture_picture.original_picture_id = nil
146           picture.resource_picture_picture.resource_picture_id = nil
147           raise ActiveRecord::Rollback unless picture.resource_picture_picture.save
148         end
149       end
150       res = self.destroy
151       raise ActiveRecord::Rollback unless res
152     end
153     res
154   end
155   
156   def self.publish oid, lsname, attr
157     l = License.find_by(name: lsname)
158     op = OriginalPicture.find oid
159     lg = l.license_group
160     attr[:license_id] = l.id
161     
162     ctl = lg.module_name.pluralize + '::Attribute'
163     le = ctl.constantize.new attr
164     
165     rp = ResourcePicture.new
166     rp.attributes = le.resource_picture_attributes op
167     rp.overwrite op
168     
169     imager = PettanImager.load op.restore
170     rp.store imager
171   end
172   
173   def self.upload fn, auth
174     b = Base64.encode64(File.open(fn, 'rb').read)
175     u = 'http://localhost:3000/original_pictures'
176     r = RestClient.post(u, 
177       {:original_picture => {:file  => b}, :auth_token => auth}.to_json, 
178       :content_type => :json, :accept => :json
179     )
180     o = JSON.parse r
181     oid = o['id']
182     oid
183   end
184   
185   def self.auto_publish dirname, auth
186     Dir.glob File.expand_path(dirname) + '/*' do |filename|
187       if File.directory?(filename)
188         img = nil
189         lsname = nil
190         attr  = nil
191         Dir.glob(filename + '/*') do |fn|
192           ext = File.extname(fn).downcase
193           case ext
194           when '.json'
195             json = JSON.parse(File.open(fn).read)
196             lsname = json["license_name"]
197             attr = json["attributes"]
198           when '.png', '.gif', '.jpeg'
199             img = fn
200           end
201         end
202         oid = OriginalPicture.upload img, auth
203         OriginalPicture.publish oid, lsname, attr
204       end
205     end
206   end
207   
208 end