activestorage 0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activestorage might be problematic. Click here for more details.

Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +11 -0
  4. data/Gemfile.lock +235 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.md +76 -0
  7. data/Rakefile +11 -0
  8. data/activestorage.gemspec +21 -0
  9. data/lib/active_storage.rb +9 -0
  10. data/lib/active_storage/attached.rb +34 -0
  11. data/lib/active_storage/attached/macros.rb +23 -0
  12. data/lib/active_storage/attached/many.rb +31 -0
  13. data/lib/active_storage/attached/one.rb +29 -0
  14. data/lib/active_storage/attachment.rb +30 -0
  15. data/lib/active_storage/blob.rb +80 -0
  16. data/lib/active_storage/disk_controller.rb +28 -0
  17. data/lib/active_storage/download.rb +90 -0
  18. data/lib/active_storage/filename.rb +31 -0
  19. data/lib/active_storage/migration.rb +28 -0
  20. data/lib/active_storage/purge_job.rb +10 -0
  21. data/lib/active_storage/railtie.rb +56 -0
  22. data/lib/active_storage/service.rb +34 -0
  23. data/lib/active_storage/service/disk_service.rb +70 -0
  24. data/lib/active_storage/service/gcs_service.rb +41 -0
  25. data/lib/active_storage/service/mirror_service.rb +34 -0
  26. data/lib/active_storage/service/s3_service.rb +55 -0
  27. data/lib/active_storage/storage_services.yml +27 -0
  28. data/lib/active_storage/verified_key_with_expiration.rb +24 -0
  29. data/lib/tasks/activestorage.rake +19 -0
  30. data/test/attachments_test.rb +95 -0
  31. data/test/blob_test.rb +28 -0
  32. data/test/database/create_users_migration.rb +7 -0
  33. data/test/database/setup.rb +6 -0
  34. data/test/disk_controller_test.rb +34 -0
  35. data/test/filename_test.rb +36 -0
  36. data/test/service/.gitignore +1 -0
  37. data/test/service/configurations-example.yml +11 -0
  38. data/test/service/disk_service_test.rb +8 -0
  39. data/test/service/gcs_service_test.rb +20 -0
  40. data/test/service/mirror_service_test.rb +50 -0
  41. data/test/service/s3_service_test.rb +11 -0
  42. data/test/service/shared_service_tests.rb +68 -0
  43. data/test/test_helper.rb +28 -0
  44. data/test/verified_key_with_expiration_test.rb +19 -0
  45. metadata +171 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d5b78ca3e20511930821d54a0c608c70d8ed125
