From c006f551f5bdc54b4fe3356098261748a66a9c3c Mon Sep 17 00:00:00 2001 From: yasushiito Date: Wed, 12 Aug 2015 08:21:29 +0900 Subject: [PATCH] fix picture io: --- app/views/original_pictures/show.html.erb | 2 +- config/application.rb | 9 ++++---- config/initializers/aws.rb | 14 ------------ config/picture_io.yml.org | 27 ++++++++++++---------- lib/local_picture.rb | 29 +++++++++++++++-------- lib/s3_picture.rb | 38 +++++++++++++++++++++++-------- 6 files changed, 68 insertions(+), 51 deletions(-) delete mode 100644 config/initializers/aws.rb diff --git a/app/views/original_pictures/show.html.erb b/app/views/original_pictures/show.html.erb index 01e285a1..139e05d7 100644 --- a/app/views/original_pictures/show.html.erb +++ b/app/views/original_pictures/show.html.erb @@ -89,7 +89,7 @@

<%= t 'original_pictures.show.history' %>

- <%= render 'history_list', :history => @item.history %> + <%= render 'history_list', :history => OriginalPicture.find_history(@item.id) %> diff --git a/config/application.rb b/config/application.rb index e3586812..6a5f0317 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 index bd3c3fba..00000000 --- a/config/initializers/aws.rb +++ /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" diff --git a/config/picture_io.yml.org b/config/picture_io.yml.org index d17e7fec..85cd6bd5 100644 --- a/config/picture_io.yml.org +++ b/config/picture_io.yml.org @@ -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 diff --git a/lib/local_picture.rb b/lib/local_picture.rb index bdaf9764..6801527c 100644 --- a/lib/local_picture.rb +++ b/lib/local_picture.rb @@ -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 diff --git a/lib/s3_picture.rb b/lib/s3_picture.rb index 6e7df838..1f78e628 100644 --- a/lib/s3_picture.rb +++ b/lib/s3_picture.rb @@ -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 -- 2.11.0