summaryrefslogtreecommitdiff
path: root/lib/gitlab/file_type_detection.rb
diff options
context:
space:
mode:
authorFrancisco Javier López <fjlopez@gitlab.com>2018-09-04 10:39:08 +0000
committerDouwe Maan <douwe@gitlab.com>2018-09-04 10:39:08 +0000
commitf9475e299c6f6b363d5ea302f1295a3ea0bf9adb (patch)
treecec2910cfe3c0065a88e884a60a588c548c0cae9 /lib/gitlab/file_type_detection.rb
parent0689900c7a5ce368c780f1fce8f465685bbfa9d6 (diff)
downloadgitlab-ce-f9475e299c6f6b363d5ea302f1295a3ea0bf9adb.tar.gz
Uploads to wiki stored inside the wiki git repository
Diffstat (limited to 'lib/gitlab/file_type_detection.rb')
-rw-r--r--lib/gitlab/file_type_detection.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/gitlab/file_type_detection.rb b/lib/gitlab/file_type_detection.rb
new file mode 100644
index 00000000000..25ee07cf940
--- /dev/null
+++ b/lib/gitlab/file_type_detection.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+# File helpers methods.
+# It needs the method filename to be defined.
+module Gitlab
+ module FileTypeDetection
+ IMAGE_EXT = %w[png jpg jpeg gif bmp tiff ico].freeze
+ # We recommend using the .mp4 format over .mov. Videos in .mov format can
+ # still be used but you really need to make sure they are served with the
+ # proper MIME type video/mp4 and not video/quicktime or your videos won't play
+ # on IE >= 9.
+ # http://archive.sublimevideo.info/20150912/docs.sublimevideo.net/troubleshooting.html
+ VIDEO_EXT = %w[mp4 m4v mov webm ogv].freeze
+ # These extension types can contain dangerous code and should only be embedded inline with
+ # proper filtering. They should always be tagged as "Content-Disposition: attachment", not "inline".
+ DANGEROUS_EXT = %w[svg].freeze
+
+ def image?
+ extension_match?(IMAGE_EXT)
+ end
+
+ def video?
+ extension_match?(VIDEO_EXT)
+ end
+
+ def image_or_video?
+ image? || video?
+ end
+
+ def dangerous?
+ extension_match?(DANGEROUS_EXT)
+ end
+
+ private
+
+ def extension_match?(extensions)
+ return false unless filename
+
+ extension = File.extname(filename).delete('.')
+ extensions.include?(extension.downcase)
+ end
+ end
+end