paperclip-swift-swauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2013 DropMyEmail Pte Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Paperclip Swift Swauth
2
+
3
+ This gem is a [Paperclip](https://github.com/thoughtbot/paperclip/) store for [OpenStack Swift](swift.openstack.org). This is different from the [paperclip-swift](https://github.com/helios-technologies/paperclip-swift/) library in the authentication. This library uses [swauth](https://github.com/gholt/swauth) for authentication.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install paperclip-swift-swauth
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ You should specify the following variables in your environment. We don't like storing passwords in the plain.
14
+
15
+ ```
16
+ export SWIFT_USER="user"
17
+ export SWIFT_PASSWORD="some-secret-password"
18
+ export SWIFT_URL="https://your-swift-server.com"
19
+ export SWIFT_TENANT="tenant-name"
20
+ export SWIFT_CONTAINER="container-name"
21
+ ```
22
+
23
+ Set the storage to be __swift_swauth__. The usual Paperclip configuration should be applicable.
24
+
25
+ ``` ruby
26
+ class User < ActiveRecord::Base
27
+ has_attached_file :avatar,
28
+ :storage => :swift_swauth
29
+ end
30
+ ```
31
+
32
+ ## License
33
+
34
+ Apache 2.0. See LICENSE for details.
@@ -0,0 +1,2 @@
1
+ require 'paperclip/storage/swift_swauth'
2
+ require 'paperclip/swift/swauth_client'
@@ -0,0 +1,97 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'swift', 'swauth_client'))
2
+
3
+ # Public: Paperclip storage adaptor for Openstack Swift.
4
+ # Swift authentication is assumed to be on Swauth
5
+ module Paperclip
6
+ module Storage
7
+ module SwiftSwauth
8
+
9
+ # Public: Hook for the base class to run
10
+ def self.extended(base)
11
+ end
12
+
13
+ # Public: Checks that the file exists
14
+ #
15
+ # style - The string which represents the file(default: default_style)
16
+ #
17
+ # Examples
18
+ #
19
+ # swift_swauth.exists 'foo'
20
+ # => true
21
+ #
22
+ # Returns a Boolean
23
+ def exists?(style=default_style)
24
+ client.object_exists?(path(style))
25
+ end
26
+
27
+ # Public: Performs the write operations stored in the write queue
28
+ #
29
+ # Examples
30
+ #
31
+ # swift_swauth.flush_writes
32
+ # => {}
33
+ #
34
+ # Returns nothing
35
+ def flush_writes
36
+ @queued_for_write.each do |style, file|
37
+ write_opts = { content_type: instance_read(:content_type) }
38
+ client.create_object(path(style), write_opts, file)
39
+ end
40
+ after_flush_writes # allows attachment to clean up temp files
41
+ @queued_for_write = {}
42
+ end
43
+
44
+ # Public: Performs the delete operations stored in the delete queue
45
+ #
46
+ # Examples
47
+ #
48
+ # swift_swauth.flush_deletes
49
+ # => []
50
+ #
51
+ # Returns nothing
52
+ def flush_deletes
53
+ @queued_for_delete.each do |path|
54
+ client.delete_object(path) if client.object_exists?(path)
55
+ end
56
+ @queue_for_delete = []
57
+ end
58
+
59
+ # Public: Performs the delete operations stored in the delete queue
60
+ #
61
+ # style - The string which represents the file
62
+ # local_dest_path - The string which represents the location to save the file to
63
+ #
64
+ # Examples
65
+ #
66
+ # swift_swauth.copy_to_local_file('foo', '/path/to/foo')
67
+ # => nil
68
+ #
69
+ # Returns nothing
70
+ def copy_to_local_file(style, local_dest_path)
71
+ if exists?(style)
72
+ local_file = File.open(local_dest_path, 'wb')
73
+ local_file.write(client.object(path(style)).data)
74
+ local_file.close
75
+ end
76
+ end
77
+
78
+ protected
79
+
80
+ # Private: Returns the swift client for performing the operations
81
+ #
82
+ # Examples
83
+ #
84
+ # swift_swauth.client
85
+ # => <Paperclip::Swift::SwauthClient>
86
+ #
87
+ # Returns an instance of SwauthClient
88
+ def client
89
+ if @client.nil?
90
+ @client = Paperclip::Swift::SwauthClient.new
91
+ end
92
+ @client
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,115 @@
1
+ require 'openstack'
2
+
3
+ module Paperclip
4
+ module Swift
5
+ class SwauthClient
6
+ SERVICE_TYPE = 'object-store'
7
+
8
+ # Public: Constructor. Assumes all the needed variables are
9
+ # found in the environment
10
+ def initialize
11
+ @swift_opts = {
12
+ user: ENV['SWIFT_USER'] || '',
13
+ password: ENV['SWIFT_PASSWORD'] || '',
14
+ url: ENV['SWIFT_URL'] || '',
15
+ tenant: ENV['SWIFT_TENANT'] || '',
16
+ container: ENV['SWIFT_CONTAINER'] || ''
17
+ }
18
+ end
19
+
20
+ # Public: Checks if the object exists in the container
21
+ #
22
+ # path - The String which represents the object
23
+ #
24
+ # Examples
25
+ #
26
+ # swauth_client.object_exists 'foo'
27
+ # => true
28
+ #
29
+ # Returns a Boolean
30
+ def object_exists?(path)
31
+ container.object_exists? path
32
+ end
33
+
34
+ # Public: Creates the object given the path, options and data
35
+ #
36
+ # path - The String which represents the object
37
+ # opts - The Hash which contains the options for the object
38
+ # data - The File or String which represents the data
39
+ #
40
+ # Examples
41
+ #
42
+ # swauth_client.create_object 'foo', {}, File.open('foo.pdf')
43
+ # => nil
44
+ #
45
+ # Returns nothing
46
+ def create_object(path, opts={}, data)
47
+ container.create_object path, opts, data
48
+ end
49
+
50
+ # Public: Deletes the object given the path
51
+ #
52
+ # path - The String which represents the object
53
+ #
54
+ # Examples
55
+ #
56
+ # swauth_client.delete_object 'foo'
57
+ # => nil
58
+ #
59
+ # Returns nothing
60
+ def delete_object(path)
61
+ container.delete_object path
62
+ end
63
+
64
+ # Public: Returns the object given the path
65
+ #
66
+ # path - The String which represents the object
67
+ #
68
+ # Examples
69
+ #
70
+ # swauth_client.object 'foo'
71
+ # => <OpenStack::Swift::Container::Object>
72
+ #
73
+ # Returns an instance of OpenStack::Swift::Container::Object
74
+ def object(path)
75
+ container.object path
76
+ end
77
+
78
+ protected
79
+
80
+ # Private: Returns the container object for the connection
81
+ #
82
+ # Examples
83
+ #
84
+ # swauth_client.container
85
+ # => <OpenStack::Swift::Container>
86
+ #
87
+ # Returns an instance of OpenStack::Swift::Container
88
+ def container
89
+ if @connection.nil?
90
+ @connection = OpenStack::Connection.create(connection_opts)
91
+ end
92
+ @connection.container(@swift_opts[:container])
93
+ end
94
+
95
+ # Private: Returns the connection options
96
+ #
97
+ # Examples
98
+ #
99
+ # swauth_client.connection_opts
100
+ # => {...}
101
+ #
102
+ # Returns a Hash of options
103
+ def connection_opts
104
+ {
105
+ username: @swift_opts[:user],
106
+ api_key: @swift_opts[:password],
107
+ auth_url: @swift_opts[:url],
108
+ authtenant_name: @swift_opts[:tenant],
109
+ auth_method: @swift_opts[:password],
110
+ service_type: SERVICE_TYPE
111
+ }
112
+ end
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-swift-swauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Wong Liang Zan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A store for paperclip that uses openstack swift. Authenication is assumed
47
+ to be swauth.
48
+ email:
49
+ - zan@liangzan.net
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/paperclip/storage/swift_swauth.rb
55
+ - lib/paperclip/swift/swauth_client.rb
56
+ - lib/paperclip-swift-swauth.rb
57
+ - LICENSE
58
+ - README.md
59
+ homepage: https://github.com/DropMyEmail/paperclip-swift-swauth
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 1.3.6
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.24
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Paperclip store for openstack swift with swauth authentication
83
+ test_files: []