OSDN Git Service

picture storerer updated
[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 supply_default art
13     self.license_id = art.default_license_id if self.license_id.blank?
14   end
15   
16   def self.default_page_size
17     25
18   end
19   
20   def self.max_page_size
21     100
22   end
23   
24   def self.page prm = nil
25     page = prm.to_i
26     page = 1 if page < 1
27     page
28   end
29   
30   def self.page_size prm = self.default_page_size
31     page_size = prm.to_i
32     page_size = self.max_page_size if page_size > self.max_page_size
33     page_size = self.default_page_size if page_size < 1
34     page_size
35   end
36   
37   def self.list artist_id, opt = {}, page = 1, page_size = self.default_page_size
38     opt.merge!(self.list_opt) unless opt[:include]
39     opt.merge!({:conditions => ['artist_id = ?', artist_id], 
40       :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}
41     )
42     OriginalPicture.find(:all, opt)
43   end
44   
45   def self.list_opt
46     {:include => [:artist, :license, :resource_picture]}
47   end
48   
49   def self.list_json_opt
50     {:include => [:resource_picture, :artist, :license]}
51   end
52   
53   def self.show cid, author, opt = {}
54     pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
55     raise ActiveRecord::Forbidden unless pic.own?(author)
56     pic
57   end
58   
59   def self.show_include_opt opt = {}
60     res = [:license]
61     res.push(opt[:include]) if opt[:include]
62     res
63   end
64   
65   def self.show_json_include_opt
66     {:include => :license}
67   end
68   
69   def destroy_with_file
70     PictureIO.original_picture_io.delete self.filename
71     self.resource_picture.destroy
72   end
73   
74   def dext
75     self.ext.downcase
76   end
77   
78   def filename
79     "#{self.id}.#{self.dext}"
80   end
81   
82   def mime_type
83     "image/#{self.dext}"
84   end
85   
86   def url
87     '/original_pictures/' + filename
88   end
89   
90   def store(picture_data, art, lid = nil)
91     res = false
92     begin
93       mgk = Magick::Image.from_blob(picture_data).shift
94     rescue 
95       self.errors.add :base, 'magick failed'
96       return false
97     end
98     self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows, 
99       :filesize => mgk.filesize, :artist_id => art.id, 
100       :license_id => lid.blank? ? art.default_license_id : lid.to_i
101     }
102     OriginalPicture.transaction do
103       if res = self.save
104         if res = PictureIO.original_picture_io.put(picture_data, self.filename)
105           rp = ResourcePicture.update_picture(self)
106           unless rp.store(mgk)
107             PictureIO.original_picture_io.delete(self.filename)
108             self.errors.add :base, 'resource picture copying error'
109             raise ActiveRecord::Rollback
110           end
111         else
112           self.errors.add :base, 'original picture io does not work'
113           raise ActiveRecord::Rollback
114         end
115       end
116     end
117     res
118   end
119   
120   def restore(subdir = nil)
121     PictureIO.original_picture_io.get self.filename, subdir
122   end
123   
124   def own? author
125     return false unless author
126     return false unless author.artist?
127     self.artist_id == author.artist.id
128   end
129   
130 end