From 8c96423c39d12abe696430f89a9f5849dd9a9158 Mon Sep 17 00:00:00 2001 From: Joseph Southan Date: Mon, 8 Feb 2021 15:28:42 +0000 Subject: [PATCH] Introduce #delete --- README.md | 8 +++++++- lib/file_storage.rb | 16 ++++++++++++++++ lib/file_storage/disk.rb | 6 ++++++ lib/file_storage/gcs.rb | 6 ++++++ lib/file_storage/in_memory.rb | 6 ++++++ spec/file_storage/disk_spec.rb | 11 +++++++++++ spec/file_storage/in_memory_spec.rb | 10 ++++++++++ spec/file_storage_spec.rb | 10 ++++++++++ 8 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 574b96f..5d1ef71 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ between different storage options. Callers don't need to worry about the specifi of where and how a file is stored and retrieved as long as the given key is valid. Keys within the `FileStorage` are URI strings that can universally locate an object -in the given provider. A valid key example would be +in the given provider. A valid key example would be `gs://gc-prd-nx-incoming/file/path.json`. ## Usage @@ -132,3 +132,9 @@ FileStorage.for("inmemory://bucket/path/file.xml").download FileStorage.for("inmemory://bucket/path/").list => ["inmemory://bucket/path/file.xml"] ``` + +### Delete a file +```ruby +FileStorage.for("inmemory://bucket/path/").delete! +=> true +``` diff --git a/lib/file_storage.rb b/lib/file_storage.rb index 4208a05..85dab7f 100644 --- a/lib/file_storage.rb +++ b/lib/file_storage.rb @@ -162,6 +162,22 @@ def list result[:keys].map { |key| "#{adapter_type}://#{result[:bucket]}/#{key}" } end + # Deletes a given key + # @return [true] + def delete! + info("Deleting file", + event: "delete_started") + + start = FileStorage::Timing.monotonic_now + adapter.delete!(bucket: bucket, key: key) + + info("File deleted", + event: "delete_finished", + duration: FileStorage::Timing.monotonic_now - start) + + true + end + private attr_reader :adapter diff --git a/lib/file_storage/disk.rb b/lib/file_storage/disk.rb index 562c4be..f976914 100644 --- a/lib/file_storage/disk.rb +++ b/lib/file_storage/disk.rb @@ -47,6 +47,12 @@ def list(bucket:, key:) } end + def delete!(bucket:, key:) + File.unlink(key_path(bucket, key)) + + true + end + private attr_reader :base_dir diff --git a/lib/file_storage/gcs.rb b/lib/file_storage/gcs.rb index 501f3fc..6d18361 100644 --- a/lib/file_storage/gcs.rb +++ b/lib/file_storage/gcs.rb @@ -50,6 +50,12 @@ def list(bucket:, key:) } end + def delete!(bucket:, key:) + get_bucket(bucket).file(key).delete + + true + end + private attr_reader :storage diff --git a/lib/file_storage/in_memory.rb b/lib/file_storage/in_memory.rb index 56f1887..47fb9fe 100644 --- a/lib/file_storage/in_memory.rb +++ b/lib/file_storage/in_memory.rb @@ -48,5 +48,11 @@ def list(bucket:, key:) keys: matching_keys, } end + + def delete!(bucket:, key:) + @buckets[bucket].delete(key) + + true + end end end diff --git a/spec/file_storage/disk_spec.rb b/spec/file_storage/disk_spec.rb index c89214d..e3663e0 100644 --- a/spec/file_storage/disk_spec.rb +++ b/spec/file_storage/disk_spec.rb @@ -130,4 +130,15 @@ end end end + + describe "#delete!" do + before { instance.upload!(bucket: bucket, key: "hello", content: "world") } + + it "deletes the given content" do + expect(instance.delete!(bucket: bucket, key: "hello")).to eq(true) + + expect { instance.download(bucket: bucket, key: "hello").download }. + to raise_error(Errno::ENOENT, /No such file or directory/) + end + end end diff --git a/spec/file_storage/in_memory_spec.rb b/spec/file_storage/in_memory_spec.rb index dc79e56..aa5feba 100644 --- a/spec/file_storage/in_memory_spec.rb +++ b/spec/file_storage/in_memory_spec.rb @@ -132,4 +132,14 @@ end end end + + describe "#delete!" do + before { instance.upload!(bucket: bucket, key: "hello", content: "world") } + + it "deletes the given content" do + expect(instance.delete!(bucket: bucket, key: "hello")).to eq(true) + + expect { instance.download(bucket: bucket, key: "hello").download }.to raise_error(KeyError) + end + end end diff --git a/spec/file_storage_spec.rb b/spec/file_storage_spec.rb index ad392c5..b19d31d 100644 --- a/spec/file_storage_spec.rb +++ b/spec/file_storage_spec.rb @@ -143,5 +143,15 @@ end end end + + describe "delete!" do + before do + described_class.for("inmemory://bucket/file1").upload!("content1") + end + + it "downloads the given file" do + expect(described_class.for("inmemory://bucket/file1").delete!).to eq(true) + end + end end end