flatiron-s3-uploader 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.example +3 -0
- data/.rspec +3 -0
- data/.rubocop.yml +38 -0
- data/.vscode/extensions.json +3 -0
- data/CODE_OF_CONDUCT.md +126 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +8 -0
- data/bin/console +13 -0
- data/bin/flatiron-s3-uploader +10 -0
- data/bin/setup +6 -0
- data/config/pre_commit.yml +3 -0
- data/flatiron-s3-uploader.gemspec +45 -0
- data/lib/flatiron_s3_uploader/cli.rb +25 -0
- data/lib/flatiron_s3_uploader/exceptions.rb +5 -0
- data/lib/flatiron_s3_uploader/file_uploader.rb +24 -0
- data/lib/flatiron_s3_uploader/image_resizer.rb +36 -0
- data/lib/flatiron_s3_uploader/version.rb +5 -0
- data/lib/flatiron_s3_uploader.rb +44 -0
- metadata +222 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 354b91cbcee79c34ae6360920988ea091224c018e8ca75f980f92ffe0e7d8ba3
|
4
|
+
data.tar.gz: 850d32d36eed45696527be70be9ef42fce6855839f042687b4046ba5aae296a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: db66936a2362dd867d66e38e3a30c86f462cccbc2ddaef582fee663a447319442892fb267bf6886f0ac2aefcd7508b9011e2346068fbad1ad57a738040f3b9b8
|
7
|
+
data.tar.gz: bbbbfc21fd1725ebd6c7695604a12cf57c49ed70087eb128e90adbdf3cf718b3f33b62d075f4844d26f7bc111943f1a3ffa0021fe54ef85f5fd83fc193c5c0c3
|
data/.env.example
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
NewCops: enable
|
6
|
+
|
7
|
+
Layout/FirstHashElementIndentation:
|
8
|
+
EnforcedStyle: consistent
|
9
|
+
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/ClassLength:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/MethodLength:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
RSpec/ExampleLength:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
RSpec/MultipleExpectations:
|
26
|
+
Max: 2
|
27
|
+
|
28
|
+
RSpec/MultipleMemoizedHelpers:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/Documentation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/Lambda:
|
35
|
+
EnforcedStyle: literal
|
36
|
+
|
37
|
+
Style/SymbolArray:
|
38
|
+
EnforcedStyle: brackets
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
- Demonstrating empathy and kindness toward other people
|
21
|
+
- Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
- Giving and gracefully accepting constructive feedback
|
23
|
+
- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
24
|
+
- Focusing on what is best not just for us as individuals, but for the overall community
|
25
|
+
|
26
|
+
Examples of unacceptable behavior include:
|
27
|
+
|
28
|
+
- The use of sexualized language or imagery, and sexual attention or
|
29
|
+
advances of any kind
|
30
|
+
- Trolling, insulting or derogatory comments, and personal or political attacks
|
31
|
+
- Public or private harassment
|
32
|
+
- Publishing others' private information, such as a physical or email
|
33
|
+
address, without their explicit permission
|
34
|
+
- Other conduct which could reasonably be considered inappropriate in a
|
35
|
+
professional setting
|
36
|
+
|
37
|
+
## Enforcement Responsibilities
|
38
|
+
|
39
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
40
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
41
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
42
|
+
or harmful.
|
43
|
+
|
44
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
45
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
46
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
47
|
+
decisions when appropriate.
|
48
|
+
|
49
|
+
## Scope
|
50
|
+
|
51
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
52
|
+
an individual is officially representing the community in public spaces.
|
53
|
+
Examples of representing our community include using an official e-mail address,
|
54
|
+
posting via an official social media account, or acting as an appointed
|
55
|
+
representative at an online or offline event.
|
56
|
+
|
57
|
+
## Enforcement
|
58
|
+
|
59
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
60
|
+
reported to the community leaders responsible for enforcement at
|
61
|
+
ianholla@gmail.com. All complaints will be reviewed and investigated promptly
|
62
|
+
and fairly.
|
63
|
+
|
64
|
+
All community leaders are obligated to respect the privacy and security of the
|
65
|
+
reporter of any incident.
|
66
|
+
|
67
|
+
## Enforcement Guidelines
|
68
|
+
|
69
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
70
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
71
|
+
|
72
|
+
### 1. Correction
|
73
|
+
|
74
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
75
|
+
unprofessional or unwelcome in the community.
|
76
|
+
|
77
|
+
**Consequence**: A private, written warning from community leaders, providing
|
78
|
+
clarity around the nature of the violation and an explanation of why the
|
79
|
+
behavior was inappropriate. A public apology may be requested.
|
80
|
+
|
81
|
+
### 2. Warning
|
82
|
+
|
83
|
+
**Community Impact**: A violation through a single incident or series of
|
84
|
+
actions.
|
85
|
+
|
86
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
87
|
+
interaction with the people involved, including unsolicited interaction with
|
88
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
89
|
+
includes avoiding interactions in community spaces as well as external channels
|
90
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
91
|
+
ban.
|
92
|
+
|
93
|
+
### 3. Temporary Ban
|
94
|
+
|
95
|
+
**Community Impact**: A serious violation of community standards, including
|
96
|
+
sustained inappropriate behavior.
|
97
|
+
|
98
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
99
|
+
communication with the community for a specified period of time. No public or
|
100
|
+
private interaction with the people involved, including unsolicited interaction
|
101
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
102
|
+
Violating these terms may lead to a permanent ban.
|
103
|
+
|
104
|
+
### 4. Permanent Ban
|
105
|
+
|
106
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
107
|
+
standards, including sustained inappropriate behavior, harassment of an
|
108
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
109
|
+
|
110
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
111
|
+
community.
|
112
|
+
|
113
|
+
## Attribution
|
114
|
+
|
115
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
116
|
+
version 2.0, available at
|
117
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
118
|
+
|
119
|
+
Community Impact Guidelines were inspired by
|
120
|
+
[Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
121
|
+
|
122
|
+
[homepage]: https://www.contributor-covenant.org
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
126
|
+
https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 ihollander
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# FlatironS3Uploader
|
2
|
+
|
3
|
+
A tool for resizing and uploading images to AWS S3.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'flatiron-s3-uploader'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```console
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```console
|
22
|
+
$ gem install flatiron-s3-uploader
|
23
|
+
```
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
The S3 upload functionality requires that you have the following environment
|
28
|
+
variables set:
|
29
|
+
|
30
|
+
- `ENV['AWS_ACCESS_KEY_ID']`
|
31
|
+
- `ENV['AWS_SECRET_ACCESS_KEY']`
|
32
|
+
- `ENV['AWS_REGION']`
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
The core functionality of uploading (and optionally resizing) images can be
|
37
|
+
performed one of two ways. To use this Ruby gem in other Ruby projects, use the
|
38
|
+
`FlatironS3Uploader.upload` method directly:
|
39
|
+
|
40
|
+
```rb
|
41
|
+
# Upload a file to a specified S3 bucket
|
42
|
+
FlatironS3Uploader.upload('path/to/image.png', 'bucket-name')
|
43
|
+
# => "https://bucket-name.s3.amazonaws.com/image.png"
|
44
|
+
|
45
|
+
# Upload and resize an image
|
46
|
+
FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400, directory: 'folder/subfolder' })
|
47
|
+
# => "https://bucket-name.s3.amazonaws.com/folder/subfolder/image.png"
|
48
|
+
```
|
49
|
+
|
50
|
+
The `FlatironS3Uploader.upload(path, bucket, options = {})` method takes the
|
51
|
+
following parameters:
|
52
|
+
|
53
|
+
- `path`: The file location of the image to be uploaded.
|
54
|
+
- `bucket`: The name of the S3 bucket.
|
55
|
+
- `options`:
|
56
|
+
- `width`: The width of the resized image. If a +height+ is not specified, the
|
57
|
+
height will scale based on the specified width.
|
58
|
+
- `height`: The height of the resized image. If a +width+ is not specified, the
|
59
|
+
width will scale based on the specified height.
|
60
|
+
- `directory`: The directory of the S3 bucket to upload to (i.e.
|
61
|
+
'phase-3/lab-name')
|
62
|
+
|
63
|
+
You can also use the gem directly from the command line:
|
64
|
+
|
65
|
+
```console
|
66
|
+
$ flatiron-s3-uploader upload path/to/image.png --bucket bucket-name --width 400 --directory folder/subfolder
|
67
|
+
```
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
72
|
+
`bundle exec rspec` to run the tests. You can also run `bin/console` for an
|
73
|
+
interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
76
|
+
release a new version, update the version number in `version.rb`, and then run
|
77
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
78
|
+
git commits and the created tag, and push the `.gem` file to
|
79
|
+
[rubygems.org](https://rubygems.org).
|
80
|
+
|
81
|
+
This project uses [Rubocop](https://rubocop.org/) to enforce consistent styling
|
82
|
+
via a [pre-commit hook](https://github.com/jish/pre-commit), so any time you
|
83
|
+
make a commit, your code will be linted. For VSCode users, it's suggested that
|
84
|
+
you install the
|
85
|
+
[Ruby VSCode extension](https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby)
|
86
|
+
and enable linting in VSCode via the `settings.json` file so that you can see
|
87
|
+
linting errors before committing your code:
|
88
|
+
|
89
|
+
```json
|
90
|
+
{
|
91
|
+
"ruby.lint": {
|
92
|
+
"rubocop": {
|
93
|
+
"useBundler": true
|
94
|
+
}
|
95
|
+
},
|
96
|
+
"ruby.format": "rubocop"
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
You can also run Rubocop manually to get a list of any issues:
|
101
|
+
|
102
|
+
```console
|
103
|
+
$ bundle exec rubocop
|
104
|
+
```
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at
|
109
|
+
https://github.com/ihollander/flatiron-s3-uploader. This project is intended to
|
110
|
+
be a safe, welcoming space for collaboration, and contributors are expected to
|
111
|
+
adhere to the
|
112
|
+
[code of conduct](https://github.com/ihollander/flatiron-s3-uploader/blob/main/CODE_OF_CONDUCT.md).
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
The gem is available as open source under the terms of the
|
117
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
118
|
+
|
119
|
+
## Code of Conduct
|
120
|
+
|
121
|
+
Everyone interacting in the FlatironS3Uploader project's codebases, issue
|
122
|
+
trackers, chat rooms and mailing lists is expected to follow the
|
123
|
+
[code of conduct](https://github.com/ihollander/flatiron-s3-uploader/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
|
6
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
7
|
+
|
8
|
+
require 'bundler/setup'
|
9
|
+
require 'pry'
|
10
|
+
|
11
|
+
require 'flatiron_s3_uploader'
|
12
|
+
|
13
|
+
Pry.start
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
|
6
|
+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
7
|
+
|
8
|
+
require 'flatiron_s3_uploader'
|
9
|
+
|
10
|
+
FlatironS3Uploader::CLI.start
|
data/bin/setup
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/flatiron_s3_uploader/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'flatiron-s3-uploader'
|
7
|
+
spec.version = FlatironS3Uploader::VERSION
|
8
|
+
spec.authors = ['ihollander']
|
9
|
+
spec.email = ['ianholla@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'A tool for resizing and uploading images to AWS S3'
|
12
|
+
spec.homepage = 'https://github.com/learn-co-curriculum/flatiron-s3-uploader'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 2.5.0'
|
15
|
+
|
16
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
17
|
+
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
19
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
20
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = 'bin'
|
30
|
+
spec.executables = 'flatiron-s3-uploader'
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
# Dependencies
|
34
|
+
spec.add_dependency 'aws-sdk-s3', '~> 1'
|
35
|
+
spec.add_dependency 'thor', '~> 1.1'
|
36
|
+
spec.add_development_dependency 'dotenv'
|
37
|
+
spec.add_development_dependency 'fastimage'
|
38
|
+
spec.add_development_dependency 'mini_magick'
|
39
|
+
spec.add_development_dependency 'pre-commit'
|
40
|
+
spec.add_development_dependency 'pry'
|
41
|
+
spec.add_development_dependency 'rake'
|
42
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
43
|
+
spec.add_development_dependency 'rubocop'
|
44
|
+
spec.add_development_dependency 'rubocop-rspec'
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
|
5
|
+
module FlatironS3Uploader
|
6
|
+
class CLI < Thor
|
7
|
+
option :bucket, required: true, desc: 'S3 bucket'
|
8
|
+
option :directory, desc: '(optional) Directory within the S3 bucket'
|
9
|
+
option :width, type: :numeric, desc: '(optional) Resize image to the specified width'
|
10
|
+
option :height, type: :numeric, desc: '(optional) Resize image to the specified height'
|
11
|
+
desc 'upload', 'Upload an image to S3'
|
12
|
+
long_desc <<-LONGDESC
|
13
|
+
Uploads an image to a S3 bucket; sets public read permission; and optionally resizes it.
|
14
|
+
|
15
|
+
Example usage:
|
16
|
+
|
17
|
+
$ flatiron-s3-uploader upload 'path/to/file.png' --bucket 'curriculum-content' --directory 'phase-0/lab' --width 600
|
18
|
+
# => https://curriculum-content.s3.amazonaws.com/phase-0/lab/file.png
|
19
|
+
LONGDESC
|
20
|
+
def upload(filepath)
|
21
|
+
bucket = options['bucket']
|
22
|
+
puts FlatironS3Uploader.upload(filepath, bucket, options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FlatironS3Uploader
|
4
|
+
class FileUploader
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def initialize(client)
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
def upload(bucket, image, mime_type, directory = nil)
|
12
|
+
key = create_key(image, directory)
|
13
|
+
client.put_object(bucket: bucket, key: key, acl: 'public-read', body: image, content_type: mime_type)
|
14
|
+
"https://#{bucket}.s3.amazonaws.com/#{key}"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def create_key(image, directory)
|
20
|
+
filename = File.basename(image).sub('-resized', '')
|
21
|
+
directory ? "#{directory}/#{filename}" : filename
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mini_magick'
|
4
|
+
|
5
|
+
module FlatironS3Uploader
|
6
|
+
class ImageResizer
|
7
|
+
attr_reader :path, :image
|
8
|
+
|
9
|
+
def initialize(path)
|
10
|
+
raise FlatironS3Uploader::FileNotFoundError unless File.exist?(path)
|
11
|
+
|
12
|
+
@path = path
|
13
|
+
@image = MiniMagick::Image.open(path)
|
14
|
+
end
|
15
|
+
|
16
|
+
def resize(width: nil, height: nil)
|
17
|
+
image.resize("#{width}x#{height}")
|
18
|
+
image.write(output_path)
|
19
|
+
output_path
|
20
|
+
end
|
21
|
+
|
22
|
+
def mime_type
|
23
|
+
image.mime_type
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def output_path
|
29
|
+
return @output_path if @output_path
|
30
|
+
|
31
|
+
file_name = File.basename(path).sub('.', '-resized.')
|
32
|
+
directory = File.dirname(path)
|
33
|
+
@output_path = "#{directory}/#{file_name}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws-sdk-s3'
|
4
|
+
require 'flatiron_s3_uploader/exceptions'
|
5
|
+
|
6
|
+
require 'dotenv/load' if ENV['GEM_ENV'] == 'development'
|
7
|
+
|
8
|
+
module FlatironS3Uploader
|
9
|
+
autoload :CLI, 'flatiron_s3_uploader/cli'
|
10
|
+
autoload :FileUploader, 'flatiron_s3_uploader/file_uploader'
|
11
|
+
autoload :ImageResizer, 'flatiron_s3_uploader/image_resizer'
|
12
|
+
autoload :VERSION, 'flatiron_s3_uploader/version'
|
13
|
+
|
14
|
+
# Uploads an image file from the specified filepath to the specified S3 bucket.
|
15
|
+
# Returns a URL for the uploaded image.
|
16
|
+
#
|
17
|
+
# Options are:
|
18
|
+
# [:width]
|
19
|
+
# The width of the resized image. If a +height+ is not specified, the height
|
20
|
+
# will scale based on the specified width.
|
21
|
+
# [:height]
|
22
|
+
# The height of the resized image. If a +width+ is not specified, the width
|
23
|
+
# will scale based on the specified height.
|
24
|
+
# [:directory]
|
25
|
+
# The directory of the S3 bucket to upload to (i.e. 'phase-3/lab-name')
|
26
|
+
#
|
27
|
+
# Examples:
|
28
|
+
# FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400 })
|
29
|
+
# # => "https://bucket-name.s3.amazonaws.com/image.png"
|
30
|
+
#
|
31
|
+
# FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400, directory: 'folder/subfolder' })
|
32
|
+
# # => "https://bucket-name.s3.amazonaws.com/folder/subfolder/image.png"
|
33
|
+
#
|
34
|
+
def self.upload(filepath, bucket, options = {})
|
35
|
+
resizer = ImageResizer.new(filepath)
|
36
|
+
filepath = resizer.resize(width: options[:width], height: options[:height]) if options[:width] || options[:height]
|
37
|
+
mime_type = resizer.mime_type
|
38
|
+
|
39
|
+
image = File.open(filepath)
|
40
|
+
|
41
|
+
client = Aws::S3::Client.new
|
42
|
+
FileUploader.new(client).upload(bucket, image, mime_type, options[:directory])
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flatiron-s3-uploader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ihollander
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-s3
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fastimage
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mini_magick
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pre-commit
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '3.10'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.10'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rubocop-rspec
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description:
|
168
|
+
email:
|
169
|
+
- ianholla@gmail.com
|
170
|
+
executables:
|
171
|
+
- flatiron-s3-uploader
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- ".env.example"
|
176
|
+
- ".rspec"
|
177
|
+
- ".rubocop.yml"
|
178
|
+
- ".vscode/extensions.json"
|
179
|
+
- CODE_OF_CONDUCT.md
|
180
|
+
- Gemfile
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.md
|
183
|
+
- Rakefile
|
184
|
+
- bin/console
|
185
|
+
- bin/flatiron-s3-uploader
|
186
|
+
- bin/setup
|
187
|
+
- config/pre_commit.yml
|
188
|
+
- flatiron-s3-uploader.gemspec
|
189
|
+
- lib/flatiron_s3_uploader.rb
|
190
|
+
- lib/flatiron_s3_uploader/cli.rb
|
191
|
+
- lib/flatiron_s3_uploader/exceptions.rb
|
192
|
+
- lib/flatiron_s3_uploader/file_uploader.rb
|
193
|
+
- lib/flatiron_s3_uploader/image_resizer.rb
|
194
|
+
- lib/flatiron_s3_uploader/version.rb
|
195
|
+
homepage: https://github.com/learn-co-curriculum/flatiron-s3-uploader
|
196
|
+
licenses:
|
197
|
+
- MIT
|
198
|
+
metadata:
|
199
|
+
allowed_push_host: https://rubygems.org
|
200
|
+
homepage_uri: https://github.com/learn-co-curriculum/flatiron-s3-uploader
|
201
|
+
source_code_uri: https://github.com/learn-co-curriculum/flatiron-s3-uploader
|
202
|
+
changelog_uri: https://github.com/learn-co-curriculum/flatiron-s3-uploader
|
203
|
+
post_install_message:
|
204
|
+
rdoc_options: []
|
205
|
+
require_paths:
|
206
|
+
- lib
|
207
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
208
|
+
requirements:
|
209
|
+
- - ">="
|
210
|
+
- !ruby/object:Gem::Version
|
211
|
+
version: 2.5.0
|
212
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
213
|
+
requirements:
|
214
|
+
- - ">="
|
215
|
+
- !ruby/object:Gem::Version
|
216
|
+
version: '0'
|
217
|
+
requirements: []
|
218
|
+
rubygems_version: 3.1.6
|
219
|
+
signing_key:
|
220
|
+
specification_version: 4
|
221
|
+
summary: A tool for resizing and uploading images to AWS S3
|
222
|
+
test_files: []
|