dragonfly-scp_data_store 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a48d4694dcc5fb0024b55eb2293aa53bb7759fde
4
+ data.tar.gz: 0a3e0830885538c850654610858d30ad628e8e70
5
+ SHA512:
6
+ metadata.gz: af032ab8450733593eec8cf890c73d171558dad9872b7d87903bfc8f8e6d778013c2aff19189d24c17ea3057f56764b2e5f358fd5c6438c9e80b6419cc93584f
7
+ data.tar.gz: 060d8e1da49e644a241fd2cb6ab4dd1b48f477463e099fd90156b2b7219d8f4b508b4f411f376d7c8e2077885321392e2cc1d8ec850e01549e8e36d9e42f117b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dragonfly-scp_data_store.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thiago Belem, Matheus Bras
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Dragonfly::ScpDataStore
2
+
3
+ A dragonfly datastore adaptar for saving your files on remote servers using Net::SCP and Net::SSH.
4
+
5
+ ## Installation
6
+
7
+ Drop this line to your application's Gemfile:
8
+
9
+ gem 'dragonfly-scp_data_store'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dragonfly-scp_data_store
18
+
19
+ ## Usage
20
+
21
+ On your dragonfly initializer:
22
+
23
+ ```ruby
24
+ app.configure do |c|
25
+ c.datastore = Dragonfly::ScpDataStore::DataStore.new(
26
+ host: ENV['CDN_SSH_HOST'], # This should be the host address of your remote server
27
+ username: ENV['CDN_SSH_USERNAME'], # The username of your server
28
+ password: ENV['CDN_SSH_PASSWORD'], # The password
29
+ folder: ENV['CDN_FOLDER'], # The folder in which you're going to store your files
30
+ base_url: ENV['CDN_BASE_URL'], # The url which you're going to retrieve your files from the server
31
+ )
32
+ end
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dragonfly/scp_data_store/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dragonfly-scp_data_store"
8
+ spec.version = Dragonfly::ScpDataStore::VERSION
9
+ spec.authors = ["Matheus Bras", "Thiago Belem"]
10
+ spec.email = ["bras.matheus@gmail.com", "contato@thiagobelem.net"]
11
+ spec.description = %q{A dragonfly datastore adapter for saving your images on remote stores using scp and ssh.}
12
+ spec.summary = %q{A dragonfly datastore adapter}
13
+ spec.homepage = "http://github.com/matheusbras/dragonfly-scp_data_store"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dragonfly", '>= 0.9'
22
+ spec.add_dependency 'net-ssh', '2.6.8'
23
+ spec.add_dependency 'net-scp', '1.1.2'
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "activesupport", "4.0.0"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", '>= 2.6.0'
28
+ end
@@ -0,0 +1,90 @@
1
+ require "dragonfly/scp_data_store/version"
2
+ require 'dragonfly'
3
+ require 'net/http'
4
+ require 'net/ssh'
5
+ require 'net/scp'
6
+
7
+ module Dragonfly
8
+ module ScpDataStore
9
+ class DataStore
10
+ include ::Dragonfly::Configurable
11
+
12
+ configurable_attr :host
13
+ configurable_attr :username
14
+ configurable_attr :password
15
+ configurable_attr :folder
16
+ configurable_attr :base_url
17
+
18
+ def initialize(options = {})
19
+ self.host = options[:host]
20
+ self.username = options[:username]
21
+ self.password = options[:password]
22
+ self.folder = options[:folder]
23
+ self.base_url = options[:base_url]
24
+ end
25
+
26
+ def store(temp_object, opts={})
27
+ uid = generate_uid(temp_object.name || 'file')
28
+
29
+ process_file(temp_object.file.path, uid)
30
+
31
+ uid
32
+ end
33
+
34
+ def retrieve(uid)
35
+ uri = URI(base_url + uid)
36
+ response = Net::HTTP.get_response(uri)
37
+
38
+ if response.is_a? Net::HTTPSuccess
39
+ [response.body, { }]
40
+ else
41
+ raise DataNotFound
42
+ end
43
+ end
44
+
45
+ def destroy(uid)
46
+ raise DataNotFound
47
+ end
48
+
49
+ private
50
+ def generate_uid(file)
51
+ name = File.basename(file, '.*').parameterize
52
+ extension = File.extname(file).downcase
53
+
54
+ time = Time.now.strftime '%Y/%m/%d/%H/%M'
55
+ hash = SecureRandom.hex(5)
56
+
57
+ "#{time}/#{hash}/#{name}#{extension}"
58
+ end
59
+
60
+ def process_file(file, uid)
61
+ remote_file = folder + uid
62
+ remote_folder = File.dirname(remote_file)
63
+
64
+ create_remote_folder(remote_folder)
65
+ upload_file(file, remote_file)
66
+ chmod(remote_file)
67
+ end
68
+
69
+ def create_remote_folder(folder)
70
+ exec("mkdir -m 777 -p #{folder}")
71
+ end
72
+
73
+ def chmod(file, mode = 755)
74
+ exec("chmod #{mode} #{file}")
75
+ end
76
+
77
+ def upload_file(path, remote_path)
78
+ Net::SCP.start(host, username, password: password) do |scp|
79
+ scp.upload!(path, remote_path)
80
+ end
81
+ end
82
+
83
+ def exec(command)
84
+ Net::SSH.start(host, username, password: password) do |ssh|
85
+ ssh.exec!(command)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,5 @@
1
+ module Dragonfly
2
+ module ScpDataStore
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dragonfly::ScpDataStore::DataStore do
4
+ let(:fixture_file) { File.new(File.expand_path("spec/fixtures/bike.jpg")) }
5
+
6
+ describe "attributes" do
7
+ let(:data_store) do
8
+ Dragonfly::ScpDataStore::DataStore.new(
9
+ host: "localhost",
10
+ username: "root",
11
+ password: "password",
12
+ folder: "images",
13
+ base_url: "localhost:3000/images",
14
+ )
15
+ end
16
+
17
+ it { data_store.host.should eq("localhost") }
18
+ it { data_store.username.should eq("root") }
19
+ it { data_store.password.should eq("password") }
20
+ it { data_store.folder.should eq("images") }
21
+ it { data_store.base_url.should eq("localhost:3000/images") }
22
+ end
23
+
24
+ describe "#store" do
25
+ let(:temp_object) { Dragonfly::TempObject.new(fixture_file) }
26
+ let(:data_store) do
27
+ Dragonfly::ScpDataStore::DataStore.new(
28
+ host: "localhost",
29
+ username: "root",
30
+ password: "password",
31
+ folder: "../fixtures/images",
32
+ base_url: "localhost:3000/images",
33
+ )
34
+ end
35
+
36
+ before do
37
+ data_store.should_receive(:create_remote_folder).exactly(2).times.and_return(true)
38
+ data_store.should_receive(:upload_file).exactly(2).times.and_return(true)
39
+ data_store.should_receive(:chmod).exactly(2).times.and_return(true)
40
+ end
41
+
42
+ it "returns an uid unique uid" do
43
+ data_store.store(temp_object).should_not eq(data_store.store(temp_object))
44
+ end
45
+
46
+ end
47
+ end
Binary file
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'active_support/inflector'
4
+ Bundler.setup(:default, :test)
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'rspec'
9
+ require 'dragonfly/scp_data_store'
10
+
11
+ def test_app
12
+ Dragonfly::App.send(:new)
13
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dragonfly-scp_data_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matheus Bras
8
+ - Thiago Belem
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dragonfly
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0.9'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: net-ssh
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '='
33
+ - !ruby/object:Gem::Version
34
+ version: 2.6.8
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '='
40
+ - !ruby/object:Gem::Version
41
+ version: 2.6.8
42
+ - !ruby/object:Gem::Dependency
43
+ name: net-scp
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 1.1.2
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.1.2
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '1.3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: activesupport
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '='
75
+ - !ruby/object:Gem::Version
76
+ version: 4.0.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '='
82
+ - !ruby/object:Gem::Version
83
+ version: 4.0.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: 2.6.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: 2.6.0
112
+ description: A dragonfly datastore adapter for saving your images on remote stores
113
+ using scp and ssh.
114
+ email:
115
+ - bras.matheus@gmail.com
116
+ - contato@thiagobelem.net
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - .gitignore
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - dragonfly-scp_data_store.gemspec
127
+ - lib/dragonfly/scp_data_store.rb
128
+ - lib/dragonfly/scp_data_store/version.rb
129
+ - spec/dragonfly-scp_data_store/dragonfly-scp_data_store_spec.rb
130
+ - spec/fixtures/bike.jpg
131
+ - spec/spec_helper.rb
132
+ homepage: http://github.com/matheusbras/dragonfly-scp_data_store
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.0.3
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A dragonfly datastore adapter
156
+ test_files:
157
+ - spec/dragonfly-scp_data_store/dragonfly-scp_data_store_spec.rb
158
+ - spec/fixtures/bike.jpg
159
+ - spec/spec_helper.rb