shrine-scp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d218a5be95309cbb6cf56dc828aa398846fc1c6a
4
+ data.tar.gz: f77ec8bedb9ef89fd565e36a587b111a2ef31772
5
+ SHA512:
6
+ metadata.gz: 5638954e75ee62d8a60e76c0bba774d040ec8ff52c70d5822072f7b3e7a93ff2b076a2a52f742daec3ea48faa9a4e0727515419885daf1f2495b9ecb6b16a031
7
+ data.tar.gz: 63c615b69f7fc458ad27d01d3745eede7619c3729cc5abd9795f314cabe45c4850d5cbe5a1893e5fa3e7e6b4febb0a40dbac3544f6807d0f3ff571414bd1f43d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,53 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
+ DisplayCopNames: true
4
+ DefaultFormatter: simple
5
+ Include:
6
+ - Rakefile
7
+ Exclude:
8
+ - test/test_helper.rb
9
+
10
+ # Style
11
+ Style/FileName:
12
+ Enabled: false
13
+
14
+ Style/StringLiterals:
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/IndentationConsistency:
18
+ EnforcedStyle: rails
19
+
20
+ Style/MultilineOperationIndentation:
21
+ EnforcedStyle: aligned
22
+
23
+ Style/MultilineMethodCallIndentation:
24
+ EnforcedStyle: aligned
25
+
26
+ Style/RegexpLiteral:
27
+ Enabled: false
28
+
29
+ Style/Documentation:
30
+ Enabled: false
31
+
32
+ Style/PercentLiteralDelimiters:
33
+ PreferredDelimiters:
34
+ '%w': '[]'
35
+
36
+ # can't enforce style to require
37
+ # the trailing underscore -- so disabling for now
38
+ Style/TrailingUnderscoreVariable:
39
+ Enabled: false
40
+
41
+ # Metrics
42
+ Metrics/LineLength:
43
+ Max: 90
44
+
45
+ Metrics/MethodLength:
46
+ Max: 15
47
+
48
+ Metrics/AbcSize:
49
+ Max: 20
50
+
51
+ # Performance
52
+ Performance/RedundantBlockCall:
53
+ Enabled: false
@@ -0,0 +1 @@
1
+ shrine-scp
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.1
6
+ - 2.2
7
+ - 2.1
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shrine-scp.gemspec
4
+ gemspec
5
+
6
+ gem "pry-byebug"
7
+ gem "rubocop"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 jordanandree
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # Shrine::Scp
2
+
3
+ Scp storage plugin for Shrine attachment and upload toolkit
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shrine-scp'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install shrine-scp
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "shrine/storage/scp"
25
+
26
+ Shrine.storages[:store] = Shrine::Storage::Scp.new(
27
+ directory: "/path/to/uploads" # Required argument
28
+ )
29
+ ```
30
+
31
+ ### Optional Configuration
32
+
33
+ **ssh_host:**
34
+ optional `user@hostname` for remote scp transfers
35
+
36
+ **host:**
37
+ URLs will by default be relative if `:prefix` is set, and you can use this option to set a CDN host (e.g. `//abc123.cloudfront.net`).
38
+
39
+ **prefix:**
40
+ The directory relative to `directory` to which files will be stored, and it is included in the URL.
41
+
42
+ **options:**
43
+ Additional arguments specific to scp. See: [https://linux.die.net/man/1/scp](https://linux.die.net/man/1/scp)
44
+
45
+ **permissions:**
46
+ bit pattern for permissions to set on uploaded files. i.e. group read permissions: `0644`
47
+
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jordanandree/shrine-scp.
56
+
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
61
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ t.warning = false
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shrine/storage/scp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shrine/storage/linter"
5
+ require "shrine/storage/scp"
6
+
7
+ storage = Shrine::Storage::Scp.new(directory: "./tmp/lint")
8
+ Shrine::Storage::Linter.new(storage).call
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,135 @@
1
+ require "shrine"
2
+
3
+ require "fileutils"
4
+ require "tempfile"
5
+
6
+ class Shrine
7
+ module Storage
8
+ class Scp
9
+ attr_reader :directory, :ssh_host, :host, :prefix, :options, :permissions
10
+
11
+ def initialize(directory:, ssh_host: nil, host: nil, prefix: nil, options: %w[-q], permissions: 0600)
12
+ # Initializes a storage for uploading via scp.
13
+ #
14
+ # :directory
15
+ # : the path where files will be transferred to
16
+ #
17
+ # :ssh_host
18
+ # : optional user@hostname for remote scp transfers over ssh
19
+ #
20
+ # :host
21
+ # : URLs will by default be relative if `:prefix` is set, and you
22
+ # can use this option to set a CDN host (e.g. `//abc123.cloudfront.net`).
23
+ #
24
+ # :prefix
25
+ # : The directory relative to `directory` to which files will be stored,
26
+ # and it is included in the URL.
27
+ #
28
+ # :options
29
+ # : Additional arguments specific to scp
30
+ # https://linux.die.net/man/1/scp
31
+ #
32
+ # :permissions
33
+ # : bit pattern for permissions to set on uploaded files
34
+ #
35
+ @directory = directory.chomp(File::SEPARATOR)
36
+ @ssh_host = ssh_host
37
+ @host = host.chomp(File::SEPARATOR) if host
38
+ @prefix = prefix.chomp(File::SEPARATOR) if prefix
39
+ @options = options
40
+ @permissions = permissions
41
+ end
42
+
43
+ def upload(io, id, **)
44
+ file = write_io(io, id)
45
+ scp_up(id, file.path)
46
+ file
47
+ end
48
+
49
+ def download(id)
50
+ file = scp_down(id)
51
+ file
52
+ end
53
+
54
+ def open(id)
55
+ file = scp_down(id)
56
+ file.tap(&:open)
57
+ end
58
+
59
+ def exists?(id)
60
+ file_path = File.join(directory, id)
61
+ bash "ls -la #{file_path}"
62
+ end
63
+
64
+ def url(id, **_options)
65
+ File.join([host, prefix, id].compact)
66
+ end
67
+
68
+ def delete(id)
69
+ file_path = path(id)
70
+ bash "rm -rf #{file_path}"
71
+ end
72
+
73
+ def clear!
74
+ file_path = path("*")
75
+ bash "rm -rf #{file_path}"
76
+ end
77
+
78
+ private
79
+
80
+ def ssh?
81
+ ssh_host
82
+ end
83
+
84
+ def bash(sh)
85
+ command = "bash -c '#{sh}' > /dev/null 2>&1; echo $?"
86
+ command = "ssh #{ssh_host} \"#{command}\"" if ssh_host
87
+ `#{command}`.chomp == "0"
88
+ end
89
+
90
+ def scp_up(id, tmp_path)
91
+ FileUtils.chmod(permissions, tmp_path)
92
+ destination = path(id)
93
+ destination = "#{ssh_host}:#{destination}" if ssh_host
94
+ scp_transfer(source: tmp_path, destination: destination)
95
+ end
96
+
97
+ def scp_down(id)
98
+ source = path(id)
99
+ tmp = tempfile!(id)
100
+
101
+ tmp if scp_transfer(source: source, destination: tmp.path)
102
+ end
103
+
104
+ def scp_transfer(source:, destination:)
105
+ command = [scp_bin, scp_options, source, destination].join(" ")
106
+ system command
107
+ end
108
+
109
+ def scp_bin
110
+ scp_bin = `which scp`.chomp
111
+ raise "scp could not be found." if scp_bin.empty?
112
+ scp_bin
113
+ end
114
+
115
+ def scp_options
116
+ options.join(" ")
117
+ end
118
+
119
+ def path(id)
120
+ File.join([directory, prefix, id].compact)
121
+ end
122
+
123
+ def tempfile!(id)
124
+ Tempfile.new(["shrine-scp-", File.extname(id)], binmode: true)
125
+ end
126
+
127
+ def write_io(io, id)
128
+ tmp = tempfile!(id)
129
+ IO.copy_stream(io, tmp)
130
+ tmp.tap(&:open)
131
+ tmp
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "shrine-scp"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["jordanandree"]
9
+ spec.email = ["jordanandree@gmail.com"]
10
+
11
+ spec.summary = %q{Scp storage for Shrine file attachment toolkit}
12
+ spec.homepage = "https://github.com/jordanandree/shrine-scp"
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "shrine", "~> 2.0"
20
+
21
+ spec.add_development_dependency "rake", "~> 11.0"
22
+ spec.add_development_dependency "minitest", "~> 5.0"
23
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-scp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jordanandree
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shrine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - jordanandree@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/lint
73
+ - bin/setup
74
+ - lib/shrine/storage/scp.rb
75
+ - shrine-scp.gemspec
76
+ - tmp/downloads/.keep
77
+ - tmp/lint/.keep
78
+ - tmp/uploads/.keep
79
+ homepage: https://github.com/jordanandree/shrine-scp
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Scp storage for Shrine file attachment toolkit
103
+ test_files: []