Merge branch 'send-big-or-bad-mime-as-link' into 'master'

Stephen Paul Weber created

Added method to send link via SMS on large file sizes and unsupported MIME types.

See merge request soprani.ca/sgx-bwmsgsv2!16

Change summary

sgx-bwmsgsv2.rb | 74 +++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 66 insertions(+), 8 deletions(-)

Detailed changes

sgx-bwmsgsv2.rb 🔗

@@ -40,6 +40,55 @@ require_relative 'lib/registration_repo'
 
 Sentry.init
 
+# List of supported MIME types from Bandwidth - https://support.bandwidth.com/hc/en-us/articles/360014128994-What-MMS-file-types-are-supported-
+MMS_MIME_TYPES = [
+	"application/json",
+	"application/ogg",
+	"application/pdf",
+	"application/rtf",
+	"application/zip",
+	"application/x-tar",
+	"application/xml",
+	"application/gzip",
+	"application/x-bzip2",
+	"application/x-gzip",
+	"application/smil",
+	"application/javascript",
+	"audio/mp4",
+	"audio/mpeg",
+	"audio/ogg",
+	"audio/flac",
+	"audio/webm",
+	"audio/wav",
+	"audio/amr",
+	"audio/3gpp",
+	"image/bmp",
+	"image/gif",
+	"image/jpeg",
+	"image/pjpeg",
+	"image/png",
+	"image/svg+xml",
+	"image/tiff",
+	"image/webp",
+	"image/x-icon",
+	"text/css",
+	"text/csv",
+	"text/calendar",
+	"text/plain",
+	"text/javascript",
+	"text/vcard",
+	"text/vnd.wap.wml",
+	"text/xml",
+	"video/avi",
+	"video/mp4",
+	"video/mpeg",
+	"video/ogg",
+	"video/quicktime",
+	"video/webm",
+	"video/x-ms-wmv",
+	"video/x-flv"
+]
+
 def panic(e)
 	Sentry.capture_exception(e)
 	puts "Shutting down gateway due to exception: #{e.message}"
@@ -252,17 +301,26 @@ module SGXbwmsgsv2
 		end
 		puts "MMSOOB: found a url node - checking if to make MMS..."
 
-		# TODO: check size of file at un.text and shrink if need
-
 		body = s.respond_to?(:body) ? s.body : ''
-		# some clients send URI in both body & <url/> so delete
-		s.body = body.sub(/\s*#{Regexp.escape(un.text)}\s*$/, '')
+		EM::HttpRequest.new(un.text, tls: { verify_peer: true }).head.then { |http|
+			# If content is too large, or MIME type is not supported, place the link inside the body and do not send MMS.
+			if http.response_header["CONTENT_LENGTH"].to_i > 3500000 ||
+			   !MMS_MIME_TYPES.include?(http.response_header["CONTENT_TYPE"])
+				unless body.include?(un.text)
+					s.body = body.empty? ? un.text : "#{body}\n#{un.text}"
+				end
+				to_catapult(s, nil, num_dest, user_id, token, secret, usern)
+			else # If size is less than ~3.5MB, strip the link from the body and attach media in the body.
+				# some clients send URI in both body & <url/> so delete
+				s.body = body.sub(/\s*#{Regexp.escape(un.text)}\s*$/, '')
 
-		puts "MMSOOB: url text is '#{un.text}'"
-		puts "MMSOOB: the body is '#{body.to_s.strip}'"
+				puts "MMSOOB: url text is '#{un.text}'"
+				puts "MMSOOB: the body is '#{body.to_s.strip}'"
 
-		puts "MMSOOB: sending MMS since found OOB & user asked"
-		to_catapult(s, un.text, num_dest, user_id, token, secret, usern)
+				puts "MMSOOB: sending MMS since found OOB & user asked"
+				to_catapult(s, un.text, num_dest, user_id, token, secret, usern)
+			end
+		}
 	end
 
 	def self.to_catapult(s, murl, num_dest, user_id, token, secret, usern)