diff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rb index 25ae274267b..5692f47515a 100644 --- a/lib/api/helpers/attachment_renderer.rb +++ b/lib/api/helpers/attachment_renderer.rb @@ -86,6 +86,10 @@ module API end def send_attachment(attachment) + if attachment.diskfile.nil? + raise ::API::Errors::NotFound.new + end + content_type attachment_content_type(attachment) header["Content-Disposition"] = attachment.content_disposition env["api.format"] = :binary diff --git a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb index cc32297a87d..57fd8038680 100644 --- a/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb +++ b/spec/requests/api/v3/attachments/attachment_resource_shared_examples.rb @@ -471,6 +471,13 @@ RSpec.shared_examples "an APIv3 attachment resource", content_type: :json, type: expect(subject.body) .to match(mock_file.read) end + + it "responds with not found if file has been deleted" do + File.delete attachment.file.path + + get path + expect(subject.status).to eq 404 + end end context "for a local text file" do