Stéphane Meny
3 years ago
committed by
GitHub
40 changed files with 243 additions and 135 deletions
@ -0,0 +1,24 @@ |
|||||||
|
class ArchiveStorageService < StorageService |
||||||
|
MAX_SIZE = 50 * (1024**2) # 50MiB |
||||||
|
|
||||||
|
def initialize(archive_io) |
||||||
|
super() |
||||||
|
@archive = Zip::File.open_buffer(archive_io) |
||||||
|
end |
||||||
|
|
||||||
|
def list_files(folder) |
||||||
|
@archive.glob(File.join(folder, "*.*")) |
||||||
|
.map(&:name) |
||||||
|
end |
||||||
|
|
||||||
|
def folder_present?(folder) |
||||||
|
!list_files(folder).empty? |
||||||
|
end |
||||||
|
|
||||||
|
def get_file_io(file_name) |
||||||
|
entry = @archive.get_entry(file_name) |
||||||
|
raise "File too large to be extracted" if entry.size > MAX_SIZE |
||||||
|
|
||||||
|
entry.get_input_stream |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,78 @@ |
|||||||
|
class S3StorageService < StorageService |
||||||
|
attr_reader :configuration |
||||||
|
|
||||||
|
def initialize(paas_config_service, paas_instance_name) |
||||||
|
super() |
||||||
|
@paas_config_service = paas_config_service |
||||||
|
@paas_instance_name = (paas_instance_name || "").to_sym |
||||||
|
@configuration = create_configuration |
||||||
|
@client = create_client |
||||||
|
end |
||||||
|
|
||||||
|
def list_files(folder) |
||||||
|
@client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder) |
||||||
|
.flat_map { |response| response.contents.map(&:key) } |
||||||
|
end |
||||||
|
|
||||||
|
def folder_present?(folder) |
||||||
|
response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1) |
||||||
|
response.key_count == 1 |
||||||
|
end |
||||||
|
|
||||||
|
def get_file_io(file_name) |
||||||
|
@client.get_object(bucket: @configuration.bucket_name, key: file_name) |
||||||
|
.body |
||||||
|
end |
||||||
|
|
||||||
|
def write_file(file_name, data) |
||||||
|
@client.put_object( |
||||||
|
body: data, |
||||||
|
bucket: @configuration.bucket_name, |
||||||
|
key: file_name, |
||||||
|
) |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def create_configuration |
||||||
|
unless @paas_config_service.config_present? |
||||||
|
raise "No PaaS configuration present" |
||||||
|
end |
||||||
|
unless @paas_config_service.s3_buckets.key?(@paas_instance_name) |
||||||
|
raise "#{@paas_instance_name} instance name could not be found" |
||||||
|
end |
||||||
|
|
||||||
|
bucket_config = @paas_config_service.s3_buckets[@paas_instance_name] |
||||||
|
StorageConfiguration.new(bucket_config[:credentials]) |
||||||
|
end |
||||||
|
|
||||||
|
def create_client |
||||||
|
credentials = |
||||||
|
Aws::Credentials.new( |
||||||
|
@configuration.access_key_id, |
||||||
|
@configuration.secret_access_key, |
||||||
|
) |
||||||
|
Aws::S3::Client.new( |
||||||
|
region: @configuration.region, |
||||||
|
credentials:, |
||||||
|
) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
class StorageConfiguration |
||||||
|
attr_reader :access_key_id, :secret_access_key, :bucket_name, :region |
||||||
|
|
||||||
|
def initialize(credentials) |
||||||
|
@access_key_id = credentials[:aws_access_key_id] |
||||||
|
@secret_access_key = credentials[:aws_secret_access_key] |
||||||
|
@bucket_name = credentials[:bucket_name] |
||||||
|
@region = credentials[:aws_region] |
||||||
|
end |
||||||
|
|
||||||
|
def ==(other) |
||||||
|
@access_key_id == other.access_key_id && |
||||||
|
@secret_access_key == other.secret_access_key && |
||||||
|
@bucket_name == other.bucket_name && |
||||||
|
@region == other.region |
||||||
|
end |
||||||
|
end |
@ -1,77 +1,17 @@ |
|||||||
class StorageService |
class StorageService |
||||||
attr_reader :configuration |
def list_files(_folder) |
||||||
|
raise NotImplementedError |
||||||
def initialize(paas_config_service, paas_instance_name) |
|
||||||
@paas_config_service = paas_config_service |
|
||||||
@paas_instance_name = (paas_instance_name || "").to_sym |
|
||||||
@configuration = create_configuration |
|
||||||
@client = create_client |
|
||||||
end |
|
||||||
|
|
||||||
def list_files(folder) |
|
||||||
@client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder) |
|
||||||
.flat_map { |response| response.contents.map(&:key) } |
|
||||||
end |
|
||||||
|
|
||||||
def folder_present?(folder) |
|
||||||
response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1) |
|
||||||
response.key_count == 1 |
|
||||||
end |
|
||||||
|
|
||||||
def get_file_io(file_name) |
|
||||||
@client.get_object(bucket: @configuration.bucket_name, key: file_name) |
|
||||||
.body |
|
||||||
end |
end |
||||||
|
|
||||||
def write_file(file_name, data) |
def folder_present?(_folder) |
||||||
@client.put_object( |
raise NotImplementedError |
||||||
body: data, |
|
||||||
bucket: @configuration.bucket_name, |
|
||||||
key: file_name, |
|
||||||
) |
|
||||||
end |
end |
||||||
|
|
||||||
private |
def get_file_io(_file_name) |
||||||
|
raise NotImplementedError |
||||||
def create_configuration |
|
||||||
unless @paas_config_service.config_present? |
|
||||||
raise "No PaaS configuration present" |
|
||||||
end |
|
||||||
unless @paas_config_service.s3_buckets.key?(@paas_instance_name) |
|
||||||
raise "#{@paas_instance_name} instance name could not be found" |
|
||||||
end |
|
||||||
|
|
||||||
bucket_config = @paas_config_service.s3_buckets[@paas_instance_name] |
|
||||||
StorageConfiguration.new(bucket_config[:credentials]) |
|
||||||
end |
|
||||||
|
|
||||||
def create_client |
|
||||||
credentials = |
|
||||||
Aws::Credentials.new( |
|
||||||
@configuration.access_key_id, |
|
||||||
@configuration.secret_access_key, |
|
||||||
) |
|
||||||
Aws::S3::Client.new( |
|
||||||
region: @configuration.region, |
|
||||||
credentials:, |
|
||||||
) |
|
||||||
end |
|
||||||
end |
|
||||||
|
|
||||||
class StorageConfiguration |
|
||||||
attr_reader :access_key_id, :secret_access_key, :bucket_name, :region |
|
||||||
|
|
||||||
def initialize(credentials) |
|
||||||
@access_key_id = credentials[:aws_access_key_id] |
|
||||||
@secret_access_key = credentials[:aws_secret_access_key] |
|
||||||
@bucket_name = credentials[:bucket_name] |
|
||||||
@region = credentials[:aws_region] |
|
||||||
end |
end |
||||||
|
|
||||||
def ==(other) |
def write_file(_file_name, _data) |
||||||
@access_key_id == other.access_key_id && |
raise NotImplementedError |
||||||
@secret_access_key == other.secret_access_key && |
|
||||||
@bucket_name == other.bucket_name && |
|
||||||
@region == other.region |
|
||||||
end |
end |
||||||
end |
end |
||||||
|
@ -0,0 +1,65 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe ArchiveStorageService do |
||||||
|
subject(:archive_service) { described_class.new(archive_content) } |
||||||
|
|
||||||
|
let(:compressed_folder) { "my_directory" } |
||||||
|
let(:compressed_filename) { "hello.txt" } |
||||||
|
let(:compressed_filepath) { File.join(compressed_folder, compressed_filename) } |
||||||
|
let(:compressed_file) do |
||||||
|
file = Tempfile.new |
||||||
|
file << "Hello World\n" |
||||||
|
file.rewind |
||||||
|
file |
||||||
|
end |
||||||
|
let(:archive_content) do |
||||||
|
zip_file = Zip::File.open_buffer(StringIO.new) |
||||||
|
zip_file.mkdir(compressed_folder) |
||||||
|
zip_file.add(compressed_filepath, compressed_file) |
||||||
|
zip_file.write_buffer |
||||||
|
end |
||||||
|
|
||||||
|
describe "#list_files" do |
||||||
|
it "returns the list of files present in an existing folder" do |
||||||
|
file_list = archive_service.list_files(compressed_folder) |
||||||
|
expect(file_list).to contain_exactly(compressed_filepath) |
||||||
|
end |
||||||
|
|
||||||
|
it "returns an empty file list for an unknown folder" do |
||||||
|
file_list = archive_service.list_files("random_folder") |
||||||
|
expect(file_list).to be_empty |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#folder_present?" do |
||||||
|
it "returns true if a folder in the archive exists" do |
||||||
|
presence = archive_service.folder_present?(compressed_folder) |
||||||
|
expect(presence).to be_truthy |
||||||
|
end |
||||||
|
|
||||||
|
it "returns false if a folder in the archive does not exist" do |
||||||
|
presence = archive_service.folder_present?("random_folder") |
||||||
|
expect(presence).to be_falsey |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe "#get_file_io" do |
||||||
|
it "returns the file content if a file exists" do |
||||||
|
content = archive_service.get_file_io(compressed_filepath) |
||||||
|
expect(content.read).to eq(compressed_file.read) |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error if the file exists but is too large" do |
||||||
|
archive = archive_service.instance_variable_get(:@archive) |
||||||
|
allow(archive).to receive(:get_entry).and_return(Zip::Entry.new(nil, "", nil, nil, nil, nil, nil, 100_000_000, nil)) |
||||||
|
|
||||||
|
expect { archive_service.get_file_io(compressed_filepath) } |
||||||
|
.to raise_error(RuntimeError, "File too large to be extracted") |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error if a file does not exist" do |
||||||
|
expect { archive_service.get_file_io("random.zzz") } |
||||||
|
.to raise_error(Errno::ENOENT) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -1,6 +1,6 @@ |
|||||||
require "rails_helper" |
require "rails_helper" |
||||||
|
|
||||||
RSpec.describe StorageService do |
RSpec.describe S3StorageService do |
||||||
let(:instance_name) { "instance_1" } |
let(:instance_name) { "instance_1" } |
||||||
let(:bucket_name) { "bucket_1" } |
let(:bucket_name) { "bucket_1" } |
||||||
let(:vcap_services) do |
let(:vcap_services) do |
Loading…
Reference in new issue