handle zip upload
This commit is contained in:
@@ -3,6 +3,19 @@
|
||||
module Templates
|
||||
module CreateAttachments
|
||||
PDF_CONTENT_TYPE = 'application/pdf'
|
||||
ZIP_CONTENT_TYPE = 'application/zip'
|
||||
JSON_CONTENT_TYPE = 'application/json'
|
||||
DOCUMENT_EXTENSIONS = %w[.docx .doc .xlsx .xls .odt .rtf].freeze
|
||||
|
||||
DOCUMENT_CONTENT_TYPES = %w[
|
||||
application/vnd.openxmlformats-officedocument.wordprocessingml.document
|
||||
application/msword
|
||||
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
|
||||
application/vnd.ms-excel
|
||||
application/vnd.oasis.opendocument.text
|
||||
application/rtf
|
||||
].freeze
|
||||
|
||||
ANNOTATIONS_SIZE_LIMIT = 6.megabytes
|
||||
InvalidFileType = Class.new(StandardError)
|
||||
PdfEncrypted = Class.new(StandardError)
|
||||
@@ -10,7 +23,7 @@ module Templates
|
||||
module_function
|
||||
|
||||
def call(template, params, extract_fields: false)
|
||||
Array.wrap(params[:files].presence || params[:file]).map do |file|
|
||||
extract_zip_files(params[:files].presence || params[:file]).flat_map do |file|
|
||||
handle_file_types(template, file, params, extract_fields:)
|
||||
end
|
||||
end
|
||||
@@ -53,6 +66,40 @@ module Templates
|
||||
raise PdfEncrypted
|
||||
end
|
||||
|
||||
def extract_zip_files(files)
|
||||
extracted_files = []
|
||||
|
||||
Array.wrap(files).each do |file|
|
||||
if file.content_type == ZIP_CONTENT_TYPE
|
||||
Zip::File.open(file.tempfile).each do |entry|
|
||||
next if entry.directory?
|
||||
|
||||
tempfile = Tempfile.new(entry.name)
|
||||
tempfile.binmode
|
||||
entry.get_input_stream { |in_stream| IO.copy_stream(in_stream, tempfile) }
|
||||
tempfile.rewind
|
||||
|
||||
type = Marcel::MimeType.for(tempfile, name: entry.name)
|
||||
|
||||
next if type.exclude?('image') &&
|
||||
type != PDF_CONTENT_TYPE &&
|
||||
type != JSON_CONTENT_TYPE &&
|
||||
DOCUMENT_CONTENT_TYPES.exclude?(type)
|
||||
|
||||
extracted_files << ActionDispatch::Http::UploadedFile.new(
|
||||
filename: File.basename(entry.name),
|
||||
type:,
|
||||
tempfile:
|
||||
)
|
||||
end
|
||||
else
|
||||
extracted_files << file
|
||||
end
|
||||
end
|
||||
|
||||
extracted_files
|
||||
end
|
||||
|
||||
def handle_file_types(template, file, params, extract_fields:)
|
||||
if file.content_type.include?('image') || file.content_type == PDF_CONTENT_TYPE
|
||||
return handle_pdf_or_image(template, file, file.read, params, extract_fields:)
|
||||
|
||||
Reference in New Issue
Block a user