Class: PorkyLib::Unencrypted::FileService
- Inherits:
-
Object
- Object
- PorkyLib::Unencrypted::FileService
show all
- Extended by:
- Gem::Deprecate
- Includes:
- FileServiceHelper, Singleton
- Defined in:
- lib/porky_lib/unencrypted/file_service.rb
Defined Under Namespace
Classes: FileServiceError, FileSizeTooLargeError
Instance Method Summary
collapse
#data_size_invalid?, #file?, #generate_file_key, #max_file_size, #max_size, #perform_upload, #read_file, #s3, #write_tempfile
Instance Method Details
#read(bucket_name, file_key, options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/porky_lib/unencrypted/file_service.rb', line 14
def read(bucket_name, file_key, options = {})
tempfile = Tempfile.new
begin
object = s3.bucket(bucket_name).object(file_key)
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if object.content_length > max_size
object.download_file(tempfile.path, options)
rescue Aws::Errors::ServiceError, Seahorse::Client::NetworkingError => e
raise FileServiceError, "Attempt to download a file from S3 failed.\n#{e.message}"
end
tempfile.read
end
|
#write(file, bucket_name, options = {}) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/porky_lib/unencrypted/file_service.rb', line 29
def write(file, bucket_name, options = {})
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name)
if file?(file)
write_file(file, bucket_name, options)
else
write_data(file, bucket_name, options)
end
end
|
#write_data(data, bucket_name, options = {}) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/porky_lib/unencrypted/file_service.rb', line 46
def write_data(data, bucket_name, options = {})
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name)
raise FileSizeTooLargeError, "Data size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
file_key = if options.key?(:file_name)
options[:file_name]
else
generate_file_key(options)
end
tempfile = write_tempfile(data, file_key)
begin
perform_upload(bucket_name, file_key, tempfile, options)
rescue Aws::Errors::ServiceError, Seahorse::Client::NetworkingError => e
raise FileServiceError, "Attempt to upload a file to S3 failed.\n#{e.message}"
end
file_key
end
|
#write_file(file, bucket_name, options = {}) ⇒ Object
40
41
42
43
44
|
# File 'lib/porky_lib/unencrypted/file_service.rb', line 40
def write_file(file, bucket_name, options = {})
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name)
write_data(read_file(file), bucket_name, options)
end
|