shrine-gdrive_storage 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6b50c4075747d9d678d02aa8229881451080bb97f4284cb5f0359d8cf1784f24
4
+ data.tar.gz: c5a5f67ea5caf04e2956d7a34a4c68beea5702eefa285162d7ac8d7b2f9bec57
5
+ SHA512:
6
+ metadata.gz: 92be70dfbdab6ed3d5fde41222318eef3283385d60744da6bad375e6acd66bf365290a25fe59404943b01782eb4f298da6a7dd49b324681616be78356fc4be0d
7
+ data.tar.gz: 3b62fb0f1d8491c3e7c588c6fff1d51b4edc7579d12e55729e22319983a8a9c92452d43568f04ea41b07d78bd5e855ea966bf9b9d340d0b2886e45e97bfc67f2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Scott Near
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,120 @@
1
+ # Shrine::Storage::GoogleDriveStorage
2
+
3
+ Provides [Google Drive Storage] for [Shrine].
4
+
5
+ shout-outz to:
6
+ https://github.com/verynear/shrine-google_drive_storage &&
7
+ https://github.com/renchap/shrine-google_cloud_storage
8
+
9
+ _note:_
10
+ This gem uses a Google API "Service Account" to upload files to Google Drive (or a particular Drive folder). You might want to dig into [shrine-google_drive_storage](https://github.com/verynear/shrine-google_drive_storage) if you want to upload to your user's various Google Drives via oAuth.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'shrine-gdrive_storage'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install shrine-gdrive_storage
27
+
28
+ ## Google Drive Setup
29
+
30
+ In order to use this storage you need a Google (or Google Apps) user which will own the files, and a Google API `client_secrets.json` file.
31
+
32
+ 1. Go to the [Google Developers console](https://console.developers.google.com/project) and create a new project, this option is on the top, next to the Google APIs logo.
33
+
34
+ 2. Go to "API Manager > Library" in the section "Google Apps APIs" and enable "Drive API".
35
+
36
+ 3. Go to "API Manager > Credentials" and create a new "Service Account Key".
37
+
38
+ 4. Download the client_secret_XXXXX.json file and rename it to client_secret.json.
39
+
40
+ 5. Create a google drive folder in which the files will be uploaded.
41
+
42
+ _note:_ find the `drive_public_folder_id` by browsing to the folder via web at https://drive.google.com and get the id from the url, like such: `https://drive.google.com/drive/u/0/folders/AAAARRRRGGGBBBFFFFadsasdX`
43
+
44
+ ## Environment variables
45
+
46
+ (`cache: Shrine::Storage::GoogleDriveStorage.new(drive_public_folder_id: 'AAAARRRRGGGBBBFFFFadsasdX')`)
47
+
48
+ use the [dotenv](https://rubygems.org/gems/dotenv) gem and add `require 'dotenv/load` to easily load values from your .env file
49
+
50
+ Create an `.env` file with these values:
51
+
52
+ ```sh
53
+ # .env
54
+ APPLICATION_NAME = "yourapp-12345"
55
+ GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/client_secret.json
56
+ GOOGLE_PUBLIC_FOLDER_ID=AAAARRRRGGGBBBFFFFadsasdX
57
+ ```
58
+
59
+ __-or-__ set the environment variables however you like.
60
+
61
+ The application name in the above cases is the project name you chose in the [Google Developers console](https://console.developers.google.com/project)
62
+
63
+
64
+ ## Usage
65
+
66
+ ```rb
67
+ require "dotenv/load" #optional, load environment variables from .env file
68
+ require "shrine/storage/google_drive_storage"
69
+
70
+ Shrine.storages = {
71
+ store: Shrine::Storage::GoogleDriveStorage.new,
72
+ }
73
+ ```
74
+
75
+ you can use this for cache storage, too.
76
+
77
+ ## Configuration
78
+
79
+ set the `drive_public_folder_id` here (instead of via environment variable) if you'd like
80
+
81
+ ```rb
82
+ Shrine::Storage::GoogleDriveStorage.new(
83
+ drive_public_folder_id: 'AAAARRRRGGGBBBFFFFadsasdX',
84
+ )
85
+ ```
86
+
87
+ ## Helpful info
88
+
89
+ If you are getting an "Access Not Configured" error while uploading files, this is due to this API not being enabled or, perhaps, your drive folder not having the correct permissions.
90
+
91
+ Try sharing your folder (`drive_public_folder_id`) with the google service account user (see https:// or client_secret.json) something like: yourapp@yourapp-12345.iam.gserviceaccount.com. You might want to make your folder public.
92
+
93
+ or maybe use "Enable G Suite Domain-wide Delegation" via https://console.developers.google.com/iam-admin/serviceaccounts/project?project=yourapp-12345
94
+
95
+
96
+ ## Miscellaneous
97
+
98
+ rtfm: http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/DriveV3
99
+
100
+ ## Testing
101
+
102
+ a quick way to make sure your client_secret.json is working:
103
+
104
+ $ ruby test/gdrive_quickstart.rb
105
+
106
+ and then run this against the nifty shrinerb linters:
107
+
108
+ $ rake test
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/edwardsharp/shrine-gdrive_storage.
113
+
114
+ ## License
115
+
116
+ [MIT License](http://opensource.org/licenses/MIT).
117
+
118
+
119
+ [Google Drive Storage]: https://drive.google.com/drive/
120
+ [Shrine]: https://github.com/janko-m/shrine
@@ -0,0 +1,86 @@
1
+ require 'shrine'
2
+ require 'google/apis/drive_v3'
3
+ require 'googleauth'
4
+ require 'googleauth/stores/file_token_store'
5
+ require 'down/chunked_io'
6
+ require 'fileutils'
7
+
8
+ class Shrine
9
+ module Storage
10
+ class GoogleDriveStorage
11
+
12
+ def initialize(drive_public_folder_id: nil, google_drive_options: {})
13
+ @drive_public_folder_id = drive_public_folder_id || ENV["GOOGLE_PUBLIC_FOLDER_ID"]
14
+ end
15
+
16
+ def upload(io, id, shrine_metadata: {}, **_options)
17
+ mime_type = io.metadata['mime_type'] rescue 'image/jpeg'
18
+
19
+ s = StringIO.new
20
+ s.write(io.read)
21
+ s.rewind
22
+ google_api_client.create_file(
23
+ { name: id,
24
+ mime_type: mime_type,
25
+ parents: [@drive_public_folder_id]
26
+ },
27
+ fields: 'id, name',
28
+ upload_source: s,
29
+ content_type: shrine_metadata['mime_type']
30
+ )
31
+
32
+ message = "Uploaded file #{id}"
33
+ end
34
+
35
+ def exists?(id)
36
+ get_drive_file_id(id)
37
+ end
38
+
39
+ def open(id)
40
+ sio = StringIO.new
41
+ google_api_client.get_file(
42
+ get_drive_file_id(id),
43
+ download_dest: sio
44
+ )
45
+ sio.rewind
46
+ sio
47
+ end
48
+
49
+ def url(id, **_options)
50
+ begin
51
+ metadata = google_api_client.get_file(
52
+ get_drive_file_id(id),
53
+ fields: 'webViewLink'
54
+ )
55
+ metadata.web_view_link
56
+ rescue Exception => e
57
+ # p "URL ERR! #{e}"
58
+ #TODO: something meaningful?
59
+ end
60
+ end
61
+
62
+ def delete(id)
63
+ google_api_client.delete_file(get_drive_file_id(id)) rescue nil
64
+ end
65
+
66
+ private
67
+
68
+ def get_drive_file_id(id)
69
+ google_api_client.list_files(q: "name contains '#{id}'").files[0].id rescue nil
70
+ end
71
+
72
+ def google_api_client
73
+ if !@google_api_client || @google_api_client.authorization.expired?
74
+ service = Google::Apis::DriveV3::DriveService.new
75
+ service.client_options.application_name = ENV['APPLICATION_NAME']
76
+ service.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
77
+ json_key_io: File.open(ENV['GOOGLE_APPLICATION_CREDENTIALS']),
78
+ scope: Google::Apis::DriveV3::AUTH_DRIVE)
79
+ @google_api_client = service
80
+ end
81
+ @google_api_client
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "shrine-gdrive_storage"
3
+ spec.version = "0.5.0"
4
+ spec.authors = ["edwardsharp"]
5
+ spec.email = ["edward@edwardsharp.net"]
6
+ spec.date = '2018-04-04'
7
+ spec.summary = "Provides Google Drive Storage for Shrine."
8
+ spec.description = "Provides Google Drive Storage for Shrine. Fork & improvemnet on Scott Near's version (https://github.com/verynear/shrine-google_drive_storage)."
9
+ spec.homepage = "https://github.com/edwardsharp/shrine-gdrive_storage"
10
+ spec.license = "MIT"
11
+
12
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
13
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
14
+ # if spec.respond_to?(:metadata)
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+ # else
17
+ # raise "RubyGems 2.0 or newer is required to protect against " \
18
+ # "public gem pushes."
19
+ # end
20
+
21
+
22
+ spec.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-gdrive_storage.gemspec"]
23
+
24
+ spec.require_paths = "lib"
25
+
26
+ spec.add_dependency 'shrine', '~> 2.1'
27
+ spec.add_dependency 'googleauth', '~> 0.5'
28
+ spec.add_dependency 'google-api-client', '~> 0.20.0'
29
+ spec.add_development_dependency 'httparty', '~> 0.16'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'minitest'
32
+ spec.add_development_dependency 'dotenv'
33
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-gdrive_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - edwardsharp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-04 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.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: googleauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-api-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.20.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.20.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
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: minitest
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: dotenv
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
+ description: Provides Google Drive Storage for Shrine. Fork & improvemnet on Scott
112
+ Near's version (https://github.com/verynear/shrine-google_drive_storage).
113
+ email:
114
+ - edward@edwardsharp.net
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - LICENSE.txt
120
+ - README.md
121
+ - lib/shrine/storage/google_drive_storage.rb
122
+ - shrine-gdrive_storage.gemspec
123
+ homepage: https://github.com/edwardsharp/shrine-gdrive_storage
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.7.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Provides Google Drive Storage for Shrine.
147
+ test_files: []