Module: PorkyLib::FileServiceHelper
Defined Under Namespace
Classes: FileServiceError
Instance Method Summary
collapse
Instance Method Details
#data_size_invalid?(data) ⇒ Boolean
10
11
12
|
# File 'lib/porky_lib/file_service_helper.rb', line 10
def data_size_invalid?(data)
data.bytesize > max_size
end
|
#file?(file_or_content) ⇒ Boolean
23
24
25
|
# File 'lib/porky_lib/file_service_helper.rb', line 23
def file?(file_or_content)
a_file?(file_or_content) || a_path?(file_or_content)
end
|
#file_data(file_or_content) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/porky_lib/file_service_helper.rb', line 14
def file_data(file_or_content)
if file?(file_or_content)
read_file(file_or_content)
else
file_or_content
end
end
|
#generate_file_key(options) ⇒ Object
72
73
74
|
# File 'lib/porky_lib/file_service_helper.rb', line 72
def generate_file_key(options)
options.key?(:directory) ? "#{options[:directory]}/#{SecureRandom.uuid}" : SecureRandom.uuid
end
|
#max_file_size ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/porky_lib/file_service_helper.rb', line 63
def max_file_size
{
B: 1024,
KB: 1024 * 1024,
MB: 1024 * 1024 * 1024,
GB: 1024 * 1024 * 1024 * 1024
}.each_pair { |symbol, bytes| return "#{(max_size.to_f / (bytes / 1024)).round(2)}#{symbol}" if max_size < bytes }
end
|
#max_size ⇒ Object
59
60
61
|
# File 'lib/porky_lib/file_service_helper.rb', line 59
def max_size
PorkyLib::Config.config[:max_file_size]
end
|
46
47
48
49
50
51
52
53
|
# File 'lib/porky_lib/file_service_helper.rb', line 46
def perform_upload(bucket_name, file_key, tempfile, options)
obj = s3.bucket(bucket_name).object(file_key)
if options.key?(:metadata)
obj.upload_file(tempfile.path, metadata: options[:metadata])
else
obj.upload_file(tempfile.path)
end
end
|
#read_file(file) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'lib/porky_lib/file_service_helper.rb', line 36
def read_file(file)
raise FileServiceError, 'file cannot be nil' if file.nil?
return file if !a_file?(file) && contain_null_byte?(file)
raise FileServiceError, 'The specified file does not exist' unless File.file?(file)
File.read(file)
rescue Errno::EACCES
raise FileServiceError, 'The specified file cannot be read, no permissions'
end
|
#s3 ⇒ Object
55
56
57
|
# File 'lib/porky_lib/file_service_helper.rb', line 55
def s3
@s3 ||= Aws::S3::Resource.new
end
|
#write_tempfile(file_contents, file_key) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/porky_lib/file_service_helper.rb', line 28
def write_tempfile(file_contents, file_key)
tempfile = Tempfile.new(file_key)
tempfile << file_contents
tempfile.close
tempfile
end
|