4
+ data.tar.gz: 8d2e522c210eee6b36f0d8b8a62af7a0c7e2df0f
5
+ SHA512:
6
+ metadata.gz: 512f8d05b25a4f040f6475d6ffea581ebb3c643e4055df64f6c78ae97b3fa61bcf41ac4d4df5af638878031c9e53ec4a94b03dc673747a10bff12b532ec26d2c
7
+ data.tar.gz: 1db5a5435511c6ad157c2cd714dee4c229854cc74468890a262307d1b707a2fa909cacfedd12e8f297e11870773eac5d2c3da8610f097f8ee11687d806993bff
@@ -0,0 +1 @@
1
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'byebug'
7
+
8
+ gem 'sqlite3'
9
+
10
+ gem 'aws-sdk', require: false
11
+ gem 'google-cloud', require: false
@@ -0,0 +1,235 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activestorage (0.1)
5
+ actionpack (>= 5.1)
6
+ activejob (>= 5.1)
7
+ activerecord (>= 5.1)
8
+ activesupport (>= 5.1)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actionpack (5.1.1)
14
+ actionview (= 5.1.1)
15
+ activesupport (= 5.1.1)
16
+ rack (~> 2.0)
17
+ rack-test (~> 0.6.3)
18
+ rails-dom-testing (~> 2.0)
19
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
20
+ actionview (5.1.1)
21
+ activesupport (= 5.1.1)
22
+ builder (~> 3.1)
23
+ erubi (~> 1.4)
24
+ rails-dom-testing (~> 2.0)
25
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
26
+ activejob (5.1.1)
27
+ activesupport (= 5.1.1)
28
+ globalid (>= 0.3.6)
29
+ activemodel (5.1.1)
30
+ activesupport (= 5.1.1)
31
+ activerecord (5.1.1)
32
+ activemodel (= 5.1.1)
33
+ activesupport (= 5.1.1)
34
+ arel (~> 8.0)
35
+ activesupport (5.1.1)
36
+ concurrent-ruby (~> 1.0, >= 1.0.2)
37
+ i18n (~> 0.7)
38
+ minitest (~> 5.1)
39
+ tzinfo (~> 1.1)
40
+ addressable (2.5.1)
41
+ public_suffix (~> 2.0, >= 2.0.2)
42
+ arel (8.0.0)
43
+ aws-sdk (2.10.7)
44
+ aws-sdk-resources (= 2.10.7)
45
+ aws-sdk-core (2.10.7)
46
+ aws-sigv4 (~> 1.0)
47
+ jmespath (~> 1.0)
48
+ aws-sdk-resources (2.10.7)
49
+ aws-sdk-core (= 2.10.7)
50
+ aws-sigv4 (1.0.0)
51
+ builder (3.2.3)
52
+ byebug (9.0.6)
53
+ concurrent-ruby (1.0.5)
54
+ declarative (0.0.9)
55
+ declarative-option (0.1.0)
56
+ digest-crc (0.4.1)
57
+ erubi (1.6.0)
58
+ faraday (0.12.1)
59
+ multipart-post (>= 1.2, < 3)
60
+ globalid (0.4.0)
61
+ activesupport (>= 4.2.0)
62
+ google-api-client (0.13.0)
63
+ addressable (~> 2.5, >= 2.5.1)
64
+ googleauth (~> 0.5)
65
+ httpclient (>= 2.8.1, < 3.0)
66
+ mime-types (~> 3.0)
67
+ representable (~> 3.0)
68
+ retriable (>= 2.0, < 4.0)
69
+ google-cloud (0.34.0)
70
+ google-cloud-bigquery (~> 0.27.0)
71
+ google-cloud-datastore (~> 1.0)
72
+ google-cloud-dns (~> 0.25.0)
73
+ google-cloud-error_reporting (~> 0.25.0)
74
+ google-cloud-language (~> 0.26.0)
75
+ google-cloud-logging (~> 1.0)
76
+ google-cloud-monitoring (~> 0.24.0)
77
+ google-cloud-pubsub (~> 0.25.0)
78
+ google-cloud-resource_manager (~> 0.26.0)
79
+ google-cloud-spanner (~> 0.21.0)
80
+ google-cloud-speech (~> 0.24.0)
81
+ google-cloud-storage (~> 1.2)
82
+ google-cloud-trace (~> 0.25.0)
83
+ google-cloud-translate (~> 1.0)
84
+ google-cloud-video_intelligence (~> 0.20.0)
85
+ google-cloud-vision (~> 0.24.0)
86
+ google-cloud-bigquery (0.27.0)
87
+ google-api-client (~> 0.13.0)
88
+ google-cloud-core (~> 1.0)
89
+ google-cloud-core (1.0.0)
90
+ google-cloud-env (~> 1.0)
91
+ googleauth (~> 0.5.1)
92
+ google-cloud-datastore (1.0.1)
93
+ google-cloud-core (~> 1.0)
94
+ google-gax (~> 0.8.0)
95
+ google-protobuf (~> 3.2.0)
96
+ google-cloud-dns (0.25.0)
97
+ google-api-client (~> 0.13.0)
98
+ google-cloud-core (~> 1.0)
99
+ zonefile (~> 1.04)
100
+ google-cloud-env (1.0.0)
101
+ faraday (~> 0.11)
102
+ google-cloud-error_reporting (0.25.0)
103
+ google-cloud-core (~> 1.0)
104
+ google-gax (~> 0.8.0)
105
+ stackdriver-core (~> 1.1)
106
+ google-cloud-language (0.26.2)
107
+ google-cloud-core (~> 1.0)
108
+ google-gax (~> 0.8.2)
109
+ google-cloud-logging (1.1.0)
110
+ google-cloud-core (~> 1.0)
111
+ google-gax (~> 0.8.0)
112
+ stackdriver-core (~> 1.1)
113
+ google-cloud-monitoring (0.24.0)
114
+ google-gax (~> 0.8.0)
115
+ google-cloud-pubsub (0.25.0)
116
+ google-cloud-core (~> 1.0)
117
+ google-gax (~> 0.8.0)
118
+ grpc-google-iam-v1 (~> 0.6.8)
119
+ google-cloud-resource_manager (0.26.0)
120
+ google-api-client (~> 0.13.0)
121
+ google-cloud-core (~> 1.0)
122
+ google-cloud-spanner (0.21.0)
123
+ concurrent-ruby (~> 1.0)
124
+ google-cloud-core (~> 1.0)
125
+ google-gax (~> 0.8.1)
126
+ grpc (~> 1.1)
127
+ grpc-google-iam-v1 (~> 0.6.8)
128
+ google-cloud-speech (0.24.0)
129
+ google-cloud-core (~> 1.0)
130
+ google-gax (~> 0.8.2)
131
+ google-cloud-storage (1.2.0)
132
+ digest-crc (~> 0.4)
133
+ google-api-client (~> 0.13.0)
134
+ google-cloud-core (~> 1.0)
135
+ google-cloud-trace (0.25.0)
136
+ google-cloud-core (~> 1.0)
137
+ google-gax (~> 0.8.0)
138
+ stackdriver-core (~> 1.1)
139
+ google-cloud-translate (1.0.0)
140
+ google-cloud-core (~> 1.0)
141
+ googleauth (~> 0.5.1)
142
+ google-cloud-video_intelligence (0.20.0)
143
+ google-gax (~> 0.8.0)
144
+ google-cloud-vision (0.24.0)
145
+ google-cloud-core (~> 1.0)
146
+ google-gax (~> 0.8.0)
147
+ google-gax (0.8.4)
148
+ google-protobuf (~> 3.2)
149
+ googleapis-common-protos (~> 1.3.5)
150
+ googleauth (~> 0.5.1)
151
+ grpc (~> 1.0)
152
+ rly (~> 0.2.3)
153
+ google-protobuf (3.2.0.2)
154
+ googleapis-common-protos (1.3.5)
155
+ google-protobuf (~> 3.2)
156
+ grpc (~> 1.0)
157
+ googleauth (0.5.1)
158
+ faraday (~> 0.9)
159
+ jwt (~> 1.4)
160
+ logging (~> 2.0)
161
+ memoist (~> 0.12)
162
+ multi_json (~> 1.11)
163
+ os (~> 0.9)
164
+ signet (~> 0.7)
165
+ grpc (1.4.1)
166
+ google-protobuf (~> 3.1)
167
+ googleauth (~> 0.5.1)
168
+ grpc-google-iam-v1 (0.6.8)
169
+ googleapis-common-protos (~> 1.3.1)
170
+ googleauth (~> 0.5.1)
171
+ grpc (~> 1.0)
172
+ httpclient (2.8.3)
173
+ i18n (0.8.4)
174
+ jmespath (1.3.1)
175
+ jwt (1.5.6)
176
+ little-plugger (1.1.4)
177
+ logging (2.2.2)
178
+ little-plugger (~> 1.1)
179
+ multi_json (~> 1.10)
180
+ loofah (2.0.3)
181
+ nokogiri (>= 1.5.9)
182
+ memoist (0.16.0)
183
+ mime-types (3.1)
184
+ mime-types-data (~> 3.2015)
185
+ mime-types-data (3.2016.0521)
186
+ mini_portile2 (2.1.0)
187
+ minitest (5.10.2)
188
+ multi_json (1.12.1)
189
+ multipart-post (2.0.0)
190
+ nokogiri (1.7.2)
191
+ mini_portile2 (~> 2.1.0)
192
+ os (0.9.6)
193
+ public_suffix (2.0.5)
194
+ rack (2.0.3)
195
+ rack-test (0.6.3)
196
+ rack (>= 1.0)
197
+ rails-dom-testing (2.0.3)
198
+ activesupport (>= 4.2.0)
199
+ nokogiri (>= 1.6)
200
+ rails-html-sanitizer (1.0.3)
201
+ loofah (~> 2.0)
202
+ rake (12.0.0)
203
+ representable (3.0.4)
204
+ declarative (< 0.1.0)
205
+ declarative-option (< 0.2.0)
206
+ uber (< 0.2.0)
207
+ retriable (3.0.2)
208
+ rly (0.2.3)
209
+ signet (0.7.3)
210
+ addressable (~> 2.3)
211
+ faraday (~> 0.9)
212
+ jwt (~> 1.5)
213
+ multi_json (~> 1.10)
214
+ sqlite3 (1.3.13)
215
+ stackdriver-core (1.1.0)
216
+ thread_safe (0.3.6)
217
+ tzinfo (1.2.3)
218
+ thread_safe (~> 0.1)
219
+ uber (0.1.0)
220
+ zonefile (1.06)
221
+
222
+ PLATFORMS
223
+ ruby
224
+
225
+ DEPENDENCIES
226
+ activestorage!
227
+ aws-sdk
228
+ bundler (~> 1.15)
229
+ byebug
230
+ google-cloud
231
+ rake
232
+ sqlite3
233
+
234
+ BUNDLED WITH
235
+ 1.15.1
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 David Heinemeier Hansson, Basecamp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Active Storage
2
+
3
+ Active Storage makes it simple to upload and reference files in cloud services, like Amazon S3 or Google Cloud Storage,
4
+ and attach those files to Active Records. It also provides a disk service for testing or local deployments, but the
5
+ focus is on cloud storage.
6
+
7
+ ## Examples
8
+
9
+ One attachment:
10
+
11
+ ```ruby
12
+ class User < ApplicationRecord
13
+ has_one_attached :avatar
14
+ end
15
+
16
+ user.avatar.attach io: File.open("~/face.jpg"), filename: "avatar.jpg", content_type: "image/jpg"
17
+ user.avatar.exist? # => true
18
+
19
+ user.avatar.purge
20
+ user.avatar.exist? # => false
21
+
22
+ user.avatar.url(expires_in: 5.minutes) # => /rails/blobs/<encoded-key>
23
+
24
+ class AvatarsController < ApplicationController
25
+ def update
26
+ Current.user.avatar.attach(params.require(:avatar))
27
+ redirect_to Current.user
28
+ end
29
+ end
30
+ ```
31
+
32
+ Many attachments:
33
+
34
+ ```ruby
35
+ class Message < ApplicationRecord
36
+ has_many_attached :images
37
+ end
38
+
39
+ <%= form_with model: @message do |form| %>
40
+ <%= form.text_field :title, placeholder: "Title" %><br>
41
+ <%= form.text_area :content %><br><br>
42
+
43
+ <%= form.file_field :images, multiple: true %><br>
44
+ <%= form.submit %>
45
+ <% end %>
46
+
47
+ class MessagesController < ApplicationController
48
+ def create
49
+ message = Message.create! params.require(:message).permit(:title, :content)
50
+ message.images.attach(params[:message][:images])
51
+ redirect_to message
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ 1. Add `require "active_storage"` to config/application.rb.
59
+ 2. Run `rails activestorage:install` to create needed directories, migrations, and configuration.
60
+ 3. Configure the storage service in `config/environments/*` with `config.active_storage.service = :local`
61
+ that references the services configured in `config/storage_services.yml`.
62
+
63
+ ## Todos
64
+
65
+ - Document all the classes
66
+ - Strip Download of its resposibilities and delete class
67
+ - Proper logging
68
+ - Convert MirrorService to use threading
69
+ - Read metadata via Marcel?
70
+ - Add Migrator to copy/move between services
71
+ - Explore direct uploads to cloud
72
+ - Extract VerifiedKeyWithExpiration into Rails as a feature of MessageVerifier
73
+
74
+ ## License
75
+
76
+ Active Storage is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.libs << "test"
7
+ test.test_files = FileList["test/**/*_test.rb"]
8
+ test.warning = false
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "activestorage"
3
+ s.version = "0.1"
4
+ s.authors = "David Heinemeier Hansson"
5
+ s.email = "david@basecamp.com"
6
+ s.summary = "Attach cloud and local files in Rails applications"
7
+ s.homepage = "https://github.com/rails/activestorage"
8
+ s.license = "MIT"
9
+
10
+ s.required_ruby_version = ">= 2.3.0"
11
+
12
+ s.add_dependency "activesupport", ">= 5.1"
13
+ s.add_dependency "activerecord", ">= 5.1"
14
+ s.add_dependency "actionpack", ">= 5.1"
15
+ s.add_dependency "activejob", ">= 5.1"
16
+
17
+ s.add_development_dependency "bundler", "~> 1.15"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- test/*`.split("\n")
21
+ end
@@ -0,0 +1,9 @@
1
+ require "active_record"
2
+ require "active_storage/railtie" if defined?(Rails)
3
+
4
+ module ActiveStorage
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Blob
8
+ autoload :Service
9
+ end
@@ -0,0 +1,34 @@
1
+ require "active_storage/blob"
2
+ require "active_storage/attachment"
3
+
4
+ require "action_dispatch/http/upload"
5
+ require "active_support/core_ext/module/delegation"
6
+
7
+ class ActiveStorage::Attached
8
+ attr_reader :name, :record
9
+
10
+ def initialize(name, record)
11
+ @name, @record = name, record
12
+ end
13
+
14
+ private
15
+ def create_blob_from(attachable)
16
+ case attachable
17
+ when ActiveStorage::Blob
18
+ attachable
19
+ when ActionDispatch::Http::UploadedFile
20
+ ActiveStorage::Blob.create_after_upload! \
21
+ io: attachable.open,
22
+ filename: attachable.original_filename,
23
+ content_type: attachable.content_type
24
+ when Hash
25
+ ActiveStorage::Blob.create_after_upload!(attachable)
26
+ else
27
+ nil
28
+ end
29
+ end
30
+ end
31
+
32
+ require "active_storage/attached/one"
33
+ require "active_storage/attached/many"
34
+ require "active_storage/attached/macros"
@@ -0,0 +1,23 @@
1
+ module ActiveStorage::Attached::Macros
2
+ def has_one_attached(name, dependent: :purge_later)
3
+ define_method(name) do
4
+ instance_variable_get("@active_storage_attached_#{name}") ||
5
+ instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
6
+ end
7
+
8
+ if dependent == :purge_later
9
+ before_destroy { public_send(name).purge_later }
10
+ end
11
+ end
12
+
13
+ def has_many_attached(name, dependent: :purge_later)
14
+ define_method(name) do
15
+ instance_variable_get("@active_storage_attached_#{name}") ||
16
+ instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
17
+ end
18
+
19
+ if dependent == :purge_later
20
+ before_destroy { public_send(name).purge_later }
21
+ end
22
+ end
23
+ end