carrierwave-yandex-disk 0.1.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: ecbdf232daf7d6395c811bfb0469a47a20ca8a96784d519d76b6fa8ac478534c
4
+ data.tar.gz: 31532928a58bfd2bf83dbd0af33e18f3f240710a4c0176865ab418a52cec5cb1
5
+ SHA512:
6
+ metadata.gz: 90abed12b1bb0fcaaf3d6ba9e29d59f441df890d4edb74436730249cfe7e269b4d051ea200e486098d825f6069225a9bdb73361b1cce3b293067e63cec789b1d
7
+ data.tar.gz: 2dc35139d5b58f2f3fd40f0f1d0b5f63296b8181b4ca962974ce9f3cc89851bb981111458bb4594aead5c65441196193926770e543dded709e94b338a4604f5b
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+
4
+ rvm:
5
+ - 2.2
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ - rbx-3.91
10
+
11
+ before_install:
12
+ - gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in carrierwave-yandex-disk.gemspec
6
+ gemspec
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ carrierwave-yandex-disk (0.1.0)
5
+ carrierwave (>= 1.2)
6
+ yandex-disk (>= 0.0.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.5)
12
+ activesupport (= 5.1.5)
13
+ activesupport (5.1.5)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (~> 0.7)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ carrierwave (1.2.2)
19
+ activemodel (>= 4.0.0)
20
+ activesupport (>= 4.0.0)
21
+ mime-types (>= 1.16)
22
+ concurrent-ruby (1.0.5)
23
+ diff-lcs (1.3)
24
+ excon (0.60.0)
25
+ faraday (0.9.2)
26
+ multipart-post (>= 1.2, < 3)
27
+ faraday_middleware (0.9.2)
28
+ faraday (>= 0.7.4, < 0.10)
29
+ i18n (0.9.5)
30
+ concurrent-ruby (~> 1.0)
31
+ mime-types (3.1)
32
+ mime-types-data (~> 3.2015)
33
+ mime-types-data (3.2016.0521)
34
+ mini_portile2 (2.3.0)
35
+ minitest (5.11.3)
36
+ multipart-post (2.0.0)
37
+ nokogiri (1.8.2)
38
+ mini_portile2 (~> 2.3.0)
39
+ rake (10.5.0)
40
+ rspec (3.7.0)
41
+ rspec-core (~> 3.7.0)
42
+ rspec-expectations (~> 3.7.0)
43
+ rspec-mocks (~> 3.7.0)
44
+ rspec-core (3.7.1)
45
+ rspec-support (~> 3.7.0)
46
+ rspec-expectations (3.7.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.7.0)
49
+ rspec-mocks (3.7.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.7.0)
52
+ rspec-support (3.7.1)
53
+ thread_safe (0.3.6)
54
+ tzinfo (1.2.5)
55
+ thread_safe (~> 0.1)
56
+ yandex-disk (0.0.8)
57
+ excon (>= 0.16)
58
+ faraday (~> 0.8)
59
+ faraday_middleware (~> 0.9.0)
60
+ nokogiri (>= 1.6.0)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 1.16)
67
+ carrierwave-yandex-disk!
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+
71
+ BUNDLED WITH
72
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 programrails
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,167 @@
1
+ # Yandex.Disk storage plugin for Carrierwave
2
+
3
+ This gem allows you to set [Yandex.Disk](https://en.wikipedia.org/wiki/Yandex_Disk) (free of charge) as an online storage for the files you upload to your site with [Carrierwave](https://github.com/carrierwaveuploader/carrierwave). It is analogous to the [fog gems family](https://github.com/fog) and is especially useful for using Carrierwave on [Heroku](https://en.wikipedia.org/wiki/Heroku) (where the common file storage is [not supported](https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku)).
4
+
5
+ This gem is NOT intended for an industry-scale cloud files storage (due to the possible Yandex.Disk bandwidth and other limitations). But it's a right fit for your (Heroku-based) job application test work, educational projects, etc.
6
+
7
+ ## Installation
8
+
9
+ Set up a Yandex.Disk free account as described on the [Yandex::Disk gem page](https://github.com/anjlab/yandex-disk).
10
+
11
+ Add these lines to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'carrierwave-yandex-disk'
15
+ ```
16
+
17
+ Set up your Rails application:
18
+
19
+ * Write your OAuth2 token (generated on the Yandex.Disk setup step) into the `config/secrets.yml` file like this:
20
+
21
+ ```ruby
22
+ development:
23
+ yandex_disk_access_token: 'AQAAAAAND3AxAATUPz31jhEFF0P_gltPlOFGi-4'
24
+
25
+ test:
26
+ yandex_disk_access_token: 'AQAAAAAND3AxAATUPz31jhEFF0P_gltPlOFGi-4'
27
+
28
+ production:
29
+ yandex_disk_access_token: <%= ENV["YANDEX_DISK_ACCESS_TOKEN"] %>
30
+ ```
31
+
32
+ See [more info about secrets.yml usage](http://guides.rubyonrails.org/4_1_release_notes.html#config-secrets-yml).
33
+
34
+ * Add your `config/secrets.yml` to the `.gitignore` file (if you haven't done it previously).
35
+
36
+ * Create a file `config/initializers/carrierwave.rb` with the following content:
37
+
38
+ ```ruby
39
+ CarrierWave.configure do |config|
40
+
41
+ config.yandex_disk_access_token = Rails.application.secrets.yandex_disk_access_token
42
+
43
+ end
44
+ ```
45
+
46
+ **NOTE**: You may use whatever other token initialization source here (if you don't like `secrets.yml`).
47
+
48
+ In the target uploader set the storage like this:
49
+
50
+ ```ruby
51
+ # app/uploaders/avatar_uploader.rb
52
+ class AvatarUploader < CarrierWave::Uploader::Base
53
+ #storage :file
54
+ storage :yandex_disk
55
+ end
56
+ ```
57
+
58
+ Set up the [Carrierwave](https://github.com/carrierwaveuploader/carrierwave) gem.
59
+
60
+ ## Usage
61
+
62
+ The usage is basically the same as in the regular (file-storage) Carrierwave case. The uploaded files get immediately "published" (e.g. available to anyone) on Yandex.Disk.
63
+
64
+ **NOTE**: Do not manually operate with the Carrierwave-uploaded files on your Yandex.Disk account!
65
+
66
+ Imagine you defined a model with an uploader:
67
+
68
+ ```ruby
69
+ class User < ApplicationRecord
70
+
71
+ mount_uploader :avatar, AvatarUploader
72
+
73
+ end
74
+ ```
75
+
76
+ Then (in your views) you can access the following methods:
77
+
78
+ **url**:
79
+
80
+ ```ruby
81
+ @user.avatar.url
82
+ ```
83
+
84
+ or
85
+
86
+ ```ruby
87
+ @user.avatar.file.url
88
+ ```
89
+
90
+ Return example:
91
+
92
+ ```ruby
93
+ https://downloader.disk.yandex.ru/disk/25e9fa3c40ea7e440029923e4a4c63e2f01cb66be3cda8cd1a756b8d2f46000f/5a934e46/jP34-9cszbD04Qaxa28_KP9GIgRMt42Dc_8aZRK8u2QXMsbsCPO6xe254apPTxbNg5jWPBB01aCTbWcWJo_f4g%3D%3D?uid=0&filename=user.png&disposition=attachment&hash=aHZ5UF177vQMTgCaLYPLS/VKtUrFKs/wlXlPu%2B7jXUw%3D%3A&limit=0&content_type=image%2Fpng&fsize=21128&hid=662f9a494d1d41839c86afd9c1de6afc&media_type=image&tknv=v2
94
+ ```
95
+
96
+ Yields the direct URL (for your uploaded file) which is dynamically fetched from the server on every view rendering. This is a Yandex.Disk direct link policy limitation.
97
+
98
+ **public_url**:
99
+
100
+ ```ruby
101
+ @user.avatar.file.public_url
102
+ ```
103
+
104
+ Return example:
105
+
106
+ ```ruby
107
+ https://yadi.sk/i/H_D62-Ln3SmAhc
108
+ ```
109
+
110
+ Yields the public URL (for your uploaded file). You may use this value for distributing the uploaded file beyond your site (on forums, blogs, etc).
111
+
112
+ **storage_path**:
113
+
114
+ ```ruby
115
+ @user.avatar.file.storage_path
116
+ ```
117
+
118
+ Return example:
119
+
120
+ ```ruby
121
+ /uploads/user/avatar/1/user.png
122
+ ```
123
+
124
+ Yields the internal storage path (for your uploaded file). You would hardly need this value. It's format depends on the uploader's `store_dir` function.
125
+
126
+ **filename**:
127
+
128
+ ```ruby
129
+ @user.avatar.file.filename
130
+ ```
131
+
132
+ Return example:
133
+
134
+ ```ruby
135
+ user.png
136
+ ```
137
+
138
+ Yields the filename (for your uploaded file).
139
+
140
+ **extension**:
141
+
142
+ ```ruby
143
+ @user.avatar.file.extension
144
+ ```
145
+
146
+ Return example:
147
+
148
+ ```ruby
149
+ .png
150
+ ```
151
+
152
+ Yields the extension (for your uploaded file).
153
+
154
+
155
+ ## Special thanks
156
+
157
+ This project is highly based on the [carrierwave-dropbox](https://github.com/robin850/carrierwave-dropbox) gem.
158
+
159
+ Thanks to its author and contributors!
160
+
161
+ ## Contributing
162
+
163
+ Bug reports and pull requests are welcome on GitHub at https://github.com/programrails/carrierwave-yandex-disk.
164
+
165
+ ## License
166
+
167
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'carrierwave/yandex/disk'
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave/yandex/disk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'carrierwave-yandex-disk'
8
+ spec.version = Carrierwave::Yandex::Disk::VERSION
9
+ spec.authors = ['programrails']
10
+ spec.email = ['programrails@yandex.ru']
11
+
12
+ spec.summary = 'Yandex.Disk integration for CarrierWave'.freeze
13
+ spec.description = 'CarrierWave storage for Yandex.Disk'.freeze
14
+ spec.homepage = 'https://github.com/programrails/carrierwave-yandex-disk'.freeze
15
+ spec.license = 'MIT'.freeze
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_dependency 'carrierwave', '>= 1.2'
28
+ spec.add_dependency 'yandex-disk', '>= 0.0.8'
29
+ end
@@ -0,0 +1,18 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/yandex/storage/disk'
3
+
4
+ require 'carrierwave/yandex/disk/version'
5
+
6
+ module CarrierWave
7
+ module Uploader
8
+ # Rubocop, shut up
9
+ class Base
10
+ add_config :yandex_disk_access_token
11
+
12
+ configure do |config|
13
+ config.storage_engines[:yandex_disk] =
14
+ 'CarrierWave::Storage::Yandex::Disk'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module Carrierwave
2
+ module Yandex
3
+ module Disk
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,113 @@
1
+ require 'yandex/disk'
2
+
3
+ module CarrierWave
4
+ module Storage
5
+ module Yandex
6
+ # Rubocop, shut up
7
+ class Disk < Abstract
8
+ # Stubs we must implement to create and save
9
+ # files (here on Yandex.Disk)
10
+
11
+ # Store a single file
12
+ def helper_create_publish(file)
13
+ location = "/#{uploader.store_path}"
14
+ location_path = "/#{uploader.store_dir}"
15
+
16
+ yandex_disk_client.mkdir_p(location_path)
17
+
18
+ yandex_disk_client.put!(file.path.to_s, location)
19
+
20
+ yandex_disk_client.make_public(location)
21
+ end
22
+
23
+ def helper_update_model(make_public_res)
24
+ location = "/#{uploader.store_path}"
25
+
26
+ public_url = ERB::Util.url_encode make_public_res[:public_url]
27
+
28
+ file_id = { location: location, public_url: public_url }.to_json
29
+
30
+ uploader.model.update_column uploader.mounted_as, file_id
31
+ end
32
+
33
+ def store!(file)
34
+ # I had to break this method into 2 helpers just because
35
+ # rubocop insisted on that. Isn't it stupid?
36
+ res = helper_create_publish(file)
37
+
38
+ helper_update_model(res)
39
+ end
40
+
41
+ # Retrieve a single file
42
+ def retrieve!(file_id)
43
+ file_id_hash = (JSON.parse file_id.gsub('=>', ':')).symbolize_keys
44
+
45
+ public_url = file_id_hash[:public_url]
46
+
47
+ location = file_id_hash[:location]
48
+
49
+ CarrierWave::Storage::Yandex::Disk::File
50
+ .new(public_url, location, yandex_disk_client)
51
+ end
52
+
53
+ def yandex_disk_client
54
+ @yandex_disk_client ||= begin
55
+ ::Yandex::Disk::Client.new(access_token: config[:access_token])
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def config
62
+ @config ||= {}
63
+
64
+ @config[:access_token] ||= uploader.yandex_disk_access_token
65
+
66
+ @config
67
+ end
68
+ # Rubocop, shut up
69
+ class File
70
+ # alike http://www.rubydoc.info/gems/carrierwave/CarrierWave/Storage/Fog/File
71
+ include CarrierWave::Utilities::Uri
72
+
73
+ def initialize(public_url, location, yandex_disk_client)
74
+ @public_url = public_url
75
+ @location = location
76
+ @yandex_disk_client = yandex_disk_client
77
+ end
78
+
79
+ def filename
80
+ ::File.basename @location
81
+ end
82
+
83
+ def extension
84
+ ::File.extname @location
85
+ end
86
+
87
+ def url
88
+ query = 'https://cloud-api.yandex.net/v1/disk/public/resources/' \
89
+ "download?public_key=#{@public_url}"
90
+ uri = URI query
91
+ res = Net::HTTP.get(uri)
92
+ hash = JSON.parse(res)
93
+ hash['href']
94
+ end
95
+
96
+ def public_url
97
+ CGI.unescape @public_url
98
+ end
99
+
100
+ def storage_path
101
+ # deviation from http://www.rubydoc.info/gems/carrierwave/CarrierWave/Storage/Fog/File
102
+ @location
103
+ end
104
+
105
+ def delete
106
+ @yandex_disk_client.delete @location
107
+ @yandex_disk_client.delete(::File.dirname(@location))
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-yandex-disk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - programrails
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: carrierwave
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yandex-disk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.0.8
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.8
83
+ description: CarrierWave storage for Yandex.Disk
84
+ email:
85
+ - programrails@yandex.ru
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - carrierwave-yandex-disk.gemspec
101
+ - lib/carrierwave/yandex/disk.rb
102
+ - lib/carrierwave/yandex/disk/version.rb
103
+ - lib/carrierwave/yandex/storage/disk.rb
104
+ homepage: https://github.com/programrails/carrierwave-yandex-disk
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Yandex.Disk integration for CarrierWave
128
+ test_files: []