nozzle 0.1.5 → 0.1.6

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
  SHA1:
3
- metadata.gz: 6acf45517971815d7ea04dab2de08a5aa4b4db51
4
- data.tar.gz: fd100f0680bf760bb5b9cd4c29604449d73ea87d
3
+ metadata.gz: 319a9dafc695844ec3d4e10b020f410cd3dea303
4
+ data.tar.gz: 362e39bcafed2d87371edc59403911a8b2a9f2e4
5
5
  SHA512:
6
- metadata.gz: 1cd93c3d5b4d613e7992b8836c9376885e8b19a8d31ca18e2c615427a12ae26aa4e35f5ccc92ee39e978393feb792f37dec8bc3140aa5616e347bba73e4acd02
7
- data.tar.gz: 080f3642843fe3bc88ae95b8346087052860075cdbba1e4128b4a95a87830bb11aa274b2a3706d24c9a640811daf50e6f416d037dea30a3abb639c58b4f5f62a
6
+ metadata.gz: 7311cea5c28ed74ebb42e29643578afcf3bdf1ecb65ac5c7e0280a26aa7442990b2a1e17c74de13369472bf6708d67db047a9447e91603fd0b33deac9b4eaf95
7
+ data.tar.gz: 0a139ca772a94e15205cbbc986cb9dcaebaea0fb4e6917594b2884fbc9180409b5327b6db70abbe8eca61143ba96cbc9d76e94213ee5927aa32e15de12de3a97
@@ -1,4 +1,5 @@
1
1
  require 'fileutils'
2
+ require 'tmpdir'
2
3
  require 'tempfile'
3
4
  require 'nozzle/adapter/outlet'
4
5
 
@@ -7,6 +8,9 @@ module Nozzle
7
8
  class Base
8
9
  include Nozzle::Adapter::Outlet
9
10
 
11
+ # minimum filename length for Nozzle to try and fit it to the filesystem.
12
+ MIN_PATH_MAX = 255
13
+
10
14
  # Initializes internal structure of new adapter.
11
15
  # outlet_class.new( instance, :avatar, 'image.jpg', :fake => true )
12
16
  def initialize( record, column, filename, options = {} )
@@ -168,7 +172,7 @@ module Nozzle
168
172
 
169
173
  @tempfile_path = File.expand_path(new_path)
170
174
  detect_properties
171
- @filename = prepare_filename(filename_candidate)
175
+ @filename = fit_to_filesystem(prepare_filename(filename_candidate))
172
176
  end
173
177
 
174
178
  # Stores temporary filename by the constructed path. Deletes old file.
@@ -203,7 +207,7 @@ module Nozzle
203
207
  { :url => url }
204
208
  end
205
209
 
206
- private
210
+ private
207
211
 
208
212
  # Returns a filename prepared for saving. Should be overridden.
209
213
  # instance.avatar.new_filename('image.jpg') # => '0006-avatary-image.jpg'
@@ -211,6 +215,31 @@ module Nozzle
211
215
  candidate
212
216
  end
213
217
 
218
+ # Returns a filename that will be acceptable for filesystem
219
+ # chopping it if nessesary.
220
+ def fit_to_filesystem(candidate)
221
+ return candidate if candidate.bytesize <= MIN_PATH_MAX
222
+ ext = File.extname(candidate)
223
+ base = File.basename(candidate, ext)
224
+ tmp = Dir.tmpdir
225
+ loop do
226
+ begin
227
+ candidate = base + ext
228
+ try_file = File.join(tmp, candidate)
229
+ FileUtils.touch try_file
230
+ FileUtils.rm try_file
231
+ return candidate
232
+ rescue Errno::ENAMETOOLONG
233
+ if base.length > 0
234
+ base.chop!
235
+ retry
236
+ else
237
+ raise
238
+ end
239
+ end
240
+ end
241
+ end
242
+
214
243
  # Tries to detect content_type and size of the file.
215
244
  # Note: this method calls `file` system command to detect file content type.
216
245
  def detect_properties
@@ -254,7 +283,6 @@ module Nozzle
254
283
  FileUtils.rmdir file_path
255
284
  end
256
285
  end
257
-
258
286
  end
259
287
  end
260
288
  end
@@ -1,3 +1,3 @@
1
1
  module Nozzle
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nozzle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Bochkariov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-13 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  requirements: []
102
102
  rubyforge_project:
103
- rubygems_version: 2.2.2
103
+ rubygems_version: 2.2.1
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: A gem to store and serve attachments for ruby rack applications