OSDN Git Service

picture storerer updated
[pettanr/pettanr.git] / app / models / original_picture.rb
index c0032f1..d178db2 100644 (file)
@@ -87,13 +87,32 @@ class OriginalPicture < ActiveRecord::Base
     '/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 store(picture_data, art, lid = nil)
+    res = false
+    begin
+      mgk = Magick::Image.from_blob(picture_data).shift
+    rescue 
+      self.errors.add :base, 'magick failed'
+      return false
+    end
+    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)
+            PictureIO.original_picture_io.delete(self.filename)
+            self.errors.add :base, 'resource picture copying error'
+            raise ActiveRecord::Rollback
+          end
+        else
+          self.errors.add :base, 'original picture io does not work'
+          raise ActiveRecord::Rollback
+        end
+      end
     end
     res
   end