about summary refs log tree commit diff
path: root/app/models/media_attachment.rb
diff options
context:
space:
mode:
authorEugen <eugen@zeonfederated.com>2017-04-19 15:37:18 +0200
committerGitHub <noreply@github.com>2017-04-19 15:37:18 +0200
commitbfbc2ca0d8dcef47f8581585b42f13b6b4c933d9 (patch)
tree800163fc3fec1cfbc8fa681ef36b1a89304da207 /app/models/media_attachment.rb
parent5ce8a1811abbb304a6cf92616a9db4fb3cac89ee (diff)
Attachment list for uncached attachments (#2110)
* For undownloaded attachments, set type :unknown, display them as a list in the web UI

* Fix case when attachment type is set explicitly
Diffstat (limited to 'app/models/media_attachment.rb')
-rw-r--r--app/models/media_attachment.rb11
1 files changed, 7 insertions, 4 deletions
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index ec1808790..bb16adb5b 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -3,7 +3,7 @@
 class MediaAttachment < ApplicationRecord
   self.inheritance_column = nil
 
-  enum type: [:image, :gifv, :video]
+  enum type: [:image, :gifv, :video, :unknown]
 
   IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
   VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
@@ -95,6 +95,8 @@ class MediaAttachment < ApplicationRecord
   private
 
   def set_shortcode
+    self.type = :unknown if file.blank? && type.blank?
+
     return unless local?
 
     loop do
@@ -104,9 +106,10 @@ class MediaAttachment < ApplicationRecord
   end
 
   def set_type_and_extension
-    self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
-
-    unless file.blank?
+    if file.blank?
+      self.type = :unknown
+    else
+      self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
       extension = Paperclip::Interpolations.content_type_extension(file, :original)
       basename  = Paperclip::Interpolations.basename(file, :original)
       file.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')