Skip to content

Commit 9ab11b9

Browse files
authored
Add Backwards Compatibility to ActiveStorage Attachments (#1864)
Add check for old attachments
1 parent 318afa3 commit 9ab11b9

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

app/controllers/attachments_controller.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,29 @@ def create
5757

5858
action_auth_level :show, :student
5959
def show
60-
attached_file = @attachment.attachment_file
61-
unless attached_file.attached?
62-
COURSE_LOGGER.log("No file attached to attachment '#{@attachment.name}'")
63-
64-
flash[:error] = "No file attached to attachment '#{@attachment.name}'"
65-
redirect_to([@course, :attachments]) && return
66-
end
6760
if @cud.instructor? || @attachment.released?
6861
begin
69-
send_data attached_file.download, filename: @attachment.filename,
70-
type: @attachment.mime_type
62+
attached_file = @attachment.attachment_file
63+
if attached_file.attached?
64+
send_data attached_file.download, filename: @attachment.filename,
65+
type: @attachment.mime_type
66+
return
67+
end
68+
69+
old_attachment_path = Rails.root.join("attachments", @attachment.filename)
70+
if File.exist?(old_attachment_path)
71+
send_file old_attachment_path, filename: @attachment.filename, type: @attachment.mime_type
72+
else
73+
COURSE_LOGGER.log("No file attached to attachment '#{@attachment.name}'")
74+
flash[:error] = "No file attached to attachment '#{@attachment.name}'"
75+
redirect_to([@course, :attachments])
76+
end
77+
return
7178
rescue StandardError
7279
COURSE_LOGGER.log("Error viewing attachment '#{@attachment.name}'")
7380
flash[:error] = "Error viewing attachment '#{@attachment.name}'"
74-
redirect_to([@course, @assessment])
81+
redirect_to([@course, @assessment]) && return
7582
end
76-
return
7783
end
7884

7985
flash[:error] = "You are unauthorized to view this attachment"

0 commit comments

Comments
 (0)