Skip to content

Commit 3647620

Browse files
authored
Rename files on upload (#355)
2 parents a117420 + ce85231 commit 3647620

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

app/models/topic.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def doc_prefix
5757
id
5858
end
5959

60+
def custom_file_name(document)
61+
[
62+
id,
63+
provider.file_name_prefix.present? ? provider.file_name_prefix.parameterize : provider.name.parameterize(separator: "_"),
64+
published_at_year,
65+
published_at_month,
66+
document.filename.base.parameterize(separator: "_"),
67+
].compact.join("_") + "." + document.filename.extension
68+
end
69+
6070
class << self
6171
def by_year(year)
6272
where("extract(year from published_at) = ?", year)

app/services/file_manager.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,9 @@ def file_content
7777
@file_content ||= document.download
7878
end
7979

80-
# naming convention described here: https://github.com/rubyforgood/skillrx/issues/305
81-
# [topic.id]_[provider.provider_name_for_file]_[topic.published_at_year]_[topic.published_at_month][document.filename.parameterize].[extension]
80+
# naming convention now enforced on upload
8281
def file_name
83-
provider = topic.provider
84-
@file_name ||= [].tap do |parts|
85-
parts << topic.id
86-
if provider.file_name_prefix.present?
87-
parts << provider.file_name_prefix
88-
else
89-
parts << provider.name
90-
end
91-
parts << topic.published_at_year
92-
parts << format("%02d", topic.published_at_month)
93-
parts << document.filename
94-
end.join("_")
82+
document.filename.to_s
9583
end
9684

9785
def language

app/services/topics/mutator.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def mutate
5555
attach_files(document_signed_ids)
5656
# topic.persisted? means that we are updating existing topic and documents can be removed
5757
shadow_delete_documents(docs_to_delete) if topic.persisted?
58+
rename_files(document_signed_ids)
5859
# document_signed_ids.any? means that some new documents were attached and we need to sync them
5960
sync_docs_for_topic_updates if document_signed_ids.any?
6061
[ :ok, topic ]
@@ -71,6 +72,30 @@ def attach_files(signed_ids)
7172
end
7273
end
7374

75+
def rename_files(signed_ids)
76+
return if signed_ids.blank?
77+
signed_ids.each do |signed_id|
78+
document = topic.documents.find { |doc| doc.blob.signed_id == signed_id }
79+
next unless document
80+
81+
new_filename = topic.custom_file_name(document)
82+
rename_document(document, new_filename)
83+
end
84+
end
85+
86+
def rename_document(document, new_filename)
87+
return unless document.blob.filename.to_s.split("_").first == "rename"
88+
return if document.filename == new_filename
89+
90+
file_io = StringIO.new(document.download)
91+
topic.documents.attach(
92+
io: file_io,
93+
filename: new_filename,
94+
content_type: document.content_type
95+
)
96+
document.purge
97+
end
98+
7499
def sync_docs_for_topic_updates
75100
topic.documents_attachments.each do |doc|
76101
DocumentsSyncJob.perform_later(

spec/jobs/documents_sync_job_spec.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44
let(:topic) { create(:topic, :with_documents) }
55
let(:provider) { topic.provider }
66
let(:document) { topic.documents.first }
7-
let(:file_name) do
8-
[
9-
topic.id,
10-
provider.file_name_prefix,
11-
topic.published_at_year,
12-
format("%02d", topic.published_at_month),
13-
document.filename,
14-
].join("_")
15-
end
7+
let(:file_name) { document.filename }
168

179
before { provider.update(file_name_prefix: "MyPrefix") }
1810

spec/models/topic_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,29 @@
4343
context "tagging" do
4444
it_behaves_like "taggable"
4545
end
46+
47+
describe "#custom_file_name" do
48+
let(:document) { double("Document", filename: ActiveStorage::Filename.new("Document name.pdf")) }
49+
let(:topic) { create(:topic, provider: provider, published_at: Time.new(2023, 12, 22)) }
50+
51+
context "when provider has a file name prefix" do
52+
let(:provider) { create(:provider, file_name_prefix: "prefix") }
53+
54+
it "returns a custom file name based on topic attributes" do
55+
expect(topic.custom_file_name(document)).to eq(
56+
"#{topic.id}_prefix_2023_12_document_name.pdf"
57+
)
58+
end
59+
end
60+
61+
context "when provider does not have a file name prefix" do
62+
let(:provider) { create(:provider, file_name_prefix: nil, name: "Provider Name") }
63+
64+
it "returns a custom file name based on topic attributes" do
65+
expect(topic.custom_file_name(document)).to eq(
66+
"#{topic.id}_provider_name_2023_12_document_name.pdf"
67+
)
68+
end
69+
end
70+
end
4671
end

spec/services/file_manager_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
it "initializes file workers with correct parameters" do
3030
expect(FileWorker).to receive(:new).with(
3131
share:,
32-
name: "#{topic.id}_MyPrefix_#{topic.published_at_year}_#{format('%02d', topic.published_at_month)}_dummy.pdf",
32+
name: "dummy.pdf",
3333
path: "#{topic.language.file_storage_prefix}CMES-Pi/assets/Content",
3434
file: document.download,
3535
new_path: nil,
3636
).and_call_original
3737
expect(FileWorker).to receive(:new).with(
3838
share:,
39-
name: "#{topic.id}_MyPrefix_#{topic.published_at_year}_#{format('%02d', topic.published_at_month)}_dummy.pdf",
39+
name: "dummy.pdf",
4040
path: "#{topic.language.file_storage_prefix}CMES-v2/assets/Content",
4141
file: document.download,
4242
new_path: nil,
@@ -64,7 +64,7 @@
6464
it "initializes file worker with parameters for video" do
6565
expect(FileWorker).to receive(:new).with(
6666
share:,
67-
name: "#{topic.id}_MyPrefix_#{topic.published_at_year}_#{format('%02d', topic.published_at_month)}_video_file.mp4",
67+
name: "video_file.mp4",
6868
path: "#{topic.language.file_storage_prefix}CMES-v2/assets/VideoContent",
6969
file: document.download,
7070
new_path: nil,

0 commit comments

Comments
 (0)