shrine-transloadit 1.0.0.beta → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 379b39a12a74f5b4e3e0c9336c1cc560eaa5ce78596fc55aa8990ea017d632bf
4
- data.tar.gz: '04930268d5f2c682dac35cb635fd2e0be617fc6ce1c3bd55b0ee572f771b300f'
3
+ metadata.gz: 19c66daf493f1fe800d44d3264dcc0a5699f1d177322781ac61b81cd928d8922
4
+ data.tar.gz: e3a5960f9c9c5db0ed2adfd2299dc7972b23ae635a55951144344135cb1af121
5
5
  SHA512:
6
- metadata.gz: 0bc371cbec27b5cf8a642eedea74c372e5b1bbef48b82956635a28dd76b02eb1436870a5ba3b18671f26bc897199c77c995df16070704688fecb3d42d11b8f74
7
- data.tar.gz: 3da98b2895dd0af05e9f6aac2cc835105b754ef91e0ae0cd528b6cd7ace93a9ac82e8b7dc1ae5dc22b928d24caff89ebb3203b9a97735ac69349e97249ec89b4
6
+ metadata.gz: 063645f16216fb57eccc422b5fc515ec27c8abc1b47ef75f6dab29db4e017bcce4af08d95cb066aa9cb7632181706243387ddc82eb90bf27b615fe1dfcac0415
7
+ data.tar.gz: 04bfb71e941d72369aa91b8d473dc24472991c2b6c63fb99090a2125d9d4123542d98b8e6f3785c100419df91a54342fa4cb1c2e7ec53d484b311fdeb57fa495
data/README.md CHANGED
@@ -10,19 +10,19 @@ generate audio waveforms and more.
10
10
 
