Skip to content

Commit 722ea1e

Browse files
committed
Merge branch 'feature/track-file-downloads' into staging
2 parents d9819c0 + 2e11339 commit 722ea1e

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

app/controllers/private_uploads_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def authenticate_user!
3232
end
3333
end
3434

35-
def allowed_filepath
35+
def allowed_directory
3636
return File.join(Rails.root, "tmp", "private", "uploads") if Rails.env.test?
3737

3838
File.join(Rails.root, "private", "uploads")

app/controllers/uploads_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ def ensure_valid_db_record
7373
end
7474

7575
def allowed_models
76-
Dir.entries(Rails.root.join("uploads"))
77-
.select { |entry| File.directory?(Rails.root.join("uploads", entry)) }
76+
uploads_root = ApplicationUploader.new.public_root.join("uploads")
77+
Dir.entries(uploads_root)
78+
.select { |entry| File.directory?(uploads_root.join(entry)) }
7879
.reject { |entry| entry.start_with?(".") || entry == "tmp" }
7980
.map { |entry| MODELS_OVERRIDES[entry.downcase] || entry }
8081
end

app/models/concerns/moveable_attachment.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ module MoveableAttachment
1212
return if uploader.nil?
1313
return if uploader.file.nil?
1414

15-
from = File.dirname(uploader.file.file.gsub("/public/", "/private/"))
16-
to = File.dirname(from.gsub("/private/", "/public/"))
15+
public_uploads = uploader.public_root.join("uploads").to_s
16+
private_uploads = uploader.private_root.join("uploads").to_s
17+
from = File.dirname(uploader.file.file.gsub(public_uploads, private_uploads))
18+
to = File.dirname(from.gsub(private_uploads, public_uploads))
1719
move_attachment(from, to)
1820
end
1921

@@ -22,8 +24,10 @@ module MoveableAttachment
2224
return if uploader.nil?
2325
return if uploader.file.nil?
2426

25-
from = File.dirname(uploader.file.file.gsub("/private/", "/public/"))
26-
to = File.dirname(from.gsub("/public/", "/private/"))
27+
public_uploads = uploader.public_root.join("uploads").to_s
28+
private_uploads = uploader.private_root.join("uploads").to_s
29+
from = File.dirname(uploader.file.file.gsub(private_uploads, public_uploads))
30+
to = File.dirname(from.gsub(public_uploads, private_uploads))
2731
move_attachment(from, to)
2832
end
2933
end

app/models/gov_document.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ def has_data?
8080
end
8181

8282
def reset_to_not_provided!
83-
remove_attachment!
8483
update!(
85-
status: OperatorDocument.statuses[:doc_not_provided],
84+
status: OperatorDocument.statuses[:doc_not_provided], attachment: nil,
8685
expire_date: nil, start_date: nil, uploaded_by: nil, user_id: nil,
8786
value: nil, link: nil
8887
)
@@ -102,8 +101,8 @@ def move_previous_attachment_to_private_directory
102101
previous_attachment_filename = previous_changes[:attachment][0]
103102
return if previous_attachment_filename.blank?
104103

105-
from = File.join(attachment.root, attachment.store_dir, previous_attachment_filename)
106-
to = from.gsub("/public/", "/private/")
104+
from = File.join(attachment.public_root, attachment.store_dir, previous_attachment_filename)
105+
to = File.join(attachment.private_root, attachment.store_dir, previous_attachment_filename)
107106
FileUtils.makedirs(File.dirname(to))
108107
system "mv #{Shellwords.escape(from)} #{Shellwords.escape(to)}"
109108
end
@@ -113,9 +112,9 @@ def move_attachment_to_public_directory
113112
attachment_attr = self[:attachment]
114113
return if attachment_attr.nil?
115114

116-
to = File.join(attachment.root, attachment.store_dir, attachment_attr)
117-
from = to.gsub("/public/", "/private/")
118-
FileUtils.makedirs(File.dirname(from))
115+
from = File.join(attachment.private_root, attachment.store_dir, attachment_attr)
116+
to = File.join(attachment.public_root, attachment.store_dir, attachment_attr)
117+
FileUtils.makedirs(File.dirname(to))
119118
system "mv #{Shellwords.escape(from)} #{Shellwords.escape(to)}"
120119
end
121120

app/uploaders/application_uploader.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ class ApplicationUploader < CarrierWave::Uploader::Base
44
storage :file
55

66
def root
7-
result = Rails.root
8-
result = Rails.root.join("private") if private_upload?
9-
result = result.to_s.gsub(Rails.root.to_s, Rails.root.join("tmp").to_s) if Rails.env.test?
10-
result
7+
return private_root if private_upload?
8+
9+
public_root
1110
end
1211

