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
14
15
16
|
# File 'lib/porky_lib/file_service_helper.rb', line 14
def file?(file_or_content)
a_file?(file_or_content) || a_path?(file_or_content)
end
|
#generate_file_key(options) ⇒ Object
65
66
67
|
# File 'lib/porky_lib/file_service_helper.rb', line 65
def generate_file_key(options)
options.key?(:directory) ? "#{options[:directory]}/#{SecureRandom.uuid}" : SecureRandom.uuid
end
|
#max_file_size ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/porky_lib/file_service_helper.rb', line 56
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
52
53
54
|
# File 'lib/porky_lib/file_service_helper.rb', line 52
def max_size
PorkyLib::Config.config[:max_file_size]
end
|
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/porky_lib/file_service_helper.rb', line 37
def perform_upload(bucket_name, file_key, tempfile, options)
obj = s3.bucket(bucket_name).object(file_key)
upload_options = {
metadata: (options[:metadata] if options.key?(:metadata)),
storage_class: (options[:storage_class] if options.key?(:storage_class))
}.compact
obj.upload_file(tempfile.path, upload_options)
end
|
#read_file(file) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/porky_lib/file_service_helper.rb', line 27
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
48
49
50
|
# File 'lib/porky_lib/file_service_helper.rb', line 48
def s3
@s3 ||= Aws::S3::Resource.new
end
|
#write_tempfile(file_contents, file_key) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/porky_lib/file_service_helper.rb', line 19
def write_tempfile(file_contents, file_key)
tempfile = Tempfile.new(file_key)
tempfile << file_contents
tempfile.close
tempfile
end
|