Skip to content

Commit c13d32a

Browse files
authored
Fix for attaching media in editor builds (#502)
## Status - Closes RaspberryPiFoundation/digital-editor-issues#438 ## What's changed? In some scenarios the blob is not being created properly on attach, which is causing a foreign key constraint, this change explicitly creates the blob prior to attaching it. ## Steps to perform after deploying to production - Try re-running one of the failed jobs
1 parent 88162af commit c13d32a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Diff for: lib/project_importer.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,30 @@ def attach_media_if_needed
7171

7272
existing_media_file.purge
7373
end
74+
7475
if images.include?(media_file)
75-
project.images.attach(**media_file)
76+
blob = create_blob(media_file)
77+
project.images.attach(blob)
7678
elsif videos.include?(media_file)
77-
project.videos.attach(**media_file)
79+
blob = create_blob(media_file)
80+
project.videos.attach(blob)
7881
elsif audio.include?(media_file)
79-
project.audio.attach(**media_file)
82+
blob = create_blob(media_file)
83+
project.audio.attach(blob)
8084
else
8185
raise "Unsupported media file: #{media_file[:filename]}"
8286
end
8387
end
8488
end
8589

90+
def create_blob(media_file)
91+
ActiveStorage::Blob.create_and_upload!(
92+
io: media_file[:io],
93+
filename: media_file[:filename],
94+
content_type: media_file[:content_type]
95+
)
96+
end
97+
8698
def find_existing_media_file(filename)
8799
project.media.find { |i| i.blob.filename == filename }
88100
end

0 commit comments

Comments
 (0)