s3streamer 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # S3Streamer
2
+
3
+ Send your files to the end user by streaming them from S3
4
+
5
+ ## Usage
6
+
7
+ ### Installation
8
+
9
+ Add the gem to your Gemfile and Bundle :
10
+
11
+ ```ruby
12
+ gem "s3streamer"
13
+ ```
14
+
15
+ Install initializer file :
16
+
17
+ ```bash
18
+ rails generate s3streamer:install
19
+ ```
20
+
21
+ Edit the initializer file with your AWS S3 credentials and your bucket name,
22
+ and restart your server.
23
+ **Note**: You can avoid setting your bucket if you pass it directly to the
24
+ `#stream_file` method options.
25
+
26
+ ### Controller
27
+
28
+ Now, you can use the `#stream_file` method in your controllers.
29
+
30
+ ```ruby
31
+ def download
32
+ stream_file("file/key.ext")
33
+ end
34
+ ```
35
+
36
+ It can also accept a `Paperclip::Attachment` object. This method will
37
+ automatically add "content_type=xxx/xxx" to the Content-Disposition HTTP
38
+ response header.
39
+
40
+ ```ruby
41
+ def download
42
+ model = Model.find(params[:id])
43
+ stream_file(model.file)
44
+ end
45
+ ```
46
+
47
+ There also are options that you can pass :
48
+
49
+ ```ruby
50
+ def download
51
+ stream_file("file/key.ext", {
52
+ name: "File name to be used.pdf",
53
+ content_type: "application/pdf",
54
+ bucket: "your_bucket"
55
+ })
56
+ end
57
+ ```
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'S3streamer'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,14 @@
1
+ module S3streamer
2
+ class InstallGenerator < Rails::Generators::Base
3
+ # Copied files come from templates folder
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ # Generator desc
7
+ desc "S3Streamer install generator"
8
+
9
+ def copy_initializer_file
10
+ say "Installing default initializer template"
11
+ copy_file "initializer.rb", "config/initializers/s3streamer.rb"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ S3Streamer.config do |config|
2
+ # Defines the default bucket to be used when streaming a file
3
+ #
4
+ config.bucket = nil
5
+
6
+ # AWS credentials key to sign in
7
+ #
8
+ config.key = ""
9
+
10
+ # AWS credentials secret to sign in
11
+ #
12
+ config.secret = ""
13
+
14
+ # Wether or not to configure aws-sdk with credentials from inializer.
15
+ #
16
+ # Defaults to true, since key and secret are needed, but you may already have
17
+ # initialized config somewhere else, so you just have to toggle the variable
18
+ # to false
19
+ #
20
+ # config.config_auth = false
21
+ end
data/lib/s3streamer.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "s3streamer/engine"
2
+
3
+ module S3Streamer
4
+ # Defines the default bucket to be used when streaming a file
5
+ #
6
+ mattr_accessor :bucket
7
+
8
+ # AWS credentials key to sign in
9
+ #
10
+ mattr_accessor :key
11
+
12
+ # AWS credentials secret to sign in
13
+ #
14
+ mattr_accessor :secret
15
+
16
+ # Wether or not to configure aws-sdk with credentials from inializer.
17
+ #
18
+ mattr_accessor :config_auth
19
+ @@config_auth = true
20
+
21
+ def self.config
22
+ yield self if block_given?
23
+ end
24
+
25
+ # Autoloaded components
26
+ autoload :Streamer, "s3streamer/streamer"
27
+ autoload :ControllerHelpers, "s3streamer/controller_helpers"
28
+ end
@@ -0,0 +1,31 @@
1
+ module S3Streamer
2
+ module ControllerHelpers
3
+ def stream_file item, options = {}
4
+ case item.class.to_s
5
+ when "Paperclip::Attachment"
6
+ path = item.path.gsub(/^\//, '')
7
+ options[:name] ||= File.basename(item.original_filename)
8
+ options[:content_type] ||= item.content_type
9
+ else
10
+ path = item
11
+ begin
12
+ options[:name] ||= File.basename(item)
13
+ rescue TypeError => e
14
+ raise TypeError.new e.message +
15
+ " -- #stream_file only accepts String or Paperclip::Attachment" +
16
+ " as its first argument"
17
+ end
18
+ end
19
+
20
+ content_disposition = "attachment; filename=#{ options[:name] }"
21
+ # Add content type if we have it
22
+ if options[:content_type]
23
+ content_disposition += "; content-type:#{ options[:content_type] }"
24
+ end
25
+ # Set Content-Disposition HTTP header
26
+ response.headers['Content-Disposition'] = content_disposition
27
+
28
+ self.response_body = Streamer.new(path, options[:bucket])
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require "rails"
2
+ require "aws"
3
+
4
+ module S3Streamer
5
+ class Engine < Rails::Engine
6
+ initializer "Initialize S3 Streamer" do |app|
7
+ # Include controller helpers in application controller
8
+ app.config.to_prepare do
9
+ ActiveSupport.on_load :action_controller do
10
+ ApplicationController.send(:include, ControllerHelpers)
11
+ end
12
+ end
13
+
14
+ # Configure aws-sdk authentication
15
+ AWS.config(
16
+ access_key_id: S3Streamer.key,
17
+ secret_access_key: S3Streamer.secret
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module S3Streamer
2
+ class Streamer
3
+ attr_accessor :object
4
+ # Creates a Stremer object by fetching object the to be stremed
5
+ # and storing it for further processing
6
+ #
7
+ # @param [String] key The path to the object in the bucket
8
+ # @param [String] bucket The bucket name to use. Can be omitted to use
9
+ # the default configured bucket
10
+ #
11
+ def initialize key, bucket = nil
12
+ # Use default bucket if not set
13
+ bucket ||= S3Streamer.bucket
14
+ # Fetch object to be streamed
15
+ @object = AWS::S3::Bucket.new(bucket).objects[key]
16
+ end
17
+
18
+ # Method to be called by Rack to stream our stored object
19
+ #
20
+ def each
21
+ @object.read do |segment|
22
+ yield segment
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module S3streamer
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :s3streamer do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3streamer
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Valentin Ballestrino
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.13
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.13
30
+ - !ruby/object:Gem::Dependency
31
+ name: aws-sdk
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.10'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.10'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Simple S3 File streaming to client
79
+ email:
80
+ - vala@glyph.fr
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/generators/s3streamer/install/install_generator.rb
86
+ - lib/generators/s3streamer/install/templates/initializer.rb
87
+ - lib/s3streamer/controller_helpers.rb
88
+ - lib/s3streamer/engine.rb
89
+ - lib/s3streamer/streamer.rb
90
+ - lib/s3streamer/version.rb
91
+ - lib/s3streamer.rb
92
+ - lib/tasks/s3streamer_tasks.rake
93
+ - MIT-LICENSE
94
+ - Rakefile
95
+ - README.md
96
+ homepage: http://www.glyph.fr
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.24
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Simple S3 File streaming to client
120
+ test_files: []