Skip to content

Commit

Permalink
Change upload to no longer silently convert Strings to StringIO
Browse files Browse the repository at this point in the history
The interface for this will change to add a `stream` version, similar to #63.

To prepare for that we simplify the `upload` method
  • Loading branch information
Andrew Morton committed Apr 18, 2023
1 parent 90691a2 commit 9ab5e33
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
14 changes: 3 additions & 11 deletions lib/bucket_store/key_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,24 @@ def download
end

# Uploads the given file to the reference key location.
# If the File is a file like object then upload as is.
# If the file variable is actually a string then treat is as the file
# contents and upload as is.
#
# If the `key` already exists, its content will be replaced by the one in input.
#
# @param [String or File like object] file The file like object to upload or a String
# with the contents
# @param [String] content Contents of the file
# @return [String] The final `key` where the content has been uploaded
# @example Upload a file
# BucketStore.for("inmemory://bucket/file.xml").upload!("hello world")
def upload!(file)
def upload!(content)
raise ArgumentError, "Key cannot be empty" if key.empty?

if file.instance_of?(String)
file = StringIO.new(file)
end

BucketStore.logger.info(event: "key_storage.upload_started",
**log_context)

start = BucketStore::Timing.monotonic_now
result = adapter.upload!(
bucket: bucket,
key: key,
file: file,
file: StringIO.new(content),
)

BucketStore.logger.info(event: "key_storage.upload_finished",
Expand Down
27 changes: 0 additions & 27 deletions spec/bucket_store/key_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,6 @@ def build_for(key)
to raise_error(ArgumentError, /key cannot be empty/i)
end
end

context "when given a file like object" do
let(:file) { StringIO.new("hello") }

it "uploads the given file" do
expect(build_for("inmemory://bucket/file1").upload!(file)).
to eq("inmemory://bucket/file1")
end

it "logs the operation" do
expect(BucketStore.logger).to receive(:info).with(
hash_including(event: "key_storage.upload_started"),
)
expect(BucketStore.logger).to receive(:info).with(
hash_including(event: "key_storage.upload_finished"),
)

build_for("inmemory://bucket/file1").upload!(file)
end

context "when we try to upload a bucket" do
it "raises an error" do
expect { build_for("inmemory://bucket").upload!(file) }.
to raise_error(ArgumentError, /key cannot be empty/i)
end
end
end
end

describe "#delete!" do
Expand Down

0 comments on commit 9ab5e33

Please sign in to comment.