OSDN Git Service

bd776a6890ad41aa8c38b5adb5d4fa42af82542c
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :original_picture_license_group
4   has_one :resource_picture
5   has_many :pictures
6   
7   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
8   validates :width, :presence => true, :numericality => true, :natural_number => true
9   validates :height, :presence => true, :numericality => true, :natural_number => true
10   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
11   validates :artist_id, :presence => true, :numericality => true, :existence => true
12   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
13   
14   before_destroy :destroy_with_file
15   
16   def supply_default
17   end
18   
19   def overwrite ar
20     self.artist_id = ar.id
21   end
22   
23   def own? ar
24     self.artist_id == ar.id
25   end
26   
27   def visible? ar
28     self.own?(ar)
29   end
30   
31   def filename
32     "#{self.id}.#{self.ext}"
33   end
34   
35   def mime_type
36     "image/#{self.ext}"
37   end
38   
39   def url
40     '/original_pictures/' + filename
41   end
42   
43   def opt_img_tag
44     tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], self.width, self.height)
45     {:src => self.url, :width => tw, :height => th}
46   end
47   
48   def self.default_page_size
49     25
50   end
51   
52   def self.max_page_size
53     100
54   end
55   
56   def self.page prm = nil
57     page = prm.to_i
58     page = 1 if page < 1
59     page
60   end
61   
62   def self.page_size prm = self.default_page_size
63     page_size = prm.to_i
64     page_size = self.max_page_size if page_size > self.max_page_size
65     page_size = self.default_page_size if page_size < 1
66     page_size
67   end
68   
69   def self.mylist artist_id, page = 1, page_size = self.default_page_size
70     opt = {}
71     opt.merge!(self.list_opt)
72     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
73     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
74     OriginalPicture.find(:all, opt)
75   end
76   
77   def history 
78     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
79   end
80   
81   def self.list_opt
82     {:include => {:resource_picture => {}, :pictures => {}}}
83   end
84   
85   def self.list_json_opt
86     {:include => {:resource_picture => {}, :pictures => {}}}
87   end
88   
89   def self.show cid, ar
90     opt = {}
91     opt.merge!(self.show_opt)
92     res = OriginalPicture.find(cid, opt)
93     raise ActiveRecord::Forbidden unless res.visible?(ar)
94     res
95   end
96   
97   def self.edit cid, ar
98     opt = {}
99     opt.merge!(self.show_opt)
100     res = OriginalPicture.find(cid, opt)
101     raise ActiveRecord::Forbidden unless res.own?(ar)
102     res
103   end
104   
105   def self.show_opt
106     {:include => {:resource_picture => {}, :pictures => {}}}
107   end
108   
109   def self.show_json_opt
110     {:include => {:resource_picture => {}, :pictures => {}}}
111   end
112   
113   def destroy_with_file
114     PictureIO.original_picture_io.delete self.filename
115     self.resource_picture.destroy
116   end
117   
118   def store(imager)
119     unless imager
120       self.errors.add :base, 'illegal picture data'
121       return false
122     end
123     res = false
124     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
125     OriginalPicture.transaction do
126       if res = self.save
127         if res = PictureIO.original_picture_io.put(imager.binary, self.filename)
128           res = true
129         else
130           self.errors.add :base, 'original picture io does not work'
131           raise ActiveRecord::Rollback
132         end
133       end
134     end
135     res
136   end
137   
138   def restore(subdir = nil)
139     PictureIO.original_picture_io.get self.filename, subdir
140   end
141   
142   def self.export ar
143     l = LicenseGroup.list
144     op = OriginalPicture.list ar.id
145     {:license_groups => l, :original_pictures => op}
146   end
147   
148 end