OSDN Git Service

rename lisence to license
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :license
4   has_one :resource_picture
5   
6   before_destroy :destroy_with_file
7   
8   def validate
9     errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
10   end
11   
12   def destroy_with_file
13     PictureIO.original_picture_io.delete self.filename
14     self.resource_picture.destroy
15   end
16   
17   def dext
18     self.ext.downcase
19   end
20   
21   def filename
22     "#{self.id}.#{self.dext}"
23   end
24   
25   def mime_type
26     "image/#{self.dext}"
27   end
28   
29   def url
30     '/original_pictures/' + filename
31   end
32   
33   def store(rimg)
34     bindata = rimg.to_blob
35     PictureIO.original_picture_io.put bindata, self.filename
36     res = if self.resource_picture
37       self.resource_picture.store rimg
38     else
39       ResourcePicture.store(rimg, self)
40     end
41     res
42   end
43   
44   def restore(subdir = nil)
45     PictureIO.original_picture_io.get self.filename, subdir
46   end
47   
48   def own? author
49     return false unless author
50     return false unless author.artist?
51     self.artist_id == author.artist.id
52   end
53   
54 end