convertr 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{convertr}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ilya Lityuga", "Alexander Svetkin"]
@@ -54,8 +54,9 @@ module Convertr
54
54
  @file = @task.file
55
55
  @logger.info("Started #{original_file}")
56
56
  begin
57
+ FileUtils.mkpath(indir)
58
+ FileUtils.cd(indir)
57
59
  fetch_file(@file.location, @file.filename)
58
- FileUtils.cd indir
59
60
  process_profile(profile_by_bitrate(@task.bitrate))
60
61
  if @task.bitrate == 600
61
62
  count = calc_thumbnails_count(@file.duration)
@@ -72,12 +73,20 @@ module Convertr
72
73
  'SUCCESS'
73
74
  end # }}}
74
75
 
75
- def fetch_file(source_url, filename) # скачивание файла по FTP {{{
76
- FileUtils.mkpath(indir)
77
- tmp_file = work_name + ".part"
76
+ # {{{
77
+ # Уже находясь в нужной директории, скачиваем файл по
78
+ # source_url через FTP во временный файл filename.part
79
+ # затем переименовываем его в filename
80
+ # Если файл уже скачан, завершаем работу
81
+ # Если находим временный файл - ждём, т.к., возможно
82
+ # другой процесс уже занят скачиванием файла, если
83
+ # ожидание продлится больше некоторого времени, удаляем
84
+ # временный файл и скачиваем самостоятельно
85
+ def fetch_file(source_url, filename)
86
+ tmp_file = filename + ".part"
78
87
  started_at = Time.now
79
88
  loop do
80
- return if ::File.exists? original_file
89
+ return if ::File.exists? filename
81
90
  if ::File.exists? tmp_file
82
91
  sleep(CONVERTOR_PAUSE_DELAY)
83
92
  if Time.now > started_at + CONVERTOR_MAX_FETCH_TIME
@@ -101,7 +110,7 @@ module Convertr
101
110
  end
102
111
  end
103
112
 
104
- FileUtils.move tmp_file, original_file
113
+ FileUtils.move tmp_file, filename
105
114
  end # }}}
106
115
 
107
116
  def profile_by_bitrate(bitrate) # bitrate -> profile {{{
@@ -191,9 +200,9 @@ module Convertr
191
200
  end # }}}
192
201
 
193
202
  # тут чёрт ногу сломит с этими картами директорий
194
- # /tmp/test.avi
203
+ # /tmp/test/test.avi
195
204
  def original_file
196
- ::File.join(@conf.tmp_dir, ::File.basename(@file.filename))
205
+ ::File.join(@conf.tmp_dir, ::File.filename(@file.filename), ::File.basename(@file.filename))
197
206
  end
198
207
 
199
208
  # /tmp/test
@@ -201,11 +210,6 @@ module Convertr
201
210
  ::File.join(@conf.tmp_dir, ::File.filename(@file.filename))
202
211
  end
203
212
 
204
- # /tmp/test/test
205
- def work_name
206
- ::File.join(@conf.tmp_dir, ::File.filename(@file.filename), ::File.filename(@file.filename))
207
- end
208
-
209
213
  # test + suffix
210
214
  def filename_with_suffix(suffix)
211
215
  ::File.filename(@file.filename) + suffix
@@ -87,40 +87,38 @@ class TestConvertor < Test::Unit::TestCase
87
87
  @convertor.task = Factory(:task, :file => @convertor.file, :crop => true, :deinterlace => true)
88
88
  end
89
89
  should "prepare valid shell command for thumbnail generation" do # {{{
90
- assert_equal "/usr/local/bin/tmaker -i /tmp/test.avi -c 20 -d -w 150 -h 112 -o \"/tmp/output/x/y/test-%d-2.jpg\" 200 400 600",
90
+ assert_equal "/usr/local/bin/tmaker -i /tmp/test/test.avi -c 20 -d -w 150 -h 112 -o \"/tmp/output/x/y/test-%d-2.jpg\" 200 400 600",
91
91
  @convertor.instance_eval { make_thumbnails_cmd(3, 200, 150, nil, 2) }
