OSDN Git Service

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