lib/fontist/utils/cache.rb in fontist-1.20.0 vs lib/fontist/utils/cache.rb in fontist-1.21.1
- old
+ new
@@ -1,8 +1,10 @@
module Fontist
module Utils
class Cache
+ MAX_FILENAME_SIZE = 255
+
include Locking
def self.lock_path(path)
"#{path}.lock"
end
@@ -108,16 +110,30 @@
dir = Dir.mktmpdir(nil, Fontist.downloads_path)
File.join(dir, filename(source))
end
def filename(source)
+ filename = response_to_filename(source)
+ format_filename(filename)
+ end
+
+ def response_to_filename(source)
if File.extname(source.original_filename).empty? && source.content_type
require "mime/types"
ext = MIME::Types[source.content_type].first&.preferred_extension
return "#{source.original_filename}.#{ext}" if ext
end
source.original_filename
+ end
+
+ def format_filename(filename)
+ return filename unless filename.length > MAX_FILENAME_SIZE
+
+ ext = File.extname(filename)
+ target_size = MAX_FILENAME_SIZE - ext.length
+ cut_filename = filename.slice(0, target_size)
+ "#{cut_filename}#{ext}"
end
def move(source_file, target_path)
# Windows requires file descriptors to be closed before files are moved
source_file.close