Class: PorkyLib::FileService

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Includes:
FileServiceHelper, Singleton
Defined in:
lib/porky_lib/file_service.rb

Defined Under Namespace

Classes: FileServiceError, FileSizeTooLargeError

Instance Method Summary collapse

Methods included from FileServiceHelper

#data_size_invalid?, #file?, #generate_file_key, #max_file_size, #max_size, #perform_upload, #read_file, #s3, #write_tempfile

Instance Method Details

#copy_file(source_bucket, destination_bucket, file_key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/porky_lib/file_service.rb', line 23

def copy_file(source_bucket, destination_bucket, file_key)
  s3_client.copy_object(
    bucket: destination_bucket,
    copy_source: "#{source_bucket}/#{file_key}",
    key: file_key
  )

  file_key
rescue Aws::Errors::ServiceError => e
  raise FileServiceError, "File move #{file_key} from S3 bucket #{source_bucket} to #{destination_bucket} failed: #{e.message}"
end

#delete_file(bucket_name, file_key) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/porky_lib/file_service.rb', line 35

def delete_file(bucket_name, file_key)
  s3_client.delete_object(
    bucket: bucket_name,
    key: file_key
  )
rescue Aws::Errors::ServiceError => e
  raise FileServiceError, "File delete of #{file_key} from S3 bucket #{bucket_name} failed: #{e.message}"
end

#overwrite_file(data, file_key, bucket_name, key_id, options = {}) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/porky_lib/file_service.rb', line 98

def overwrite_file(data, file_key, bucket_name, key_id, options = {})
  raise FileServiceError, 'Invalid input. One or more input values is nil' if overwrite_input_invalid?(data, file_key, bucket_name, key_id)
  raise FileServiceError, 'Invalid input. file_key cannot be nil if overwriting an existing file' if file_key.nil?

  raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)

  tempfile = encrypt_file_contents(data, key_id, file_key, options)

  begin
    perform_upload(bucket_name, file_key, tempfile, options)
  rescue Aws::Errors::ServiceError => e
    raise FileServiceError, "Attempt to upload a file to S3 failed.\n#{e.message}"
  end

  # Remove tempfile from disk
  tempfile.unlink
end

#presigned_get_url(bucket_name, file_key) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/porky_lib/file_service.rb', line 128

def presigned_get_url(bucket_name, file_key)
  obj = s3.bucket(bucket_name).object(file_key)

  obj.presigned_url(:get,
                    expires_in: presign_url_expires_in)
rescue Aws::Errors::ServiceError => e
  raise FileServiceError, "PresignedGetUrl for #{file_key} from S3 bucket #{bucket_name} failed: #{e.message}"
end

#presigned_post_url(bucket_name, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/porky_lib/file_service.rb', line 116

def presigned_post_url(bucket_name, options = {})
  file_name = options[:file_name] || SecureRandom.uuid
  obj = s3.bucket(bucket_name).object(file_name)

  presigned_url = obj.presigned_url(:put,
                                    expires_in: presign_url_expires_in,
                                    metadata: options[:metadata])
  [presigned_url, file_name]
rescue Aws::Errors::ServiceError => e
  raise FileServiceError, "PresignedPostUrl for #{file_name} from S3 bucket #{bucket_name} failed: #{e.message}"
end

#read(bucket_name, file_key, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/porky_lib/file_service.rb', line 44

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 => e
    raise FileServiceError, "Attempt to download a file from S3 failed.\n#{e.message}"
  end

  decrypt_file_contents(tempfile)
end

#read_file_info(bucket_name, file_key) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/porky_lib/file_service.rb', line 14

def read_file_info(bucket_name, file_key)
  s3_client.head_object(
    bucket: bucket_name,
    key: file_key
  ).to_h
rescue Aws::Errors::ServiceError => e
  raise FileServiceError, "File info read for #{file_key} in S3 bucket #{bucket_name} failed: #{e.message}"
end

#write(file, bucket_name, key_id, options = {}) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
# File 'lib/porky_lib/file_service.rb', line 59

def write(file, bucket_name, key_id, options = {})
  raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name, key_id)

  if file?(file)
    write_file(file, bucket_name, key_id, options)
  else
    write_data(file, bucket_name, key_id, options)
  end
end

#write_data(data, bucket_name, key_id, options = {}) ⇒ Object

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/porky_lib/file_service.rb', line 76

def write_data(data, bucket_name, key_id, options = {})
  raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name, key_id)
  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 = encrypt_file_contents(data, key_id, file_key, options)

  begin
    perform_upload(bucket_name, file_key, tempfile, options)
  rescue Aws::Errors::ServiceError => e
    raise FileServiceError, "Attempt to upload a file to S3 failed.\n#{e.message}"
  end

  # Remove tempfile from disk
  tempfile.unlink
  file_key
end

#write_file(file, bucket_name, key_id, options = {}) ⇒ Object

Raises:



70
71
72
73
74
# File 'lib/porky_lib/file_service.rb', line 70

def write_file(file, bucket_name, key_id, options = {})
  raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name, key_id)

  write_data(read_file(file), bucket_name, key_id, options)
end