OSDN Git Service

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