Skip to content

Commit

Permalink
Fix file cleanup for models with folder indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
yamax2 committed Nov 6, 2022
1 parent 993fa83 commit aba669d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ GEM
yandex_client (1.3.1)
http
ox
zeitwerk (2.6.4)
zeitwerk (2.6.5)

PLATFORMS
x86_64-linux-musl
Expand Down
5 changes: 4 additions & 1 deletion app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ def with_redis_transaction(&)
def enqueue_remove_job(dir, filename)
return if filename.blank?

dir_with_index = dir
dir_with_index = "#{dir}#{folder_index}" if folder_index.nonzero?

::Yandex::RemoveFileJob.perform_async \
yandex_token_id,
[dir, filename].join('/')
[dir_with_index, filename].join('/')
end
end
5 changes: 4 additions & 1 deletion app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ def remove_file

return if storage_filename.blank?

dir_with_index = yandex_token.other_dir
dir_with_index = "#{dir_with_index}#{folder_index}" if folder_index.nonzero?

::Yandex::RemoveFileJob.perform_async(
yandex_token_id,
[yandex_token.other_dir, storage_filename].join('/')
[dir_with_index, storage_filename].join('/')
)
end
end
3 changes: 2 additions & 1 deletion app/models/yandex/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def dir_names
end

def valid_dir?(directory)
directory.blank? || directory.to_s.starts_with?('/')
directory.blank? || \
(directory.to_s.starts_with?('/') && !directory.to_s.ends_with?('/'))
end
end
end
31 changes: 31 additions & 0 deletions spec/models/photo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@
end
end

context 'when image with a folder_index' do
let(:photo) { create :photo, storage_filename: '052/001/zozo.jpg', yandex_token: node, folder_index: 5 }

it do
expect { photo.destroy }.to change { enqueued_jobs(klass: Yandex::RemoveFileJob).size }.by(1)

expect(job_args).to eq([[node.id, '/dir5/052/001/zozo.jpg']])
end
end

context 'when video' do
let(:video) do
create :photo,
Expand All @@ -200,6 +210,27 @@
end
end

context 'when video a folder_index' do
let(:video) do
create :photo,
:video,
storage_filename: 'test.mp4',
preview_filename: 'test.jpg',
video_preview_filename: 'test.preview.mp4',
yandex_token: node,
folder_index: 5
end

it do
expect { video.destroy }.
to change { enqueued_jobs(klass: Yandex::RemoveFileJob).size }.by(3)

expect(job_args).to match_array(
[[node.id, '/other5/test.mp4'], [node.id, '/other5/test.jpg'], [node.id, '/other5/test.preview.mp4']]
)
end
end

context 'when invalid video from previous release' do
let(:rubric) { create :rubric }
let(:video) do
Expand Down
11 changes: 11 additions & 0 deletions spec/models/track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@
end
end

context 'when upload with a folder_index' do
let(:track) { create :track, storage_filename: 'zozo.gpx', yandex_token: node, folder_index: 6 }
let(:job_args) { enqueued_jobs(klass: Yandex::RemoveFileJob).pluck('args') }

it do
expect { track.destroy }.to change { enqueued_jobs(klass: Yandex::RemoveFileJob).size }.by(1)

expect(job_args).to eq([[node.id, '/other6/zozo.gpx']])
end
end

context 'when track is not uploaded' do
let(:track) { create :track, local_filename: 'zozo.gpx' }

Expand Down
9 changes: 9 additions & 0 deletions spec/models/yandex/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@

it { expect(token).to be_valid }
end

context 'when ends with /' do
before { token[dir_attr] = '/zozo/' }

it do
expect(token).not_to be_valid
expect(token.errors).to include(dir_attr)
end
end
end

it_behaves_like 'dir validation', :dir
Expand Down

0 comments on commit aba669d

Please sign in to comment.