Skip to content

Commit 89dfbe8

Browse files
committed
Fixing bugs
1 parent 1d21b92 commit 89dfbe8

File tree

7 files changed

+70
-35
lines changed

7 files changed

+70
-35
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Paperclip
22
module Av
33
module Transcoder
4-
VERSION = "0.1.0"
4+
VERSION = "0.1.2"
55
end
66
end
77
end

lib/paperclip/paperclip_processors/transcoder.rb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize file, options = {}, attachment = nil
2020
@geometry = options[:geometry]
2121
unless @geometry.nil?
2222
modifier = @geometry[0]
23-
@geometry[0] = '' if ['#', '<', '>'].includes? modifier
23+
@geometry[0] = '' if ['#', '<', '>'].include? modifier
2424
@width, @height = @geometry.split('x')
2525
@keep_aspect = @width[0] == '!' || @height[0] == '!'
2626
@pad_only = @keep_aspect && modifier == '#'
@@ -41,28 +41,33 @@ def make
4141
::Av.cli.add_source @file
4242
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
4343
dst.binmode
44-
45-
cli = ::Av.cli(quite: true)
46-
cli.add_source(@file.path)
47-
cli.add_destination(dst.path)
48-
cli.reset_input_filters
49-
if @convert_options[:input]
50-
@convert_options[:input].each do |h|
51-
cli.add_input_param h
52-
end
53-
end
54-
if @convert_options[:output]
55-
@convert_options[:output].each do |h|
56-
cli.add_output_param h
44+
45+
if @meta
46+
log "Transocding supported file #{@file.path}"
47+
cli = ::Av.cli(quite: true)
48+
cli.add_source(@file.path)
49+
cli.add_destination(dst.path)
50+
cli.reset_input_filters
51+
if @convert_options.present?
52+
if @convert_options[:input]
53+
@convert_options[:input].each do |h|
54+
cli.add_input_param h
55+
end
56+
end
57+
if @convert_options[:output]
58+
@convert_options[:output].each do |h|
59+
cli.add_output_param h
60+
end
61+
end
5762
end
58-
end
5963

60-
begin
61-
cli.run
62-
log "Successfully transcoded #{@base} to #{dst}"
63-
return dst # Things went fine, pass the file to paperclip for saving
64-
rescue Cocaine::ExitStatusError => e
65-
raise Paperclip::Error, "error while transcoding #{@basename}: #{e}" if @whiny
64+
begin
65+
cli.run
66+
log "Successfully transcoded #{@base} to #{dst}"
67+
return dst
68+
rescue Cocaine::ExitStatusError => e
69+
raise Paperclip::Error, "error while transcoding #{@basename}: #{e}" if @whiny
70+
end
6671
end
6772
# If the file is not supported, just return it
6873
@file

paperclip-av-transcoder.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "sqlite3"
2626

2727
spec.add_dependency "paperclip", ">=2.5.2"
28-
spec.add_dependency "av", ">= 0.0.1"
28+
spec.add_dependency "av", ">= 0.4.0"
2929
end

spec/schema.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
ActiveRecord::Schema.define version: 0 do
22
create_table "documents", force: true do |t|
33
t.string :owner
4-
t.string :original_file_name
5-
t.string :original_content_type
6-
t.integer :original_updated_at
7-
t.integer :original_file_size
4+
t.string :video_file_name
5+
t.string :video_content_type
6+
t.integer :video_updated_at
7+
t.integer :video_file_size
8+
t.string :image_file_name
9+
t.string :image_content_type
10+
t.integer :image_updated_at
11+
t.integer :image_file_size
812
end
913
end

spec/spec_helper.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Connect to sqlite
1212
ActiveRecord::Base.establish_connection("adapter" => "sqlite3", "database" => ":memory:")
1313

14-
ActiveRecord::Base.logger = Logger.new(nil)
14+
ActiveRecord::Base.logger = Logger.new(STDOUT)
1515
load(File.join(File.dirname(__FILE__), 'schema.rb'))
1616

1717
Paperclip::Railtie.insert
@@ -22,7 +22,7 @@
2222
end
2323

2424
class Document < ActiveRecord::Base
25-
has_attached_file :original,
25+
has_attached_file :video,
2626
storage: :filesystem,
2727
path: "./spec/tmp/:id.:extension",
2828
url: "/spec/tmp/:id.:extension",
@@ -40,5 +40,17 @@ class Document < ActiveRecord::Base
4040
}
4141
},
4242
processors: [:transcoder]
43-
do_not_validate_attachment_file_type :original
43+
44+
has_attached_file :image,
45+
storage: :filesystem,
46+
path: "./spec/tmp/:id.:extension",
47+
url: "/spec/tmp/:id.:extension",
48+
whiny: true,
49+
styles: {
50+
small: "100x100"
51+
},
52+
processors: [:transcoder, :thumbnail]
53+
54+
do_not_validate_attachment_file_type :video
55+
do_not_validate_attachment_file_type :image
4456
end

spec/support/assets/image.png

95 Bytes
Loading

spec/transcoder/transcoder_spec.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
require 'spec_helper'
22

33
describe Paperclip::Transcoder do
4-
let(:subject) { Paperclip::Transcoder.new(source) }
5-
let(:document) { Document.create(original: source) }
6-
let(:source) { File.new(Dir.pwd + '/spec/support/assets/sample.mp4') }
4+
let(:supported) { File.new(Dir.pwd + '/spec/support/assets/sample.mp4') }
5+
let(:unsupported) { File.new(File.expand_path('spec/support/assets/image.png')) }
6+
77
let(:destination) { Pathname.new("#{Dir.tmpdir}/transcoder/") }
88

9-
describe ".transcode" do
10-
it { expect(File.exists?(document.original.path(:small))).to eq true }
9+
describe "supported formats" do
10+
let(:subject) { Paperclip::Chainer.new(supported) }
11+
let(:document) { Document.create(video: Rack::Test::UploadedFile.new(supported, 'video/mp4')) }
12+
13+
describe ".transcode" do
14+
it { expect(File.exists?(document.video.path(:small))).to eq true }
15+
end
16+
end
17+
18+
describe "unsupported formats" do
19+
let(:subject) { Paperclip::Chainer.new(unsupported) }
20+
let(:document) { Document.create(image: Rack::Test::UploadedFile.new(unsupported, 'image/png')) }
21+
describe ".transcode" do
22+
it { expect(File.exists?(document.image.path(:small))).to eq true }
23+
end
1124
end
25+
1226
end

0 commit comments

Comments
 (0)