From 085ceb16e28bf07b4b7a6c36aaa2c29fc842c602 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 6 Feb 2025 22:52:19 +0100 Subject: [PATCH] Fix export file deletion --- CHANGELOG.md | 2 +- app/controllers/exports_controller.rb | 6 +++++- spec/requests/exports_spec.rb | 29 +++++++++++++++++---------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1256b1f9..dbef78f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - After deleting one point from the map, other points can now be deleted as well. #723 #678 - +- Fixed a bug where export file was not being deleted from the server after it was deleted. #808 ### Added - `X-Dawarich-Version` header to the `GET /api/v1/health` endpoint response. #800 diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb index 6f9b4c65..34b239dc 100644 --- a/app/controllers/exports_controller.rb +++ b/app/controllers/exports_controller.rb @@ -23,7 +23,11 @@ class ExportsController < ApplicationController end def destroy - @export.destroy + ActiveRecord::Base.transaction do + @export.destroy + + File.delete(Rails.root.join('public', 'exports', @export.name)) + end redirect_to exports_url, notice: 'Export was successfully destroyed.', status: :see_other end diff --git a/spec/requests/exports_spec.rb b/spec/requests/exports_spec.rb index 0ec6fa61..2c5a6b72 100644 --- a/spec/requests/exports_spec.rb +++ b/spec/requests/exports_spec.rb @@ -76,9 +76,25 @@ RSpec.describe '/exports', type: :request do end describe 'DELETE /destroy' do - let!(:export) { create(:export, user:, url: 'exports/export.json') } + let!(:export) { create(:export, user:, url: 'exports/export.json', name: 'export.json') } + let(:export_file) { Rails.root.join('public', 'exports', export.name) } - before { sign_in user } + before do + sign_in user + + FileUtils.mkdir_p(File.dirname(export_file)) + File.write(export_file, '{"some": "data"}') + end + + after { FileUtils.rm_f(export_file) } + + it 'removes the export file from disk' do + expect(File.exist?(export_file)).to be true + + delete export_url(export) + + expect(File.exist?(export_file)).to be false + end it 'destroys the requested export' do expect { delete export_url(export) }.to change(Export, :count).by(-1) @@ -89,14 +105,5 @@ RSpec.describe '/exports', type: :request do expect(response).to redirect_to(exports_url) end - - it 'remove the export file from the disk' do - export_file = Rails.root.join('public', export.url) - FileUtils.touch(export_file) - - delete export_url(export) - - expect(File.exist?(export_file)).to be_falsey - end end end