gcloud_storage 0.0.3
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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +150 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config.yml.example +4 -0
- data/gcloud_storage.gemspec +31 -0
- data/lib/gcloud_storage.rb +28 -0
- data/lib/gcloud_storage/base.rb +106 -0
- data/lib/gcloud_storage/configuration.rb +14 -0
- data/lib/gcloud_storage/error.rb +10 -0
- data/lib/gcloud_storage/local_store.rb +61 -0
- data/lib/gcloud_storage/uploader.rb +109 -0
- data/lib/gcloud_storage/version.rb +3 -0
- data/secrets.tar.enc +0 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0017df3846f33dafd70d35c2cbfdf29555f3f50a
|
4
|
+
data.tar.gz: 7e7bfc6dfa78b1a72865f60dd46a48bded42199c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4fda258be39c1b6453178f049e41b94e154652eb703467777fff90678be737b13b771b86ce911b2aae30e419ac7894e68b3d8cbd37d1a7ead1afeddfbc72be5c
|
7
|
+
data.tar.gz: e8bbb30a802c6e3a4e46709b301d7b80d694d0dc4f7422a1399aacf5948290fe1dfd5ad1e1e13fdeefa49fbd29daceb94c8902ca0383baa79894ab2e4946853d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2.3@gcloud_storage
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at itsprdp@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Pradeep G
|
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,150 @@
|
|
1
|
+
# GcloudStorage [](https://badge.fury.io/rb/gcloud_storage) [](https://travis-ci.org/itsprdp/gcloud_storage) [](https://coveralls.io/github/itsprdp/gcloud_storage?branch=compute_instances)
|
2
|
+
|
3
|
+
Simple Google Cloud Storage file upload gem for Ruby. This is an alternative gem
|
4
|
+
for carrierwave with fog. As, carrierwave with fog only uses API Key
|
5
|
+
authentication to talk to Google Cloud Storage API. This gem supports the
|
6
|
+
service account authentication and as well as compute instance service account
|
7
|
+
where you don't have to initialize the gem with the credentials.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'gcloud_storage'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install gcloud_storage
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Model:
|
28
|
+
```ruby
|
29
|
+
class TempFile < ActiveRecord::Base
|
30
|
+
include GcloudStorage::Uploader
|
31
|
+
|
32
|
+
# attribute :file
|
33
|
+
mount_gcloud_uploader :file #[, presence: true] #=> Run's presence validation
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Migration:
|
38
|
+
```ruby
|
39
|
+
class CreateTempFiles < ActiveRecord::Migration
|
40
|
+
def change
|
41
|
+
create_table :temp_files do |t|
|
42
|
+
t.string :file
|
43
|
+
|
44
|
+
t.timestamps null: false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Attachment Methods:
|
51
|
+
```ruby
|
52
|
+
temp_file = TempFile.new(file_uploader_object: path_to_file) # => TempFile object
|
53
|
+
tempfile.save
|
54
|
+
temp_file.file_url # => HTTPS URL which expires in 300 seconds bye default
|
55
|
+
temp_file.file_expirable_url(60) # => HTTPS URL which expires in 60 seconds
|
56
|
+
temp_file.file_path # => "/uploads/#{model_name}s/:id/#{attribute_name}s/filename.extension"
|
57
|
+
```
|
58
|
+
|
59
|
+
Create an initializer file `config/initializers/gcloud_storage.rb` and add these
|
60
|
+
lines:
|
61
|
+
|
62
|
+
For remote storage:
|
63
|
+
```ruby
|
64
|
+
# Uncomment this to support large file uploads
|
65
|
+
# Faraday.default_adapter = :httpclient
|
66
|
+
|
67
|
+
GcloudStorage.configure do |config|
|
68
|
+
config.credentials = {
|
69
|
+
bucket_name: 'bucket_name', # Storage bucket name
|
70
|
+
project_id: 'project_id', # Google Cloud Project ID
|
71
|
+
key_file: 'key_file_path' # Compute Service account json file
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
# Add this to validate and cache connection object
|
76
|
+
# while loading the Rails Application
|
77
|
+
# GcloudStorage.initialize_service!
|
78
|
+
```
|
79
|
+
|
80
|
+
For local storage:
|
81
|
+
```ruby
|
82
|
+
GcloudStorage.configure do |config|
|
83
|
+
config.credentials = {
|
84
|
+
storage: :local_store
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
# Add this to validate and cache connection object
|
89
|
+
# while loading the Rails Application
|
90
|
+
# GcloudStorage.initialize_service!
|
91
|
+
```
|
92
|
+
|
93
|
+
File upload example:
|
94
|
+
You can pass path to the file to be uploaded as a `String` or as `Pathname` or
|
95
|
+
as `Rack::Multipart::UploadedFile` object using HTML Multipart form.
|
96
|
+
The attribute to initialize will be `#{column}_uploader_object`. Here the column
|
97
|
+
name is file in the above example.
|
98
|
+
|
99
|
+
```
|
100
|
+
$ rails console
|
101
|
+
Loading development environment (Rails 4.2.0)
|
102
|
+
:001 > `echo "This is a test file" > temp.txt`
|
103
|
+
=> ""
|
104
|
+
:002 > file = Rack::Multipart::UploadedFile.new("temp.txt")
|
105
|
+
=> #<Rack::Multipart::UploadedFile:0x007f9e29ae37c8 @content_type="text/plain", @original_filename="temp.txt", @tempfile=#<Tempfile:/var/folders/temp.txt>>
|
106
|
+
:003 > temp_file = TempFile.new(file_uploader_object: file) # file_attribute => #{column}_uploader_object
|
107
|
+
=> #<TempFile id: nil, file: "temp.txt", created_at: nil, updated_at: nil>
|
108
|
+
:004 > temp_file.valid?
|
109
|
+
=> true
|
110
|
+
:005 > temp_file.save
|
111
|
+
=> true
|
112
|
+
:006 > temp_file.file_url
|
113
|
+
=> "https://storage.googleapis.com/<bucket-name>/uploads/temp_files/1/files/temp.txt?GoogleAccessId=compute%40developer.gserviceaccount.com&Expires=1459851006&Signature=XXXX"
|
114
|
+
:007 > `echo "Yet Another test file" > tmp/yet_another_test.txt`
|
115
|
+
=> ""
|
116
|
+
:008 > another_file = TempFile.new(file_uploader_object: "tmp/yet_another_test.txt")
|
117
|
+
=> #<TempFile id: nil, file: "yet_another_test.txt", created_at: nil, updated_at: nil>
|
118
|
+
:009 > another_file.save
|
119
|
+
=> true
|
120
|
+
:010 > another_file.file_url
|
121
|
+
=> "https://storage.googleapis.com/<bucket-name>/uploads/temp_files/2/files/yet_another_test.txt?GoogleAccessId=compute%40developer.gserviceaccount.com&Expires=1459851800&Signature=XXXX"
|
122
|
+
:011 > open(another_file.file_url).read
|
123
|
+
=> "Yet Another test file\n"
|
124
|
+
:012 > another_file.file_path
|
125
|
+
=> "uploads/temp_files/2/files/yet_another_test.txt"
|
126
|
+
```
|
127
|
+
|
128
|
+
## TODO
|
129
|
+
1. More specs for GcloudStorage::Uploader
|
130
|
+
2. Support overriding the mountable methods in a better way. For now the user
|
131
|
+
can override the methods in the model.
|
132
|
+
|
133
|
+
## Development
|
134
|
+
|
135
|
+
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.
|
136
|
+
|
137
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
138
|
+
run specs locally you need to setup a service account on google cloud project
|
139
|
+
and add the service json file to the root directory as
|
140
|
+
`test-bucket-service.json` and create `config.yml` from `config.yml.example`
|
141
|
+
and fill in the appropriate values.
|
142
|
+
|
143
|
+
## Contributing
|
144
|
+
|
145
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/itsprdp/gcloud_storage. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
146
|
+
|
147
|
+
|
148
|
+
## License
|
149
|
+
|
150
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gcloud_storage"
|
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
|
data/bin/setup
ADDED
data/config.yml.example
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gcloud_storage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gcloud_storage"
|
8
|
+
spec.version = GcloudStorage::VERSION
|
9
|
+
spec.authors = ["Pradeep G"]
|
10
|
+
spec.email = ["itsprdp@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple Google Cloud Storage file upload gem for Ruby.}
|
13
|
+
spec.description = %q{Simple Google Cloud Storage file upload gem for Ruby. You can use this as an alternative to carrierwave with fog.}
|
14
|
+
spec.homepage = "https://github.com/itsprdp/gcloud_storage"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "gcloud", "0.7.2"
|
23
|
+
spec.add_dependency "faraday", "0.9.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "activerecord"
|
29
|
+
spec.add_development_dependency "sqlite3"
|
30
|
+
spec.add_development_dependency "coveralls"
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "gcloud_storage/version"
|
2
|
+
require "gcloud_storage/configuration"
|
3
|
+
require "gcloud_storage/base"
|
4
|
+
require "gcloud_storage/uploader"
|
5
|
+
require "gcloud_storage/error"
|
6
|
+
|
7
|
+
module GcloudStorage
|
8
|
+
class << self
|
9
|
+
attr_accessor :configuration, :connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
self.configuration ||= Configuration.new
|
14
|
+
yield(configuration)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.service
|
18
|
+
self.connection || self.initialize_service!
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.initialize_service!
|
22
|
+
if self.configuration
|
23
|
+
self.connection ||= Base.new(self.configuration.credentials)
|
24
|
+
else
|
25
|
+
Error.missing_credentials
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require "gcloud"
|
2
|
+
require "gcloud_storage/configuration"
|
3
|
+
require "gcloud_storage/error"
|
4
|
+
require "gcloud_storage/local_store"
|
5
|
+
|
6
|
+
module GcloudStorage
|
7
|
+
class Base
|
8
|
+
attr_reader :connection
|
9
|
+
|
10
|
+
def initialize(options)
|
11
|
+
@connection = { credentials: options }
|
12
|
+
|
13
|
+
Error.missing_credentials unless options.is_a?(Hash)
|
14
|
+
|
15
|
+
unless options[:storage] == :local_store
|
16
|
+
missing_options = []
|
17
|
+
|
18
|
+
[:bucket_name, :project_id, :key_file].each do |option|
|
19
|
+
option_value = options[option]
|
20
|
+
|
21
|
+
unless options[:compute_instance] && option == :key_file
|
22
|
+
missing_options << option if (option_value.nil? || !(option_value))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
unless missing_options.empty?
|
27
|
+
raise Error::Argument.new(":#{missing_options.join(', ')} are missing")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Cache service, storage and bucket to @connection
|
32
|
+
self.bucket
|
33
|
+
end
|
34
|
+
|
35
|
+
def service
|
36
|
+
storage_type = @connection[:credentials][:storage].present? ?
|
37
|
+
@connection[:credentials][:storage] : (:gcloud)
|
38
|
+
|
39
|
+
begin
|
40
|
+
@connection[:service] ||=
|
41
|
+
if storage_type == :local_store
|
42
|
+
LocalStore.new
|
43
|
+
else
|
44
|
+
Gcloud.new(@connection[:credentials][:project_id], @connection[:credentials][:key_file])
|
45
|
+
end
|
46
|
+
rescue => e
|
47
|
+
raise e
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def storage
|
52
|
+
@connection[:storage] ||= (
|
53
|
+
begin
|
54
|
+
tries ||= 3
|
55
|
+
self.service.storage
|
56
|
+
rescue => e
|
57
|
+
retry unless (tries -= 1).zero?
|
58
|
+
raise e
|
59
|
+
end
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
def bucket
|
64
|
+
@connection[:bucket] ||= self.storage.bucket(
|
65
|
+
@connection[:credentials][:bucket_name]
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
def expirable_url(file_path, num_secs=300)
|
70
|
+
file = self.bucket.file(file_path)
|
71
|
+
file.signed_url(method: 'GET', expires: num_secs)
|
72
|
+
end
|
73
|
+
|
74
|
+
def upload_file(file, dest_file_path)
|
75
|
+
remote_file = nil
|
76
|
+
file_path = (file.respond_to?(:tempfile))? file.tempfile : file
|
77
|
+
|
78
|
+
begin
|
79
|
+
retries ||= 2
|
80
|
+
remote_file = self.bucket.upload_file file_path, dest_file_path
|
81
|
+
rescue => e
|
82
|
+
unless (retries -= 1).zero?
|
83
|
+
retry
|
84
|
+
else
|
85
|
+
raise e
|
86
|
+
end
|
87
|
+
else
|
88
|
+
unless remote_file.md5 == Digest::MD5.base64digest(File.read(file_path))
|
89
|
+
raise Exception.new('Uploaded file is corrupted.')
|
90
|
+
end
|
91
|
+
|
92
|
+
remote_file
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def delete_file(file_path)
|
97
|
+
begin
|
98
|
+
file = self.bucket.file(file_path)
|
99
|
+
file.delete
|
100
|
+
rescue => e
|
101
|
+
raise e
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end # Base
|
105
|
+
|
106
|
+
end # GcloudStorage
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module GcloudStorage
|
2
|
+
module Error
|
3
|
+
class Configuration < StandardError; end
|
4
|
+
class Argument < ArgumentError; end
|
5
|
+
|
6
|
+
def self.missing_credentials
|
7
|
+
raise Configuration.new("Missing credentials. Please configure using GcloudStorage.configure(&block).")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
|
3
|
+
module GcloudStorage
|
4
|
+
class LocalStore
|
5
|
+
def initialize
|
6
|
+
@path = "#{FileUtils.pwd}/public/"
|
7
|
+
|
8
|
+
FileUtils.mkdir_p(@path + "/uploads") unless Dir.exist?(@path)
|
9
|
+
|
10
|
+
@file = Struct.new(:path, :md5) do
|
11
|
+
def signed_url(_arg1=nil,_arg2=nil)
|
12
|
+
path
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete
|
16
|
+
abs_path = "#{@path}#{path}"
|
17
|
+
ret = FileUtils.rm(abs_path) if File.exist?(abs_path)
|
18
|
+
abs_path == ret[0] if ret
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def service
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def storage
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def bucket(_bucket_name=nil)
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def file(file_path)
|
36
|
+
abs_file_path = "#{@path}#{file_path}"
|
37
|
+
@file.new(abs_file_path)
|
38
|
+
end
|
39
|
+
|
40
|
+
def upload_file file_path, dest_path
|
41
|
+
dest_file_path = "#{@path}#{dest_path}"
|
42
|
+
copy_file(file_path, dest_file_path)
|
43
|
+
@file.new(dest_file_path, file_md5(dest_file_path))
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def file_md5(file_path)
|
48
|
+
Digest::MD5.base64digest(File.read(file_path))
|
49
|
+
end
|
50
|
+
|
51
|
+
def copy_file(src, dest)
|
52
|
+
dest_dir = dest.split("/")
|
53
|
+
_file_name = dest_dir.pop
|
54
|
+
dest_dir = dest_dir.join("/")
|
55
|
+
|
56
|
+
FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
|
57
|
+
FileUtils.cp(src, dest)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module GcloudStorage
|
2
|
+
module Uploader
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def mount_gcloud_uploader(column, options = {})
|
9
|
+
private_methods = []
|
10
|
+
|
11
|
+
attr_accessor :"#{column}_uploader_object"
|
12
|
+
|
13
|
+
after_save :"upload_#{column}_file_to_gc", if: lambda { send(:"#{column}_changed?") }
|
14
|
+
before_destroy :"delete_#{column}_file_from_gc"
|
15
|
+
|
16
|
+
after_initialize :"init_file_name_for_#{column}", if: lambda { send(:"#{column}_uploader_object").present? }
|
17
|
+
|
18
|
+
if options[:presence] == true
|
19
|
+
validate :"#{column}_presence"
|
20
|
+
|
21
|
+
define_method(:"#{column}_presence") do
|
22
|
+
errors.add(column,"cannot be empty. Please set a file path for :#{column}_uploader_object attribute.") unless send(column.to_sym).present?
|
23
|
+
end
|
24
|
+
|
25
|
+
private_methods << :"#{column}_presence"
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method(:"#{column}_path") do
|
29
|
+
"uploads/#{self.class.to_s.underscore}s/#{self.id || "non_persisted"}/#{column}s/#{send(column.to_sym)}"
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method(:"#{column}_url") do
|
33
|
+
expirable_gc_url(send(:"#{column}_path"))
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method(:"#{column}_expirable_url") do |num_secs|
|
37
|
+
expirable_gc_url(send(:"#{column}_path"), num_secs)
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method(:"upload_#{column}_file_to_gc") do
|
41
|
+
upload_file_to_gc(send(:"#{column}_uploader_object"), send(:"#{column}_path")) if send(:"#{column}_uploader_object").present?
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method(:"delete_#{column}_file_from_gc") do
|
45
|
+
delete_file_from_gc(send(:"#{column}_path")) if send(column.to_sym).present?
|
46
|
+
end
|
47
|
+
|
48
|
+
define_method(:"init_file_name_for_#{column}") do
|
49
|
+
send(:"#{column}=", sanitize_filename(return_filename(send(:"#{column}_uploader_object"))))
|
50
|
+
end
|
51
|
+
|
52
|
+
private_methods.push(
|
53
|
+
:"upload_#{column}_file_to_gc",
|
54
|
+
:"delete_#{column}_file_from_gc",
|
55
|
+
:"init_file_name_for_#{column}"
|
56
|
+
)
|
57
|
+
|
58
|
+
unless respond_to?(:sanitize_filename)
|
59
|
+
define_method(:sanitize_filename) do |file_name|
|
60
|
+
file_name.gsub(/[^0-9A-z.\-]/, '_') if file_name
|
61
|
+
end
|
62
|
+
|
63
|
+
private_methods << :sanitize_filename
|
64
|
+
end
|
65
|
+
|
66
|
+
unless respond_to?(:return_filename)
|
67
|
+
define_method(:return_filename) do |file|
|
68
|
+
if file.is_a?(String)
|
69
|
+
file.split("/").last
|
70
|
+
elsif file.is_a?(Pathname)
|
71
|
+
file.to_s
|
72
|
+
elsif file.respond_to?(:original_filename)
|
73
|
+
file.original_filename
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private_methods << :return_filename
|
78
|
+
end
|
79
|
+
|
80
|
+
unless respond_to?(:upload_file_to_gc)
|
81
|
+
define_method(:upload_file_to_gc) do |file_path, dest_path|
|
82
|
+
GcloudStorage.service.upload_file(file_path, dest_path)
|
83
|
+
end
|
84
|
+
|
85
|
+
private_methods << :upload_file_to_gc
|
86
|
+
end
|
87
|
+
|
88
|
+
unless respond_to?(:delete_file_from_gc)
|
89
|
+
define_method(:delete_file_from_gc) do |file_path|
|
90
|
+
GcloudStorage.service.delete_file(file_path)
|
91
|
+
end
|
92
|
+
|
93
|
+
private_methods << :delete_file_from_gc
|
94
|
+
end
|
95
|
+
|
96
|
+
unless respond_to?(:expirable_gc_url)
|
97
|
+
define_method(:expirable_gc_url) do |file_path, num_secs = 300|
|
98
|
+
GcloudStorage.service.expirable_url(file_path, num_secs) if persisted?
|
99
|
+
end
|
100
|
+
|
101
|
+
private_methods << :expirable_gc_url
|
102
|
+
end
|
103
|
+
|
104
|
+
private_methods.each {|method| private method}
|
105
|
+
end # mount_custom_uploader
|
106
|
+
end # ClassMethods
|
107
|
+
|
108
|
+
end # Uploader
|
109
|
+
end # GcloudStorage
|
data/secrets.tar.enc
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gcloud_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pradeep G
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gcloud
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.7.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
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: activerecord
|
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: sqlite3
|
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: coveralls
|
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
|
+
description: Simple Google Cloud Storage file upload gem for Ruby. You can use this
|
126
|
+
as an alternative to carrierwave with fog.
|
127
|
+
email:
|
128
|
+
- itsprdp@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".ruby-version"
|
136
|
+
- ".travis.yml"
|
137
|
+
- CODE_OF_CONDUCT.md
|
138
|
+
- Gemfile
|
139
|
+
- LICENSE.txt
|
140
|
+
- README.md
|
141
|
+
- Rakefile
|
142
|
+
- bin/console
|
143
|
+
- bin/setup
|
144
|
+
- config.yml.example
|
145
|
+
- gcloud_storage.gemspec
|
146
|
+
- lib/gcloud_storage.rb
|
147
|
+
- lib/gcloud_storage/base.rb
|
148
|
+
- lib/gcloud_storage/configuration.rb
|
149
|
+
- lib/gcloud_storage/error.rb
|
150
|
+
- lib/gcloud_storage/local_store.rb
|
151
|
+
- lib/gcloud_storage/uploader.rb
|
152
|
+
- lib/gcloud_storage/version.rb
|
153
|
+
- secrets.tar.enc
|
154
|
+
homepage: https://github.com/itsprdp/gcloud_storage
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata: {}
|
158
|
+
post_install_message:
|
159
|
+
rdoc_options: []
|
160
|
+
require_paths:
|
161
|
+
- lib
|
162
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
requirements: []
|
173
|
+
rubyforge_project:
|
174
|
+
rubygems_version: 2.4.8
|
175
|
+
signing_key:
|
176
|
+
specification_version: 4
|
177
|
+
summary: Simple Google Cloud Storage file upload gem for Ruby.
|
178
|
+
test_files: []
|