OSDN Git Service

t#29688:change owner permission
[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     if ar.is_a?(Author)
25       self.artist_id == ar.artist.id
26     elsif ar.is_a?(Artist)
27       self.artist_id == ar.id
28     else
29       false
30     end
31   end
32   
33   def visible? ar
34     self.own?(ar)
35   end
36   
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     tw, th = PettanImager.thumbnail_size(self.width, self.height)
51     {:src => self.url, :width => tw, :height => th}
52   end
53   
54   def self.default_page_size
55     25
56   end
57   
58   def self.max_page_size
59     100
60   end
61   
62   def self.page prm = nil
63     page = prm.to_i
64     page = 1 if page < 1
65     page
66   end
67   
68   def self.page_size prm = self.default_page_size
69     page_size = prm.to_i
70     page_size = self.max_page_size if page_size > self.max_page_size
71     page_size = self.default_page_size if page_size < 1
72     page_size
73   end
74   
75   def self.mylist artist_id, page = 1, page_size = self.default_page_size
76     opt = {}
77     opt.merge!(self.list_opt)
78     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
79     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
80     OriginalPicture.find(:all, opt)
81   end
82   
83   def history 
84     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
85   end
86   
87   def self.list_opt
88     {:include => {:resource_picture => {}, :pictures => {}}}
89   end
90   
91   def self.list_json_opt
92     {:include => {:resource_picture => {}, :pictures => {}}}
93   end
94   
95   def self.show cid, ar
96     opt = {}
97     opt.merge!(self.show_opt)
98     res = OriginalPicture.find(cid, opt)
99     raise ActiveRecord::Forbidden unless res.visible?(ar)
100     res
101   end
102   
103   def self.edit cid, ar
104     opt = {}
105     opt.merge!(self.show_opt)
106     res = OriginalPicture.find(cid, opt)
107     raise ActiveRecord::Forbidden unless res.own?(ar)
108     res
109   end
110   
111   def self.show_opt
112     {:include => {:resource_picture => {}, :pictures => {}}}
113   end
114   
115   def self.show_json_opt
116     {:include => {:resource_picture => {}, :pictures => {}}}
117   end
118   
119   def destroy_with_file
120     PictureIO.original_picture_io.delete self.filename
121     self.resource_picture.destroy
122   end
123   
124   def store(imager)
125     unless imager
126       self.errors.add :base, 'illegal picture data'
127       return false
128     end
129     res = false
130     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
131     OriginalPicture.transaction do
132       if res = self.save
133         if res = PictureIO.original_picture_io.put(imager.binary, self.filename)
134           res = true
135         else
136           self.errors.add :base, 'original picture io does not work'
137           raise ActiveRecord::Rollback
138         end
139       end
140     end
141     res
142   end
143   
144   def restore(subdir = nil)
145     PictureIO.original_picture_io.get self.filename, subdir
146   end
147   
148   def self.export ar
149     l = LicenseGroup.list
150     op = OriginalPicture.list ar.id
151     {:license_groups => l, :original_pictures => op}
152   end
153   
154 end