diff --git a/app/actions/api/firmware/patch.rb b/app/actions/api/firmware/patch.rb index 0dd8e41f..bf25bebb 100644 --- a/app/actions/api/firmware/patch.rb +++ b/app/actions/api/firmware/patch.rb @@ -54,7 +54,7 @@ module Terminus # :reek:FeatureEnvy def replace record, content, response Pathname.mktmpdir do |root| - root.join("#{record.version}.bin").write(content).open { record.replace it } + root.join("#{record.version}.bin").write(content).open { record.upload it } end update = repository.update record.id, attachment_data: record.attachment_attributes diff --git a/app/actions/firmware/update.rb b/app/actions/firmware/update.rb index 1bb84965..56094e71 100644 --- a/app/actions/firmware/update.rb +++ b/app/actions/firmware/update.rb @@ -47,7 +47,7 @@ module Terminus def attach record, attachment return unless attachment - record.replace attachment[:tempfile], metadata: {"filename" => "#{record.version}.bin"} + record.upload attachment[:tempfile], metadata: {"filename" => "#{record.version}.bin"} repository.update record.id, attachment_data: record.attachment_attributes end diff --git a/app/actions/screens/update.rb b/app/actions/screens/update.rb index ed1fc43e..fe490c41 100644 --- a/app/actions/screens/update.rb +++ b/app/actions/screens/update.rb @@ -42,7 +42,7 @@ module Terminus tempfile = image[:tempfile] extension = File.extname tempfile - record.replace tempfile, metadata: {"filename" => "#{record.name}#{extension}"} + record.upload tempfile, metadata: {"filename" => "#{record.name}#{extension}"} repository.update record.id, image_data: record.image_attributes end diff --git a/app/repositories/firmware.rb b/app/repositories/firmware.rb index 10e2a8e3..eb6ffa85 100644 --- a/app/repositories/firmware.rb +++ b/app/repositories/firmware.rb @@ -16,7 +16,6 @@ module Terminus def delete id find(id).then { it.attachment_destroy if it } - firmware.by_pk(id).delete end diff --git a/app/structs/firmware.rb b/app/structs/firmware.rb index 20f97dcc..49eca09f 100644 --- a/app/structs/firmware.rb +++ b/app/structs/firmware.rb @@ -25,7 +25,7 @@ module Terminus def attachment_destroy store.delete attachment_id if attachment_id - attributes[:attachment_data].clear + attributes[:attachment_data].clear if attributes.key? :attachment_data end def attachment_id = attachment_attributes[:id] @@ -49,14 +49,10 @@ module Terminus attacher.assign(io, **).tap { |file| attributes[:attachment_data] = file.data } end - def replace(io, **) - attachment_destroy - upload(io, **) - self - end - def upload(io, **) + attachment_destroy attacher.upload(io, **).tap { |file| attributes[:attachment_data] = file.data } + self end def errors = attacher.errors diff --git a/app/structs/screen.rb b/app/structs/screen.rb index 41b41832..e32af9bc 100644 --- a/app/structs/screen.rb +++ b/app/structs/screen.rb @@ -30,7 +30,7 @@ module Terminus def image_destroy store.delete image_id if image_id - attributes[:image_data].clear + attributes[:image_data].clear if attributes.key? :image_data end def image_id = image_attributes[:id] @@ -61,13 +61,8 @@ module Terminus def mime_type = image_attributes.dig :metadata, :mime_type - def replace(io, **) - image_destroy - upload(io, **) - self - end - def upload(io, **) + image_destroy attacher.upload(io, **).tap { |file| attributes[:image_data] = file.data } self end diff --git a/spec/app/repositories/firmware_spec.rb b/spec/app/repositories/firmware_spec.rb index 0b4cd640..95d93682 100644 --- a/spec/app/repositories/firmware_spec.rb +++ b/spec/app/repositories/firmware_spec.rb @@ -28,7 +28,7 @@ RSpec.describe Terminus::Repositories::Firmware, :db do it "deletes associated attachment" do upload = firmware.upload StringIO.new([123].pack("N")) - repository.update firmware.id, attachment_data: upload.data + repository.update firmware.id, attachment_data: upload.attachment_attributes repository.delete firmware.id expect(Hanami.app[:shrine].storages[:store].store).to eq({}) @@ -50,7 +50,7 @@ RSpec.describe Terminus::Repositories::Firmware, :db do it "deletes all attachments" do upload = firmware.upload StringIO.new([123].pack("N")) - repository.update firmware.id, attachment_data: upload.data + repository.update firmware.id, attachment_data: upload.attachment_attributes repository.delete_all expect(Hanami.app[:shrine].storages[:store].store).to eq({}) diff --git a/spec/app/structs/firmware_spec.rb b/spec/app/structs/firmware_spec.rb index a4be3e6e..540f3f72 100644 --- a/spec/app/structs/firmware_spec.rb +++ b/spec/app/structs/firmware_spec.rb @@ -123,50 +123,25 @@ RSpec.describe Terminus::Structs::Firmware, :db do end end - describe "#replace" do - it "replaces file" do - instance = path.open { |io| firmware.replace io } + describe "#upload" do + it "uploads file when valid" do + path = temp_dir.join "test.bin" + path.binwrite [123].pack("N") + instance = path.open { |io| firmware.upload io } expect(instance.attachment_attributes).to match( id: /\h{32}\.bin/, metadata: { filename: "test.bin", + height: nil, size: 4, mime_type: "application/octet-stream", - width: nil, - height: nil + width: nil }, storage: "store" ) end - it "updates storage ID" do - id = firmware.attachment_id - instance = path.open { |io| firmware.replace io } - expect(id).not_to eq(instance.attachment_id) - end - end - - describe "#upload" do - it "uploads file when valid" do - path = temp_dir.join "test.bin" - path.binwrite [123].pack("N") - - upload = firmware.upload path.open - - expect(upload.data).to match( - "id" => /\h{32}\.bin/, - "metadata" => { - "filename" => "test.bin", - "height" => nil, - "size" => 4, - "mime_type" => "application/octet-stream", - "width" => nil - }, - "storage" => "store" - ) - end - it "updates attributes when valid" do path = temp_dir.join "test.bin" path.binwrite [123].pack("N") @@ -185,19 +160,26 @@ RSpec.describe Terminus::Structs::Firmware, :db do ) end + it "updates storage ID" do + id = firmware.attachment_id + instance = path.open { |io| firmware.upload io } + + expect(id).not_to eq(instance.attachment_id) + end + it "doesn't upload file when invalid" do upload = firmware.upload StringIO.new - expect(upload.data).to match( - "id" => /\h{32}/, - "metadata" => { - "filename" => nil, - "height" => nil, - "size" => 0, - "mime_type" => nil, - "width" => nil + expect(upload.attachment_attributes).to match( + id: /\h{32}/, + metadata: { + filename: nil, + height: nil, + size: 0, + mime_type: nil, + width: nil }, - "storage" => "store" + storage: "store" ) end end diff --git a/spec/app/structs/screen_spec.rb b/spec/app/structs/screen_spec.rb index e2417c19..4107240a 100644 --- a/spec/app/structs/screen_spec.rb +++ b/spec/app/structs/screen_spec.rb @@ -147,68 +147,6 @@ RSpec.describe Terminus::Structs::Screen, :db do end end - describe "#replace" do - it "replaces file when valid" do - instance = path.open { |io| screen.replace io } - - expect(instance.image_attributes).to match( - id: /\h{32}\.png/, - metadata: { - bit_depth: 1, - checksum: match_md5_checksum, - filename: "test.png", - height: 1, - size: 81, - mime_type: "image/png", - width: 1 - }, - storage: "store" - ) - end - - it "updates attributes when valid" do - instance = path.open { |io| screen.replace io } - - expect(instance.image_attributes).to match( - id: /\h{32}\.png/, - storage: "store", - metadata: { - bit_depth: 1, - checksum: match_md5_checksum, - filename: "test.png", - size: 81, - mime_type: "image/png", - width: 1, - height: 1 - } - ) - end - - it "updates storage ID" do - id = screen.image_id - instance = path.open { |io| screen.replace io } - expect(id).not_to eq(instance.image_id) - end - - it "doesn't replace file when invalid" do - instance = screen.replace StringIO.new - - expect(instance.image_attributes).to match( - id: /\h{32}/, - metadata: { - bit_depth: nil, - checksum: match_md5_checksum, - filename: nil, - height: nil, - size: 0, - mime_type: nil, - width: nil - }, - storage: "store" - ) - end - end - describe "#upload" do it "uploads file when valid" do instance = path.open { |io| screen.upload io } @@ -246,6 +184,13 @@ RSpec.describe Terminus::Structs::Screen, :db do ) end + it "updates storage ID" do + id = screen.image_id + instance = path.open { |io| screen.upload io } + + expect(id).not_to eq(instance.image_id) + end + it "doesn't upload file when invalid" do upload = screen.upload StringIO.new