OSDN Git Service

destroy picture file with record t#27036
authoryasushiito <yas@pen-chan.jp>
Tue, 3 Jan 2012 09:30:58 +0000 (18:30 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 3 Jan 2012 09:30:58 +0000 (18:30 +0900)
app/controllers/original_pictures_controller.rb
app/controllers/system_pictures_controller.rb
app/models/original_picture.rb
app/models/resource_picture.rb
app/models/system_picture.rb
lib/local_picture.rb
lib/s3_picture.rb

index ac85efe..0b651ff 100644 (file)
@@ -174,8 +174,10 @@ class OriginalPicturesController < ApplicationController
       end
       return
     end
-    @original_picture.destroy
-
+    OriginalPicture.transaction do
+      @original_picture.destroy
+    end
+    
     respond_to do |format|
       format.html { redirect_to original_pictures_url }
       format.json { head :ok }
index a1de9cc..c671774 100644 (file)
@@ -125,8 +125,11 @@ class SystemPicturesController < ApplicationController
   # DELETE /system_pictures/1.json
   def destroy
     @system_picture = SystemPicture.find(params[:id])
-    @system_picture.destroy
-
+    SystemPicture.transaction do
+      @system_picture.destroy
+      
+    end
+    
     respond_to do |format|
       format.html { redirect_to system_pictures_url }
       format.json { head :ok }
index d67a581..dacbb6f 100644 (file)
@@ -3,10 +3,17 @@ class OriginalPicture < ActiveRecord::Base
   belongs_to :lisence
   has_one :resource_picture
   
+  before_destroy :destroy_with_file
+  
   def validate
     errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
   end
   
+  def destroy_with_file
+    PictureIO.original_picture_io.delete self.filename
+    self.resource_picture.destroy
+  end
+  
   def dext
     self.ext.downcase
   end
index 1bf9021..391c2db 100644 (file)
@@ -4,6 +4,16 @@ class ResourcePicture < ActiveRecord::Base
   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
index 3fe6007..9d01831 100644 (file)
@@ -1,11 +1,17 @@
 class SystemPicture < ActiveRecord::Base
   has_many :balloons
   has_many :balloon_templates
-
+  
+  before_destroy :destroy_with_file
+  
   def validate
     errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
   end
   
+  def destroy_with_file
+    PictureIO.system_picture_io.delete self.filename
+  end
+  
   def dext
     self.ext.downcase
   end
index 536ab22..74f020c 100644 (file)
@@ -32,6 +32,10 @@ class PictureIO
       Dir.mkdir(dir(subdir)) unless File.exist?(dir(subdir))
     end
     
+    def exist?(filename, subdir = nil)
+      File.exist?(dir(subdir) + filename)
+    end
+    
     def put(bindata, filename, subdir = nil)
       mkdir subdir
       open(dir(subdir) + filename, 'wb') do |f|
@@ -47,5 +51,9 @@ class PictureIO
       bindata
     end
     
+    def delete(filename, subdir = nil)
+      File.delete(dir(subdir) + filename)
+    end
+    
   end
 end
index f6d7bf8..57bfa6c 100644 (file)
@@ -27,6 +27,10 @@ class PictureIO
       sd
     end
     
+    def exist?(filename, subdir = nil)
+      AWS::S3::S3Object.exist?(dir(subdir) + filename)
+    end
+    
     def put(bindata, filename, subdir = nil)
       AWS::S3::S3Object.store(dir(subdir) + filename, bindata, base)
     end
@@ -40,29 +44,9 @@ class PictureIO
       bindata
     end
     
-    def store(dt)
-      iw, ih = Image.fix_size_both(80, 80, dt.columns, dt.rows)
-      n = dt.to_blob
-      v = dt.flip.to_blob
-      h = dt.flop.to_blob
-      vh = dt.flip.flop.to_blob
-      AWS::S3::S3Object.store(filename, n, 'pettanr')
-      AWS::S3::S3Object.store('v/' + filename, v, 'pettanr') if v
-      AWS::S3::S3Object.store('h/' + filename, h, 'pettanr') if h
-      AWS::S3::S3Object.store('vh/' + filename, vh, 'pettanr') if vh
-      
-      t = Image.resize(dt.to_blob, iw, ih).to_blob
-      AWS::S3::S3Object.store('thumbnail/' + filename, t, 'pettanr')
+    def delete(filename, subdir = nil)
+      AWS::S3::S3Object.delete(dir(subdir) + filename, base)
     end
     
-    def restore(turn)
-      dt = ''
-  #      if AWS::S3::S3Object.exists?(fn, 'pettanr')
-      d = turn ? "#{turn}/" : ''
-      AWS::S3::S3Object.stream(d + filename, 'pettanr') do |st|
-        dt += st if st
-      end
-      dt
-    end
   end
 end