92
92
  end # }}}
93
93
  end
94
94
  #}}}
95
95
  context "when source file is available locally" do # {{{
96
96
  setup do
97
- @convertor.file = Factory(:file, :filename => 'test.avi', :location => 'ftp://example.com/test.avi')
98
- FileUtils.touch '/tmp/test.avi'
97
+ FileUtils.touch '/tmp/test/test.avi'
99
98
  Net::FTP.expects(:open).never
100
99
  end
101
100
  should "return immediately on attempt to fetch file" do
102
- assert_nil @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', 'test.avi') }
101
+ assert_nil @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', '/tmp/test/test.avi') }
103
102
  end
104
103
  end
105
104
  # }}}
106
105
  context "when source file is not available locally" do # {{{
107
106
  setup do
108
- @convertor.file = Factory(:file, :filename => 'test.avi', :location => 'ftp://example.com/test.avi')
109
- FileUtils.rm_rf '/tmp/test.avi'
107
+ FileUtils.rm_rf '/tmp/test/test.avi'
110
108
  end
111
109
 
112
110
  context "and ftp server works fine" do
113
111
  setup do
114
112
  ftp = mock()
115
113
  ftp.expects(:login).with('test','test')
116
- ftp.expects(:getbinaryfile).with('test.avi', '/tmp/test/test.part', 1024)
114
+ ftp.expects(:getbinaryfile).with('test.avi', '/tmp/test/test.avi.part', 1024)
117
115
  ftp.expects(:close)
118
116
  Net::FTP.expects(:new).with('example.com', nil, nil, nil).returns(ftp)
119
117
  end
120
118
  should "download file by FTP" do
121
- assert_equal 0, @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', 'test.avi') }
119
+ assert_equal 0, @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', '/tmp/test/test.avi') }
122
120
  assert !File.exists?('/tmp/test/test.avi.part')
123
- assert File.exists?('/tmp/test.avi')
121
+ assert File.exists?('/tmp/test/test.avi')
124
122
  end
125
123
  end
126
124
 
@@ -134,7 +132,7 @@ class TestConvertor < Test::Unit::TestCase
134
132
  end
135
133
  should "remove tmp file and raise error" do
136
134
  assert_raise Net::FTPError do
137
- @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', 'test.avi') }
135
+ @convertor.instance_eval { fetch_file('ftp://example.com/test.avi', '/tmp/test/test.avi') }
138
136
  end
139
137
  assert !File.exists?('/tmp/test/test.avi.part')
140
138
  end
@@ -157,7 +155,7 @@ class TestConvertor < Test::Unit::TestCase
157
155
  end
158
156
  end
159
157
  # }}}
160
- context "successfuly processing some task" do
158
+ context "successfuly processing some task" do # {{{
161
159
  setup do
162
160
  file = Factory(:file, :filename => 'test.avi', :location => 'ftp://example.com/test.avi', :duration => 600)
163
161
  @convertor.task = Factory(:task, :file => file, :bitrate => 600)
@@ -172,7 +170,8 @@ class TestConvertor < Test::Unit::TestCase
172
170
  assert_equal 'SUCCESS', @convertor.instance_eval { process_task }
173
171
  end
174
172
  end
175
- context "getting ftp error on processing some task" do
173
+ # }}}
174
+ context "getting ftp error on processing some task" do # {{{
176
175
  setup do
177
176
  file = Factory(:file, :filename => 'test.avi', :location => 'ftp://example.com/test.avi', :duration => 600)
178
177
  @convertor.task = Factory(:task, :file => file, :bitrate => 600)
@@ -184,7 +183,7 @@ class TestConvertor < Test::Unit::TestCase
184
183
  end
185
184
  should "return FAILURE" do
186
185
  assert_equal 'FAILURE', @convertor.instance_eval { process_task }
187
- end
186
+ end #}}}
188
187
  end
189
188
  end
190
189
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: convertr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ilya Lityuga