OSDN Git Service

fix picture io:
authoryasushiito <yas@pen-chan.jp>
Tue, 11 Aug 2015 23:21:29 +0000 (08:21 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 11 Aug 2015 23:21:29 +0000 (08:21 +0900)
app/views/original_pictures/show.html.erb
config/application.rb
config/initializers/aws.rb [deleted file]
config/picture_io.yml.org
lib/local_picture.rb
lib/s3_picture.rb

index 01e285a..139e05d 100644 (file)
@@ -89,7 +89,7 @@
       <h1>
         <%= t 'original_pictures.show.history' %>
       </h1>
-      <%= render 'history_list', :history => @item.history %>
+      <%= render 'history_list', :history => OriginalPicture.find_history(@item.id) %>
     </td>
   </tr>
 </table>
index e358681..6a5f031 100644 (file)
@@ -64,11 +64,12 @@ end
 y = YAML.load(open(Rails.root + 'config/picture_io.yml').read)
 require y[Rails.env]["adapter"]
 pio = PictureIO.const_get y[Rails.env]["io"]
+host_dir = y[Rails.env]["host_dir"]
 PictureIO.setup do |config|
-  config.original_picture_io = pio.new y[Rails.env]["original_picture"]
-  config.resource_picture_io = pio.new y[Rails.env]["resource_picture"]
-  config.picture_io = pio.new y[Rails.env]["picture"]
-  config.system_picture_io = pio.new y[Rails.env]["system_picture"]
+  config.original_picture_io = pio.new host_dir, y[Rails.env]["original_picture"]
+  config.resource_picture_io = pio.new host_dir, y[Rails.env]["resource_picture"]
+  config.picture_io = pio.new host_dir, y[Rails.env]["picture"]
+  config.system_picture_io = pio.new host_dir, y[Rails.env]["system_picture"]
 end
 module ActiveRecord
   class Forbidden < ActiveRecordError
diff --git a/config/initializers/aws.rb b/config/initializers/aws.rb
deleted file mode 100644 (file)
index bd3c3fb..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-Aws.config.update(
-  region: Rails.application.secrets.aws_region, 
-  credentials: Aws::Credentials.new(
-    Rails.application.secrets.aws_access_key_id, 
-    Rails.application.secrets.aws_secret_access_key
-  )
-)
-#require 'aws/s3'
-#AWS::S3::Base.establish_connection!(
-#  :access_key_id => Rails.application.secrets.aws_access_key_id, 
-#  :secret_access_key => Rails.application.secrets.aws_secret_access_key, 
-#)
-#AWS::S3::DEFAULT_HOST = "us-east-1.amazonaws.com"
-# :s3_endpoint => "us-east-1.amazonaws.com"
index d17e7fe..85cd6bd 100644 (file)
@@ -1,23 +1,26 @@
 development:
   adapter: local_picture
   io: LocalPicture
-  original_picture: /pettanr/dev/o/
-  resource_picture: /pettanr/dev/r/
-  picture: /temp/p/p/
-  system_picture: /pettanr/dev/s/
+  host_dir: /pettanr/dev/
+  original_picture: o
+  resource_picture: r
+  picture: p
+  system_picture: s
 
 test:
   adapter: local_picture
   io: LocalPicture
-  original_picture: /pettanr/test/o/
-  resource_picture: /pettanr/test/r/
-  picture: /temp/p/tp/
-  system_picture: /pettanr/test/s/
+  host_dir: /pettanr/test/
+  original_picture: o
+  resource_picture: r
+  picture: p
+  system_picture: s
 
 production:
   adapter: s3_picture
   io: S3Picture
-  original_picture: pettan/o/
-  resource_picture: pettan/r/
-  picture: pettan/p/
-  system_picture: pettan/s/
+  host_dir: pettanr
+  original_picture: o
+  resource_picture: r
+  picture: p
+  system_picture: s
index bdaf976..6801527 100644 (file)
@@ -1,29 +1,38 @@
 require 'picture_io'
 class PictureIO
   class LocalPicture
-    def initialize base_dir = Rails.root + 'public/images/'
-      @base = base_dir
+    def initialize host_dir = Rails.root + 'public/images/', base_dir = ''
+      @host_dir = host_dir
+      @base_dir = @host_dir + base_dir
     end
     
-    def base
-      @base
+    def host_dir
+      @host_dir
     end
     
-    def base=(b)
-      @base = b
+    def host_dir=(d)
+      @host_dir = d
+    end
+    
+    def base_dir
+      @base_dir
+    end
+    
+    def base_dir=(d)
+      @base_dir = d
     end
     
     def dir(subdir = nil)
       sd = if subdir.to_s.empty?
-        self.base
+        ''
       else
-        self.base + (subdir.to_s + '/')
+        subdir.to_s + '/'
       end
-      sd
+      @base_dir + '/' + sd
     end
     
     def mkdir subdir = nil
-      Dir.mkdir(base) unless File.exist?(base)
+      Dir.mkdir(base_dir) unless File.exist?(base_dir)
       return if subdir.to_s.empty?
       Dir.mkdir(dir(subdir)) unless File.exist?(dir(subdir))
     end
index 6e7df83..1f78e62 100644 (file)
@@ -1,18 +1,36 @@
 require 'picture_io'
 class PictureIO
+  Aws.config.update(
+    region: Rails.application.secrets.aws_region, 
+    credentials: Aws::Credentials.new(
+      Rails.application.secrets.aws_access_key_id, 
+      Rails.application.secrets.aws_secret_access_key
+    )
+  )
   class S3Picture
     @@client = Aws::S3::Client.new
     
-    def initialize base_dir = 'pettanr'
-      @base = base_dir
+    def initialize host_dir = 'pettanr', base_dir = ''
+      @host_dir = host_dir
+      @base_dir = base_dir
+      s3 = Aws::S3::Resource.new
+      @bucket = s3.bucket(@host_dir)
     end
     
-    def base
-      @base
+    def self.client
+      @@client
     end
     
-    def base=(b)
-      @base = b
+    def bucket
+      @bucket
+    end
+    
+    def host_dir
+      @host_dir
+    end
+    
+    def host_dir=(d)
+      @host_dir = d
     end
     
     def self.subdirs
@@ -31,7 +49,7 @@ class PictureIO
     def exist?(filename, subdir = nil)
       res = true
       begin
-        res = @@client.get_object(bucket: self.base, key: dir(subdir) + filename).exist?
+        res = self.bucket.object(dir(subdir) + filename).exists?
       rescue
         raise PictureIO::Error
       end
@@ -41,7 +59,7 @@ class PictureIO
     def put(bindata, filename, subdir = nil)
       res = true
       begin
-        @@client.put_object(bucket: self.base, key: dir(subdir) + filename, body: bindata)
+        @@client.put_object(bucket: self.host_dir, key: dir(subdir) + filename, body: bindata)
       rescue
         raise PictureIO::Error
       end
@@ -51,7 +69,7 @@ class PictureIO
     def get(filename, subdir = nil)
       bindata = ''
       begin
-        @@client.get_object(bucket: self.base, key: dir(subdir) + filename) do |st|
+        @@client.get_object(bucket: self.host_dir, key: dir(subdir) + filename) do |st|
           bindata += st if st
         end
       rescue
@@ -63,7 +81,7 @@ class PictureIO
     def delete(filename, subdir = nil)
       res = true
       begin
-        @@client.delete_object(bucket: self.base, key: dir(subdir) + filename)
+        @@client.delete_object(bucket: self.host_dir, key: dir(subdir) + filename)
       rescue
         raise PictureIO::Error
       end