belongs_to :license
has_one :resource_picture
+ validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
+ validates :width, :presence => true, :numericality => true, :natural_number => true
+ validates :height, :presence => true, :numericality => true, :natural_number => true
+ validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
+ validates :artist_id, :presence => true, :numericality => true, :existence => true
+ validates :license_id, :presence => true, :numericality => true, :existence => true
+
before_destroy :destroy_with_file
def validate
end
def self.list_opt
- {:include => [:artist, :license, :resource_picture]}
+ {:include => [:license, :resource_picture]}
end
def self.list_json_opt
- {:include => [:resource_picture, :artist, :license]}
+ {:include => [:license, :resource_picture]}
end
- def self.show cid, opt = {}
- Comic.find(cid, :include => self.show_include_opt(opt))
+ def self.show cid, artist, opt = {}
+ pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
+ raise ActiveRecord::Forbidden unless pic.own?(artist)
+ pic
end
def self.show_include_opt opt = {}
- res = [:author]
+ res = [:license, :resource_picture]
res.push(opt[:include]) if opt[:include]
res
end
def self.show_json_include_opt
- {:include => :author}
+ {:include => [:license, :resource_picture]}
end
def destroy_with_file
'/original_pictures/' + filename
end
- def store(rimg)
- bindata = rimg.to_blob
- PictureIO.original_picture_io.put bindata, self.filename
- res = if self.resource_picture
- self.resource_picture.store rimg
- else
- ResourcePicture.store(rimg, self)
+ def data_to_mgk picture_data
+ begin
+ mgk = Magick::Image.from_blob(picture_data).shift
+ rescue
+ self.errors.add :base, 'magick failed'
+ return false
+ end
+ mgk
+ end
+
+ def store(picture_data, art, lid = nil)
+ mgk = data_to_mgk picture_data
+ return false unless mgk
+ res = false
+ self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows,
+ :filesize => mgk.filesize, :artist_id => art.id,
+ :license_id => lid.blank? ? art.default_license_id : lid.to_i
+ }
+ OriginalPicture.transaction do
+ if res = self.save
+ if res = PictureIO.original_picture_io.put(picture_data, self.filename)
+ rp = ResourcePicture.update_picture(self)
+ unless rp.store(mgk)
+ res = false
+ PictureIO.original_picture_io.delete(self.filename)
+ self.errors.add :base, 'resource picture copying error'
+ raise ActiveRecord::Rollback
+ end
+ res = true
+ else
+ self.errors.add :base, 'original picture io does not work'
+ raise ActiveRecord::Rollback
+ end
+ end
end
res
end
PictureIO.original_picture_io.get self.filename, subdir
end
- def own? author
- return false unless author
- return false unless author.artist?
- self.artist_id == author.artist.id
+ def own? art
+ return false unless art
+ self.artist_id == art.id
end
end