about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-04-18 03:28:50 -0500
committermultiple creatures <dev@multiple-creature.party>2019-05-21 03:16:22 -0500
commit1ca30982faae8fc96e8b61186165b001be04de1e (patch)
treed43834a28737c36326b9004496b467590f85d648 /app
parentb53ddb81269c8c8c0144ce7aeae917fa634fbf71 (diff)
Add support for general-purpose file sharing.
Diffstat (limited to 'app')
-rw-r--r--app/models/media_attachment.rb37
-rw-r--r--app/serializers/initial_state_serializer.rb2
2 files changed, 29 insertions, 10 deletions
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index 2bc81dc6f..5c93ed4c5 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -125,8 +125,7 @@ class MediaAttachment < ApplicationRecord
     },
   }.freeze
 
-  IMAGE_LIMIT = 8.megabytes
-  VIDEO_LIMIT = 40.megabytes
+  SIZE_LIMIT = 40.megabytes
   GIF_LIMIT = ENV.fetch('MAX_GIF_SIZE', 200).to_i.kilobytes
 
   belongs_to :account,          inverse_of: :media_attachments, optional: true
@@ -138,10 +137,9 @@ class MediaAttachment < ApplicationRecord
                     processors: ->(f) { file_processors f },
                     convert_options: { all: '-quality 90 -strip' }
 
-  validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
-  validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video_or_audio?
-  validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video_or_audio?
-  remotable_attachment :file, VIDEO_LIMIT
+  do_not_validate_attachment_file_type :file
+  validates_attachment_size :file, less_than: SIZE_LIMIT
+  remotable_attachment :file, SIZE_LIMIT
 
   include Attachmentable
 
@@ -167,6 +165,10 @@ class MediaAttachment < ApplicationRecord
     video? || gifv? || audio?
   end
 
+  def is_media?
+    file_content_type.in?(IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES)
+  end
+
   def to_param
     shortcode
   end
@@ -192,7 +194,9 @@ class MediaAttachment < ApplicationRecord
   after_commit :reset_parent_cache, on: :update
   before_create :prepare_description, unless: :local?
   before_create :set_shortcode
+  before_create :set_file_name, unless: :is_media?
   before_post_process :set_type_and_extension
+  before_post_process :is_media?
   before_save :set_meta
 
   class << self
@@ -225,8 +229,10 @@ class MediaAttachment < ApplicationRecord
           small: VIDEO_STYLES[:small],
           original: VIDEO_FORMAT,
         }
-      else
+      elsif VIDEO_MIME_TYPES.include? f.instance.file_content_type
         VIDEO_STYLES
+      else
+        {original: {}}
       end
     end
 
@@ -241,7 +247,7 @@ class MediaAttachment < ApplicationRecord
         [:video_transcoder, :blurhash_transcoder]
       elsif AUDIO_MIME_TYPES.include? f.file_content_type
         [:audio_transcoder]
-      else
+      elsif IMAGE_MIME_TYPES.include? f.file_content_type
         [:lazy_thumbnail, :blurhash_transcoder]
       end
     end
@@ -264,8 +270,21 @@ class MediaAttachment < ApplicationRecord
     self.description = description.strip[0...666] unless description.nil?
   end
 
+  def set_file_name
+    temp = file.queued_for_write[:original]
+    unless temp.nil?
+      orig = temp.original_filename
+      ext = File.extname(orig).downcase
+      name = File.basename(orig, '.*')
+      self.file.instance_write(:file_name, "#{name}#{ext}")
+    end
+  end
+
   def set_type_and_extension
-    self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : AUDIO_MIME_TYPES.include?(file_content_type) ? :audio : :image
+    self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video :
+      AUDIO_MIME_TYPES.include?(file_content_type) ? :audio :
+      IMAGE_MIME_TYPES.include?(file_content_type) ? :image :
+      :unknown
   end
 
   def set_meta
diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb
index b951696da..4622ddb9e 100644
--- a/app/serializers/initial_state_serializer.rb
+++ b/app/serializers/initial_state_serializer.rb
@@ -77,7 +77,7 @@ class InitialStateSerializer < ActiveModel::Serializer
   end
 
   def media_attachments
-    { accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::AUDIO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES }
+    { accept_content_types: [] }
   end
 
   private