X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fresource_picture.rb;h=be16276ba039f4166cc458a5a5be1cf70a52ec03;hb=6a9d02a32b5c300f00b3a69dd0ee7669cf8dac34;hp=1bf90217b25bf5f3800c6a7dd9d9adc36e2bfae3;hpb=fff244784232225332e0beecdc655d9ded12e2d9;p=pettanr%2Fpettanr.git diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index 1bf90217..be16276b 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -1,9 +1,19 @@ class ResourcePicture < ActiveRecord::Base belongs_to :artist - belongs_to :lisence + belongs_to :license has_many :panel_pictures belongs_to :original_picture + before_destroy :destroy_with_file + + def destroy_with_file + PictureIO.resource_picture_io.delete self.filename + PictureIO.resource_picture_io.class.subdirs.each do |d| + next if d.empty? + PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d) + end + end + def self.resize(data, dw, dh) Magick::Image.from_blob(data).shift.resize(dw, dh) end @@ -59,42 +69,57 @@ class ResourcePicture < ActiveRecord::Base end def thumbnail(rimg) - tw, th = ResourcePicture.fix_size_both(80, 80, rimg.columns, rimg.rows) + tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows) ResourcePicture.resize(rimg.to_blob, tw, th).to_blob end - def self.store(img, original_picture) - - resource_picture = ResourcePicture.new( - width: original_picture.width, height: original_picture.height, - ext: original_picture.ext, filesize: original_picture.filesize, - original_picture_id: original_picture.id - ) - res = if resource_picture.save - if resource_picture.store(img) - true - else - false - end - else - false + def self.update_picture(op) + res = op.resource_picture || ResourcePicture.new + res.attributes = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, + :original_picture_id => op.id, :artist_id => op.artist_id, :license_id => op.license_id + } + res + end + + def to_gif? + self.dext == 'png' and self.license.no_convert == 0 + end + + def self.png_to_gif(data) + begin + mgk = Magick::Image.from_blob(data).shift + mgk.format = 'gif' + mgk.to_blob + res = mgk + rescue + res = false end res end - def store(img) + def store(mgk) res = false - bindata = img.to_blob -# begin - PictureIO.resource_picture_io.put bindata, self.filename - PictureIO.resource_picture_io.class.subdirs.each do |d| - next if d.empty? - PictureIO.resource_picture_io.put(self.__send__(d, img), self.filename, d) + if res = self.save + if res = self.store_picture(mgk) + if self.to_gif? + if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob) + res = self.store_picture(gifmgk) + else + res = false + end + end end - res = true -# rescue -# res = false -# end + end + res + end + + def store_picture(mgk) + res = false + PictureIO.resource_picture_io.class.subdirs.each do |d| + picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk) + res = PictureIO.resource_picture_io.put(picdata, self.filename, d) + break unless res + end res end