11
11
  * [Installation](#installation)
12
12
  * [Setup](#setup)
13
- - [Credentials](#credentials)
14
- * [Usage](#usge)
15
- - [Backgrounding](#backgrounding)
13
+ * [Usage](#usage)
16
14
  * [Notifications](#notifications)
17
15
  * [Direct uploads](#direct-uploads)
18
16
  * [Promotion](#promotion)
19
17
  * [Skipping exports](#skipping-exports)
20
18
  * [API](#api)
21
- - [Processing & Saving](#processing-saving)
22
- - [Generating steps](#generating-steps)
23
- - [Parsing files](#parsing-files)
24
- - [Verifying signature](#verifying-signature)
25
- - [Transloadit instance](#transloadit-instance)
19
+ - [Processor](#processor)
20
+ - [Saver](#saver)
21
+ - [Step](#step)
22
+ - [Import step](#import-step)
23
+ - [Export step](#export-step)
24
+ - [File](#file)
25
+ * [Instrumentation](#instrumentation)
26
26
 
27
27
  ## Installation
28
28
 
@@ -35,39 +35,30 @@ gem "shrine-transloadit", "~> 1.0"
35
35
 
36
36
  ## Setup
37
37
 
38
- Load the `transloadit` plugin and configure your Transloadit key and secret:
39
-
40
- ```rb
41
- Shrine.plugin :transloadit, auth: {
42
- key: "YOUR_TRANSLOADIT_KEY",
43
- secret: "YOUR_TRANSLOADIT_SECRET",
44
- }
45
- ```
46
-
47
- ### Credentials
48
-
49
- You'll need to create [credentials] for the storage services you want to import
50
- from and export to. Then you need to map these credentials to Shrine storages:
38
+ You'll first need to create [credentials] for the storage service you want to
39
+ import from and export to. Let's assume you're using S3 and have named the
40
+ credentials `s3_store`. Now you can load the `transloadit` plugin, providing
41
+ Transloadit key & secret, and mapping credentials to Shrine storages:
51
42
 
52
43
  ```rb
44
+ # example storage configuration
53
45
  Shrine.storages = {
54
46
  cache: Shrine::Storage::S3.new(prefix: "cache", **options),
55
47
  store: Shrine::Storage::S3.new(**options),
56
48
  }
57
49
 
58
- Shrine.plugin :transloadit, auth: { ... },
50
+ # transloadit plugin configuration
51
+ Shrine.plugin :transloadit,
52
+ auth: {
53
+ key: "YOUR_TRANSLOADIT_KEY",
54
+ secret: "YOUR_TRANSLOADIT_SECRET",
55
+ },
59
56
  credentials: {
60
57
  cache: :s3_store, # use "s3_store" credentials for :cache storage
61
58
  store: :s3_store, # use "s3_store" credentials for :store storage
62
59
  }
63
- ```
64
-
65
- ### Derivatives
66
60
 
67
- The examples will assume you have the [`derivatives`][derivatives] plugin
68
- loaded:
69
-
70
- ```rb
61
+ # for storing processed files
71
62
  Shrine.plugin :derivatives
72
63
  ```
73
64
 
@@ -83,7 +74,7 @@ files as derivatives:
83
74
 
84
75
  ```rb
85
76
  class VideoUploader < Shrine
86
- Attacher.transloadit_processor :video do
77
+ Attacher.transloadit_processor do
87
78
  import = file.transloadit_import_step
88
79
  encode = transloadit_step "encode", "/video/encode", use: import
89
80
  thumbs = transloadit_step "thumbs", "/video/thumbs", use: import
@@ -93,23 +84,23 @@ class VideoUploader < Shrine
93
84
  assembly.create!
94
85
  end
95
86
 
96
- Attacher.transloadit_saver :video do |response|
97
- transcoded = store.transloadit_file(response["results"]["encode"])
98
- thumbnails = store.transloadit_files(response["results"]["thumbs"])
87
+ Attacher.transloadit_saver do |results|
88
+ transcoded = store.transloadit_file(results["encode"])
89
+ thumbnails = store.transloadit_files(results["thumbs"])
99
90
 
100
91
  merge_derivatives(transcoded: transcoded, thumbnails: thumbnails)
101
92
  end
102
93
  end
103
94
  ```
104
95
  ```rb
105
- response = attacher.transloadit_process(:video)
96
+ response = attacher.transloadit_process
106
97
  response.reload_until_finished!
107
98
 
108
99
  if response.error?
109
100
  # handle error
110
101
  end
111
102
 
112
- attacher.transloadit_save(:video, response)
103
+ attacher.transloadit_save(response["results"])
113
104
  attacher.derivatives #=>
114
105
  # {
115
106
  # transcoded: #<Shrine::UploadedFile storage_key=:store ...>,
@@ -131,7 +122,7 @@ class PromoteJob
131
122
  def perform(record, name, file_data)
132
123
  attacher = Shrine::Attacher.retrieve(model: record, name: name, file: file_data)
133
124
  attacher.atomic_promote
134
- attacher.transloadit_process(:video)
125
+ attacher.transloadit_process
135
126
  # ...
136
127
  rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
137
128
  end
@@ -144,7 +135,7 @@ When using [assembly notifications], the attacher data can be sent to the
144
135
  webhook via `:fields`:
145
136
 
146
137
  ```rb
147
- Attacher.transloadit_processor :video do
138
+ Attacher.transloadit_processor do
148
139
  # ...
149
140
  assembly = transloadit.assembly(
150
141
  steps: [ ... ],
@@ -175,20 +166,17 @@ post "/transloadit/video" do
175
166
  record_class, record_id, name, file_data = response["fields"]["attacher"].values
176
167
  record_class = Object.const_get(record_class)
177
168
 
169
+ attacher = record_class.send(:"#{name}_attacher")
170
+ derivatives = attacher.transloadit_save(response["results"])
171
+
178
172
  begin
179
173
  record = record_class.find(record_id)
180
174
  attacher = Shrine::Attacher.retrieve(model: record, name: name, file: file_data)
181
175
 
182
- attacher.transloadit_save(:video, response)
183
-
176
+ attacher.merge_derivatives(derivatives)
184
177
  attacher.atomic_persist
185
178
  rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
186
- unless attacher
187
- attacher = record_class.send(:"#{name}_attacher")
188
- attacher.transloadit_save(:video, response)
189
- end
190
-
191
- attacher.destroy(background: true) # delete orphaned files
179
+ attacher.destroy_attached # delete orphaned processed files
192
180
  end
193
181
 
194
182
  # return successful response for Transloadit
@@ -229,20 +217,20 @@ storage, you can skip promotion on the Shrine side:
229
217
 
230
218
  ```rb
231
219
  class VideoUploader < Shrine
232
- Attacher.transloadit_processor :video do
220
+ Attacher.transloadit_processor do
233
221
  import = file.transloadit_import_step
234
222
  encode = transloadit_step "encode", "/video/encode", use: import
235
223
  thumbs = transloadit_step "thumbs", "/video/thumbs", use: import
236
224
  export = store.transloadit_export_step use: [import, encode, thumbs] # include original
237
225
 
238
- assembly = transloadit.assembly(use: [import, encode, thumbs, export])
226
+ assembly = transloadit.assembly(steps: [import, encode, thumbs, export])
239
227
  assembly.create!
240
228
  end
241
229
 
242
- Attacher.transloadit_saver :video do |response|
243
- stored = store.transloadit_file(response["results"]["import"])
244
- transcoded = store.transloadit_file(response["results"]["encode"])
245
- thumbnails = store.transloadit_files(response["results"]["thumbs"])
230
+ Attacher.transloadit_saver do |results|
231
+ stored = store.transloadit_file(results["import"])
232
+ transcoded = store.transloadit_file(results["encode"])
233
+ thumbnails = store.transloadit_files(results["thumbs"])
246
234
 
247
235
  set(stored) # set promoted file
248
236
  merge_derivatives(transcoded: transcoded, thumbnails: thumbnails)
@@ -254,20 +242,17 @@ class PromoteJob
254
242
  def perform(record, name, file_data)
255
243
  attacher = Shrine::Attacher.retrieve(model: record, name: name, file: file_data)
256
244
 
257
- response = attacher.transloadit_process(:video)
245
+ response = attacher.transloadit_process
258
246
  response.reload_until_finished!
259
247
 
260
248
  if response.error?
261
249
  # handle error
262
250
  end
263
251
 
264
- original_file = attacher.file
265
- attacher.transloadit_save(:video, response)
266
-
267
- attacher.atomic_persist(original_file)
252
+ attacher.transloadit_save(response["results"])
253
+ attacher.atomic_persist attacher.uploaded_file(file_data)
268
254
  rescue Shrine::AttachmentChanged, ActiveRecord::RecordNotFound
269
- # delete orphaned processed files (backgrounding plugin is recommended)
270
- attacher.destroy(background: true) if attacher
255
+ attacher&.destroy_attached # delete orphaned processed files
271
256
  end
272
257
  end
273
258
  ```
@@ -297,7 +282,7 @@ the `:url` storage, and then upload them to your permanent storage:
297
282
 
298
283
  ```rb
299
284
  class VideoUploader < Shrine
300
- Attacher.transloadit_processor :video do
285
+ Attacher.transloadit_processor do
301
286
  import = file.transloadit_import_step
302
287
  encode = transloadit_step "encode", "/video/encode", use: import
303
288
  thumbs = transloadit_step "thumbs", "/video/thumbs", use: import
@@ -307,10 +292,10 @@ class VideoUploader < Shrine
307
292
  assembly.create!
308
293
  end
309
294
 
310
- Attacher.transloadit_saver :video do |response|
295
+ Attacher.transloadit_saver do |results|
311
296
  url = shrine_class.new(:url)
312
- transcoded = url.transloadit_file(response["results"]["encode"])
313
- thumbnails = url.transloadit_files(response["results"]["thumbs"])
297
+ transcoded = url.transloadit_file(results["encode"])
298
+ thumbnails = url.transloadit_files(results["thumbs"])
314
299
 
315
300
  # results are uploaded to Transloadit's temporary storage
316
301
  transcoded #=> #<Shrine::UploadedFile @storage_key=:url @id="https://tmp.transloadit.com/..." ...>
@@ -322,14 +307,14 @@ class VideoUploader < Shrine
322
307
  end
323
308
  ```
324
309
  ```rb
325
- response = attacher.transloadit_process(:video)
310
+ response = attacher.transloadit_process
326
311
  response.reload_until_finished!
327
312
 
328
313
  if response.error?
329
314
  # handle error
330
315
  end
331
316
 
332
- attacher.transloadit_save(:video, response)
317
+ attacher.transloadit_save(response["results"])
333
318
  attacher.derivatives #=>
334
319
  # {
335
320
  # transcoded: #<Shrine::UploadedFile storage_key=:store ...>,
@@ -549,10 +534,30 @@ file = uploader.transloadit_file(
549
534
  # ...
550
535
  )
551
536
 
537
+ file #=> #<Shrine::UploadedFile @id="foo" storage_key=:store ...>
538
+
552
539
  file.storage #=> #<Shrine::Storage::S3>
553
540
  file.id #=> "foo"
554
541
  ```
555
542
 
543
+ You can use the plural `Shrine#transloadit_files` to convert an array of
544
+ results:
545
+
546
+ ```rb
547
+ files = uploader.transloadit_files [
548
+ { "url" => "https://my-bucket.s3.amazonaws.com/foo", ... },
549
+ { "url" => "https://my-bucket.s3.amazonaws.com/bar", ... },
550
+ { "url" => "https://my-bucket.s3.amazonaws.com/baz", ... },
551
+ ]
552
+
553
+ files #=>
554
+ # [
555
+ # #<Shrine::UploadedFile @id="foo" @storage_key=:store ...>,
556
+ # #<Shrine::UploadedFile @id="bar" @storage_key=:store ...>,
557
+ # #<Shrine::UploadedFile @id="baz" @storage_key=:store ...>,
558
+ # ]
559
+ ```
560
+
556
561
  It will include basic metadata:
557
562
 
558
563
  ```rb
@@ -600,6 +605,48 @@ file = uploader.transloadit_file(
600
605
  file.id #=> "https://example.com/foo"
601
606
  ```
602
607
 
608
+ ## Instrumentation
609
+
610
+ If the `instrumentation` plugin has been loaded, the `transloadit` plugin adds
611
+ instrumentation around triggering processing.
612
+
613
+ ```rb
614
+ # instrumentation plugin needs to be loaded *before* transloadit
615
+ plugin :instrumentation
616
+ plugin :transloadit
617
+ ```
618
+
619
+ Calling the processor will trigger a `transloadit.shrine` event with the
620
+ following payload:
621
+
622
+ | Key | Description |
623
+ | :---- | :---------- |
624
+ | `:processor` | Name of the processor |
625
+ | `:uploader` | The uploader class that sent the event |
626
+
627
+ A default log subscriber is added as well which logs these events:
628
+
629
+ ```
630
+ Transloadit (1238ms) – {:processor=>:video, :uploader=>VideoUploader}
631
+ ```
632
+
633
+ You can also use your own log subscriber:
634
+
635
+ ```rb
636
+ plugin :transloadit, log_subscriber: -> (event) {
637
+ Shrine.logger.info JSON.generate(name: event.name, duration: event.duration, **event.payload)
638
+ }
639
+ ```
640
+ ```
641
+ {"name":"transloadit","duration":1238,"processor":"video","uploader":"VideoUploader"}
642
+ ```
643
+
644
+ Or disable logging altogether:
645
+
646
+ ```rb
647
+ plugin :transloadit, log_subscriber: nil
648
+ ```
649
+
603
650
  ## Contributing
604
651
 
605
652
  Tests are run with:
@@ -37,7 +37,7 @@ class Shrine
37
37
  end
38
38
 
39
39
  module AttacherClassMethods
40
- def transloadit_processor(name, &block)
40
+ def transloadit_processor(name = :default, &block)
41
41
  if block
42
42
  shrine_class.opts[:transloadit][:processors][name.to_sym] = block
43
43
  else
@@ -46,7 +46,7 @@ class Shrine
46
46
  end
47
47
  end
48
48
 
49
- def transloadit_saver(name, &block)
49
+ def transloadit_saver(name = :default, &block)
50
50
  if block
51
51
  shrine_class.opts[:transloadit][:savers][name.to_sym] = block
52
52
  else
@@ -57,14 +57,19 @@ class Shrine
57
57
  end
58
58
 
59
59
  module AttacherMethods
60
- def transloadit_process(name, *args)
60
+ def transloadit_process(name = :default, *args)
61
61
  processor = self.class.transloadit_processor(name)
62
62
  instrument_transloadit(name) do
63
63
  instance_exec(*args, &processor)
64
64
  end
65
65
  end
66
66
 
67
- def transloadit_save(name, *args)
67
+ def transloadit_save(name = :default, *args)
68
+ unless name.respond_to?(:to_sym)
69
+ args.prepend(name)
70
+ name = :default
71
+ end
72
+
68
73
  saver = self.class.transloadit_saver(name)
69
74
  instance_exec(*args, &saver)
70
75
  end
@@ -147,19 +152,14 @@ class Shrine
147
152
  fail Error, "storage not supported: #{storage.inspect}"
148
153
  end
149
154
 
150
- metadata = {
151
- "filename" => result.fetch("name"),
152
- "size" => result.fetch("size"),
153
- "mime_type" => result.fetch("mime"),
154
- }
155
-
156
- # merge transloadit's meatadata, but don't let it override ours
157
- metadata.merge!(result.fetch("meta")) { |k, v1, v2| v1 }
158
-
159
155
  self.class::UploadedFile.new(
160
156
  id: id,
161
157
  storage: storage_key,
162
- metadata: metadata,
158
+ metadata: result.fetch("meta").merge(
159
+ "filename" => result.fetch("name"),
160
+ "size" => result.fetch("size"),
161
+ "mime_type" => result.fetch("mime"),
162
+ )
163
163
  )
164
164
  end
165
165
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-transloadit"
3
- gem.version = "1.0.0.beta"
3
+ gem.version = "1.0.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.2"
6
6
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "*.gemspec"]
14
14
  gem.require_path = "lib"
15
15
 
16
- gem.add_dependency "shrine", ">= 3.0.0.beta2", "< 4"
16
+ gem.add_dependency "shrine", ">= 3.0.0.beta3", "< 4"
17
17
  gem.add_dependency "transloadit", "~> 2.0"
18
18
 
19
19
  gem.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-transloadit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-11 00:00:00.000000000 Z
11
+ date: 2019-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0.beta2
19
+ version: 3.0.0.beta3
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '4'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 3.0.0.beta2
29
+ version: 3.0.0.beta3
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '4'
@@ -140,9 +140,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
140
  version: '2.2'
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ">"
143
+ - - ">="
144
144
  - !ruby/object:Gem::Version
145
- version: 1.3.1
145
+ version: '0'
146
146
  requirements: []
147
147
  rubygems_version: 3.0.3
148
148
  signing_key: