asset_uploader 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +23 -0
- data/Rakefile +14 -0
- data/asset_uploader.gemspec +17 -0
- data/bin/asset_uploader +74 -0
- data/lib/asset_uploader/uploader.rb +49 -0
- data/lib/asset_uploader/version.rb +3 -0
- data/lib/asset_uploader.rb +36 -0
- data/test/test_asset_uploader.rb +22 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dave Thomas
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# AssetUploader
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'asset_uploader'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install asset_uploader
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/asset_uploader/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dave Thomas"]
|
6
|
+
gem.email = ["dave@pragprog.com"]
|
7
|
+
gem.description = %q{Support uploading of checksummed assets to S3}
|
8
|
+
gem.summary = gem.description
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "asset_uploader"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AssetUploader::VERSION
|
17
|
+
end
|
data/bin/asset_uploader
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Usage:
|
3
|
+
#
|
4
|
+
# asset_uploader [ --no-upload | --no-sign ] target_prefix asset asset...
|
5
|
+
#
|
6
|
+
# Takes the files (or files under directories) named by asset... and uploads them
|
7
|
+
# to S3, renaming each file to include a checksum. Prints the path for each
|
8
|
+
# file uploaded.
|
9
|
+
#
|
10
|
+
# The target_prefix specifies where the file is stored on S3.
|
11
|
+
#
|
12
|
+
# The --no-upload option prints the path but does not upload
|
13
|
+
#
|
14
|
+
# The --no-sign option uploads the file with no checksum. Files uploaded with
|
15
|
+
# no checksum will be hard to change, as the caching front end won't know they
|
16
|
+
# have changed
|
17
|
+
#
|
18
|
+
# You need to set the S3 credentials in your environment
|
19
|
+
#
|
20
|
+
# export AU_ACCESS_KEY_ID=...
|
21
|
+
# export SECRET_ACCESS_KEY=...
|
22
|
+
# export AU_BUCKET_NAME=...
|
23
|
+
#
|
24
|
+
# The default bucket name for assets is a-origin.pragprog.com
|
25
|
+
#
|
26
|
+
# Examples
|
27
|
+
#
|
28
|
+
# asset_uploader magazines/2012-06/images images
|
29
|
+
#
|
30
|
+
# asset_uploader --no-sign newsletter/2012-06-04 newsletter.html
|
31
|
+
|
32
|
+
require 'optparse'
|
33
|
+
|
34
|
+
begin
|
35
|
+
gem 'asset_uploader'
|
36
|
+
rescue Gem::LoadError
|
37
|
+
end
|
38
|
+
|
39
|
+
require 'asset_uploader'
|
40
|
+
|
41
|
+
def usage
|
42
|
+
STDERR.puts File.read(__FILE__).sub(/^\s*$.*/m, '').gsub(/^#/, '')
|
43
|
+
exit
|
44
|
+
end
|
45
|
+
|
46
|
+
def process(prefix, asset, opt)
|
47
|
+
au = AssetUploader.new(prefix, asset)
|
48
|
+
au.sign if opt[:sign]
|
49
|
+
if opt[:upload]
|
50
|
+
s3_path = au.upload
|
51
|
+
else
|
52
|
+
s3_path = au.target_path
|
53
|
+
end
|
54
|
+
puts "#{asset}\t#{s3_path}"
|
55
|
+
end
|
56
|
+
|
57
|
+
opt = { :upload => true, :sign => true }
|
58
|
+
|
59
|
+
parser = OptionParser.new
|
60
|
+
parser.on("-n", "--no-upload") { opt[:upload] = false }
|
61
|
+
parser.on("-u", "--no-sign" ) { opt[:sign] = false }
|
62
|
+
parser.on("-h", "--help") { usage }
|
63
|
+
|
64
|
+
params = parser.parse(ARGV)
|
65
|
+
prefix = params.shift || usage
|
66
|
+
|
67
|
+
until params.empty?
|
68
|
+
asset = params.shift
|
69
|
+
if File.directory?(asset)
|
70
|
+
Dir.glob("#{asset}/*").each {|f| params.unshift(f) }
|
71
|
+
else
|
72
|
+
process(prefix, asset, opt)
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'aws-s3'
|
3
|
+
require 'aws/s3'
|
4
|
+
|
5
|
+
class AssetUploader
|
6
|
+
class Uploader
|
7
|
+
|
8
|
+
def self.get_env(name)
|
9
|
+
ENV[name] || begin
|
10
|
+
STDERR.puts "#{name} is not set in your environment"
|
11
|
+
exit(1)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
S3_CONFIG = {
|
16
|
+
:access_key_id => get_env('AU_ACCESS_KEY_ID'),
|
17
|
+
:secret_access_key => get_env('SECRET_ACCESS_KEY'),
|
18
|
+
:bucket_name => get_env('AU_BUCKET_NAME')
|
19
|
+
}
|
20
|
+
|
21
|
+
def do_upload(name, filename)
|
22
|
+
content = open(filename)
|
23
|
+
connect_to_s3
|
24
|
+
bucket = S3_CONFIG[:bucket_name]
|
25
|
+
ext = File.extname(filename)
|
26
|
+
AWS::S3::S3Object.store(name,
|
27
|
+
content,
|
28
|
+
bucket,
|
29
|
+
:access => :public_read)
|
30
|
+
# :content_type => MIME_TYPES[ext],
|
31
|
+
# :content_disposition => 'attachment')
|
32
|
+
AWS::S3::S3Object.url_for(name, bucket)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def connect_to_s3
|
38
|
+
self.class.connect_to_s3
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.connect_to_s3
|
42
|
+
AWS::S3::Base.establish_connection!(
|
43
|
+
:access_key_id => S3_CONFIG[:access_key_id],
|
44
|
+
:secret_access_key => S3_CONFIG[:secret_access_key]
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "asset_uploader/version"
|
2
|
+
require "asset_uploader/uploader"
|
3
|
+
|
4
|
+
require 'digest/sha1'
|
5
|
+
|
6
|
+
class AssetUploader
|
7
|
+
|
8
|
+
SIGNATURE_LENGTH = 6
|
9
|
+
PADDING = "x" * SIGNATURE_LENGTH
|
10
|
+
|
11
|
+
def initialize(prefix, path_to_asset)
|
12
|
+
@prefix = prefix
|
13
|
+
@path_to_asset = path_to_asset
|
14
|
+
@original_file_name = @target_file_name = File.basename(@path_to_asset)
|
15
|
+
end
|
16
|
+
|
17
|
+
def sign
|
18
|
+
content = File.read(@path_to_asset)
|
19
|
+
digest = Digest::SHA1.digest(content)
|
20
|
+
digest = digest.unpack("q").first
|
21
|
+
digest = -digest if digest < 0
|
22
|
+
digest = "#{digest.to_s(36)}#{PADDING}"[0, SIGNATURE_LENGTH]
|
23
|
+
@target_file_name = @original_file_name.sub(/\.(\w+)$/) { "__#{digest}__.#{$1}" }
|
24
|
+
end
|
25
|
+
|
26
|
+
def target_path
|
27
|
+
File.join(@prefix, @target_file_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def upload
|
31
|
+
uploader = Uploader.new
|
32
|
+
path = uploader.do_upload(target_path, @path_to_asset)
|
33
|
+
path.sub(/s3.amazonaws.com\//, '').sub(/\?AWS.*/, '').sub(/-origin/, '')
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'asset_uploader'
|
3
|
+
require 'flexmock/test_unit'
|
4
|
+
|
5
|
+
class TestAssetUploader < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_initial_state
|
8
|
+
au = AssetUploader.new('prefix', 'myfile.jpg')
|
9
|
+
assert_equal "prefix/myfile.jpg", au.target_path
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_signing
|
13
|
+
au = AssetUploader.new('prefix', 'myfile.jpg')
|
14
|
+
flexmock(File).should_receive(:read).with('myfile.jpg').and_return('something')
|
15
|
+
digest = 1234567890
|
16
|
+
flexmock(Digest::SHA1).should_receive(:digest).and_return([digest].pack('q'))
|
17
|
+
signed_path = "myfile__#{digest.to_s(36)}__.jpg"
|
18
|
+
assert_equal signed_path, au.sign
|
19
|
+
assert_equal File.join('prefix', signed_path), au.target_path
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asset_uploader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dave Thomas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Support uploading of checksummed assets to S3
|
15
|
+
email:
|
16
|
+
- dave@pragprog.com
|
17
|
+
executables:
|
18
|
+
- asset_uploader
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- asset_uploader.gemspec
|
28
|
+
- bin/asset_uploader
|
29
|
+
- lib/asset_uploader.rb
|
30
|
+
- lib/asset_uploader/uploader.rb
|
31
|
+
- lib/asset_uploader/version.rb
|
32
|
+
- test/test_asset_uploader.rb
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.19
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Support uploading of checksummed assets to S3
|
57
|
+
test_files:
|
58
|
+
- test/test_asset_uploader.rb
|