2019-08-14 09:48:44 +00:00
|
|
|
class LocalResource
|
2019-10-16 06:52:30 +00:00
|
|
|
attr_reader :uri
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
def initialize(uri, file_type = nil)
|
2020-01-07 17:29:17 +00:00
|
|
|
@uri = URI(uri)
|
2020-04-29 20:11:13 +00:00
|
|
|
@file_type = file_type
|
2019-10-16 06:52:30 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def file
|
|
|
|
@file ||= Tempfile.new(tmp_filename, tmp_folder, encoding: encoding).tap do |f|
|
|
|
|
io.rewind
|
|
|
|
f.write(io.read)
|
|
|
|
f.close
|
|
|
|
end
|
2020-01-07 17:29:17 +00:00
|
|
|
@file.open
|
2019-10-16 06:52:30 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def io
|
|
|
|
@io ||= uri.open
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def encoding
|
|
|
|
io.rewind
|
|
|
|
io.read.encoding
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
def find_file_type
|
|
|
|
@file_type ? @file_type.split('/').last : Pathname.new(uri.path).extname
|
|
|
|
end
|
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def tmp_filename
|
2020-04-29 20:11:13 +00:00
|
|
|
[Time.now.to_i.to_s, find_file_type].join('.')
|
2019-10-16 06:52:30 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-16 06:52:30 +00:00
|
|
|
def tmp_folder
|
|
|
|
Rails.root.join('tmp')
|
|
|
|
end
|
2020-09-21 13:12:30 +00:00
|
|
|
|
|
|
|
def filename
|
|
|
|
File.basename(uri.path)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|