activestorage-autobots 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +71 -0
- data/lib/active_storage/autobots/railtie.rb +13 -0
- data/lib/active_storage/autobots/version.rb +5 -0
- data/lib/active_storage/autobots.rb +82 -0
- data/lib/activestorage-autobots.rb +1 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 408099118b75915f2050d104ce5e0a967c5ccd8bdb91732f2b616b23dd9fc56c
|
4
|
+
data.tar.gz: 6f8a8078c3a51f9c2c17151027aff180f9fa9b87fb89f57fcb228affb63e0b8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bee39d51c6aaf45e772cdb9f3364b3e154bcbfdc91689a4cea6adae504f9e6a818db19f3833ac3e160524394c09ab1ddb31b60d98e6f65a78de27e5532d7ffb5
|
7
|
+
data.tar.gz: 7b6968cdf2325b54c94bb24e45dd27f3796daa15573d7d631c054cb483ce5e26341511997628d9ec2191a9f5751baf0f2cb7261036dc1da5eb676ae48cae12d7
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 JetThoughts
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/activestorage-autobots.svg)](https://rubygems.org/gems/activestorage-autobots)
|
2
|
+
[![Build](https://github.com/jetthoughts/activestorage-autobots/workflows/Build/badge.svg)](https://github.com/jetthoughts/activestorage-autobots/actions)
|
3
|
+
|
4
|
+
# ActiveStorage Autobots (aka ActiveStorage Transformers)
|
5
|
+
|
6
|
+
Enables ActiveStorage variants for other file types than images, such as audio or video files, through an API for registering custom transformers similar to previewers. An example `ffmpeg` transformation is provided.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'activestorage-autobots'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# lib/active_storage/transformers/ffmpeg_transformer.rb
|
24
|
+
|
25
|
+
require 'active_storage/transformers/transformer'
|
26
|
+
|
27
|
+
class ActiveStorage::Transformers::FFMPEGTransformer < ActiveStorage::Transformers::Transformer
|
28
|
+
def self.accept?(blob)
|
29
|
+
blob.video? || blob.audio?
|
30
|
+
end
|
31
|
+
|
32
|
+
def transform(input, format:)
|
33
|
+
format ||= File.extname(input.path)
|
34
|
+
options = transformations[:ffmpeg_opts]
|
35
|
+
create_tempfile(ext: format) do |output|
|
36
|
+
system "ffmpeg -y -i #{input.path} #{options} #{output.path}", exception: true
|
37
|
+
yield output
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_tempfile(ext: '')
|
42
|
+
ext = ".#{ext}" unless ext.blank? || ext.start_with?('.')
|
43
|
+
tempfile = Tempfile.new(['active_storage_transformer_', ext], binmode: true)
|
44
|
+
yield tempfile
|
45
|
+
ensure
|
46
|
+
tempfile.close!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Update in initializers
|
52
|
+
```ruby
|
53
|
+
# config/initializers/active_storage.rb
|
54
|
+
|
55
|
+
require 'active_storage/transformers/ffmpeg_transformer'
|
56
|
+
|
57
|
+
ActiveStorage.transformers << ActiveStorage::Transformers::FFMPEGTransformer
|
58
|
+
# => [ ActiveStorage::Transformers::ImageProcessingTransformer, FFMPEGTransformer ]
|
59
|
+
```
|
60
|
+
|
61
|
+
```html+erb
|
62
|
+
<%= audio_tag user.my_audio.variant(ffmpeg_opts: "-af silenceremove=stop_periods=-1:stop_duration=1:stop_threshold=-90dB", format: "mp3") %>
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jetthoughts/activestorage-autobots. 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.
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_storage"
|
4
|
+
require "active_support"
|
5
|
+
require "active_support/rails"
|
6
|
+
|
7
|
+
module ActiveStorage
|
8
|
+
mattr_accessor :transformers, default: [ActiveStorage::Transformers::ImageProcessingTransformer]
|
9
|
+
|
10
|
+
module Autobots
|
11
|
+
module RepresentableOverride
|
12
|
+
def variable?
|
13
|
+
ActiveStorage.transformers.any? { |klass| klass.accept?(self) }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
module VariantOverride
|
19
|
+
def process
|
20
|
+
blob.open do |input|
|
21
|
+
variation.transform(blob, input) do |output|
|
22
|
+
service.upload(key, output, content_type: content_type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module VariantWithRecordOverride
|
29
|
+
def transform_blob
|
30
|
+
blob.open do |input|
|
31
|
+
variation.transform(blob, input) do |output|
|
32
|
+
yield io: output, filename: "#{blob.filename.base}.#{variation.format.downcase}",
|
33
|
+
content_type: variation.content_type, service_name: blob.service.name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module VariationOverride
|
40
|
+
def transform(blob, file, &block)
|
41
|
+
ActiveSupport::Notifications.instrument("transform.active_storage") do
|
42
|
+
transformer(blob).transform(file, format: format, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def transformer(blob)
|
49
|
+
transformer_class(blob).new(transformations.except(:format))
|
50
|
+
end
|
51
|
+
|
52
|
+
def transformer_class(blob)
|
53
|
+
ActiveStorage.transformers.detect { |klass| klass.accept?(blob) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module ImageProcessingTransformerOverride
|
58
|
+
def accept?(blob)
|
59
|
+
ActiveStorage.variable_content_types.include?(blob.content_type)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module VideoAnalyzerOverride
|
64
|
+
def accept?(blob)
|
65
|
+
blob.video? || blob.content_type.end_with?("/mp4")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
ActiveSupport.on_load(:active_storage_blob) do
|
72
|
+
ActiveStorage::Blob::Representable.prepend(ActiveStorage::Autobots::RepresentableOverride)
|
73
|
+
ActiveStorage::Variant.send(:prepend, ActiveStorage::Autobots::VariantOverride)
|
74
|
+
ActiveStorage::VariantWithRecord.send(:prepend, ActiveStorage::Autobots::VariantWithRecordOverride)
|
75
|
+
ActiveStorage::Variation.send(:prepend, ActiveStorage::Autobots::VariationOverride)
|
76
|
+
ActiveStorage::Analyzer::VideoAnalyzer.singleton_class.send(:prepend, ActiveStorage::Autobots::VideoAnalyzerOverride)
|
77
|
+
ActiveStorage::Transformers::ImageProcessingTransformer.send(
|
78
|
+
:extend, ActiveStorage::Autobots::ImageProcessingTransformerOverride
|
79
|
+
)
|
80
|
+
end
|
81
|
+
|
82
|
+
require "active_storage/autobots/railtie" if defined?(Rails)
|
@@ -0,0 +1 @@
|
|
1
|
+
require "active_storage/autobots"
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activestorage-autobots
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Keen
|
8
|
+
- Sébastien Dubois
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 6.1.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 6.1.0
|
28
|
+
description: Enables ActiveStorage variants for other file types than images,such
|
29
|
+
as audio or video files, through an API for registering custom transformers similar
|
30
|
+
to previewers.
|
31
|
+
email:
|
32
|
+
- pftg@jetthoughts.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- MIT-LICENSE
|
38
|
+
- README.md
|
39
|
+
- lib/active_storage/autobots.rb
|
40
|
+
- lib/active_storage/autobots/railtie.rb
|
41
|
+
- lib/active_storage/autobots/version.rb
|
42
|
+
- lib/activestorage-autobots.rb
|
43
|
+
homepage: https://github.com/jetthoughts/activestorage-autobots
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.5.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.4.13
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Allow to add custom ActiveStorage transformers
|
66
|
+
test_files: []
|