1312
def cache_dir
@@ -34,6 +33,18 @@ def private_upload?
3433
false
3534
end
3635

36+
def public_root
37+
return Rails.root.join("tmp") if Rails.env.test?
38+
39+
Rails.root
40+
end
41+
42+
def private_root
43+
return Rails.root.join("tmp", "private") if Rails.env.test?
44+
45+
Rails.root.join("private")
46+
end
47+
3748
def original_filename
3849
if file.present?
3950
file.filename

spec/integration/uploads_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RSpec.describe UploadsController, type: :request do
66
before(:all) do
77
@etc_dir = Rails.root.join("tmp", "etc")
8+
FileUtils.mkdir_p(ApplicationUploader.new.public_root.join("uploads"))
89
FileUtils.mkdir_p(@etc_dir)
910

1011
@observation_report = create(:observation_report)

spec/models/gov_document_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,29 @@
2828
context "when removing attachment" do
2929
it "moves previous attachment to private folder" do
3030
expect(document.attachment.file.file).to match(Rails.root.join("tmp/uploads").to_s)
31-
document.remove_attachment!
31+
expect(document.attachment.file.exists?).to be(true)
32+
document.attachment = nil
3233
document.save!
3334
document.reload
3435
expect(document.read_attribute(:attachment)).to be_nil
3536
prev_version = document.versions.last.reify
3637
expect(prev_version.attachment.file.file).to match("/private/uploads")
38+
expect(prev_version.attachment.file.exists?).to be(true)
3739
end
3840
end
3941

4042
context "when changing attachment" do
4143
it "moves previous attachment to private folder" do
4244
expect(document.attachment.file.file).to match(Rails.root.join("tmp/uploads").to_s)
45+
expect(document.attachment.file.exists?).to be(true)
4346
document.attachment = Rack::Test::UploadedFile.new(File.join(Rails.root, "spec", "support", "files", "doc.pdf"))
4447
document.save!
4548
document.reload
4649
expect(document.read_attribute(:attachment)).to match(".pdf")
4750
prev_version = document.versions.last.reify
4851
expect(prev_version.attachment.file.file).to match("/private/uploads")
4952
expect(prev_version.attachment.file.file).to match(".png")
53+
expect(prev_version.attachment.file.exists?).to be(true)
5054
end
5155
end
5256
end
@@ -98,6 +102,7 @@
98102
document.destroy!
99103
document.reload
100104
expect(document.attachment.file.file).to match("/private/uploads")
105+
expect(document.attachment.file.exists?).to be(true)
101106
end
102107
end
103108

@@ -112,12 +117,15 @@
112117

113118
it "moves attachment back to public directory" do
114119
expect(document.attachment.file.file).to match("/private/uploads")
120+
expect(document.attachment.file.exists?).to be(true)
115121
document.restore
116122
reloaded_doc = GovDocument.find(document.id) # as reload does not reload paper_trail.live? weird
117123
expect(reloaded_doc.attachment.file.file).to match(Rails.root.join("tmp/uploads").to_s)
124+
expect(reloaded_doc.attachment.file.exists?).to be(true)
118125
# first version should stay in private directory
119126
prev_version = reloaded_doc.versions[-2].reify
120127
expect(prev_version.attachment.file.file).to match("/private/uploads")
128+
expect(prev_version.attachment.file.exists?).to be(true)
121129
end
122130
end
123131
end

spec/models/observation_document_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
report.destroy!
3737
report.reload
3838
expect(report.attachment.file.file).to match("/private/uploads")
39+
expect(report.attachment.file.exists?).to be(true)
3940
end
4041
end
4142

@@ -50,6 +51,7 @@
5051
report.restore
5152
report.reload
5253
expect(report.attachment.file.file).to match(Rails.root.join("tmp/uploads").to_s)
54+
expect(report.attachment.file.exists?).to be(true)
5355
end
5456
end
5557
end

spec/models/observation_report_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
report.destroy!
8484
report.reload
8585
expect(report.attachment.file.file).to match("/private/uploads")
86+
expect(report.attachment.file.exists?).to be(true)
8687
end
8788
end
8889

@@ -97,6 +98,7 @@
9798
report.restore
9899
report.reload
99100
expect(report.attachment.file.file).to match(Rails.root.join("tmp/uploads").to_s)
101+
expect(report.attachment.file.exists?).to be(true)
100102
end
101103
end
102104
end

0 commit comments

Comments
 (0)