17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'app/services/collavre/tools/creative_attach_files_service.rb', line 17
def call(creative_id:, files:)
raise "Current.user is required" unless Current.user
creative = Creative.find_by(id: creative_id)
return { error: "Creative not found", id: creative_id } unless creative
unless creative.has_permission?(Current.user, :write)
return { error: "No write permission on Creative", id: creative_id }
end
unless creative.attachments_embeddable?
return { error: "Cannot attach files to GitHub-synced content", id: creative_id }
end
return { error: "No files provided" } if files.blank?
return { error: "Each file requires a filename" } if files.any? { |f| f["filename"].to_s.blank? }
blobs = files.map do |f|
name = f["filename"].to_s
content = f["content"].to_s
ActiveStorage::Blob.create_and_upload!(
io: StringIO.new(content),
filename: name,
content_type: f["content_type"].presence || Marcel::MimeType.for(name: name) || "text/plain"
)
end
blobs.each { |blob| creative.embed_attachment_blob!(blob) }
{
success: true,
creative_id: creative.id,
attachments: blobs.map { |b|
{
signed_id: b.signed_id,
filename: b.filename.to_s,
content_type: b.content_type,
byte_size: b.byte_size,
url: public_asset_url(b)
}
}
}
end
|