]> cat aescling's git repositories - mastodon.git/commitdiff
Fix #416 - Generate random unique 14-byte (19 characters) shortcodes
authorEugen Rochko <eugen@zeonfederated.com>
Thu, 5 Jan 2017 23:21:12 +0000 (00:21 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Thu, 5 Jan 2017 23:29:12 +0000 (00:29 +0100)
for local attachments, use them in URLs. Check status privacy
before redirecting to actual file.

app/controllers/media_controller.rb
app/models/media_attachment.rb
db/migrate/20170105224407_add_shortcode_to_media_attachments.rb [new file with mode: 0644]
db/schema.rb

index 6f1f7ec482f1e1ecacc8b5550483414fa20bdaff..488c4f944f6f7c544067776949872ddcdf1a93dd 100644 (file)
@@ -10,6 +10,7 @@ class MediaController < ApplicationController
   private
 
   def set_media_attachment
-    @media_attachment = MediaAttachment.where.not(status_id: nil).find(params[:id])
+    @media_attachment = MediaAttachment.where.not(status_id: nil).find_by!(shortcode: params[:id])
+    raise ActiveRecord::RecordNotFound unless @media_attachment.status.permitted?(current_account)
   end
 end
index 2a5d23739ae9dd707d57ed357fd63b47ee36c00a..ecbed03e33402afd18c1207a96fe4ed0af6b306a 100644 (file)
@@ -16,6 +16,7 @@ class MediaAttachment < ApplicationRecord
 
   validates :account, presence: true
 
+  scope :local, -> { where(remote_url: '') }
   default_scope { order('id asc') }
 
   def local?
@@ -38,6 +39,12 @@ class MediaAttachment < ApplicationRecord
     image? ? 'image' : 'video'
   end
 
+  def to_param
+    shortcode
+  end
+
+  before_create :set_shortcode
+
   class << self
     private
 
@@ -62,4 +69,15 @@ class MediaAttachment < ApplicationRecord
       end
     end
   end
+
+  private
+
+  def set_shortcode
+    return unless local?
+
+    loop do
+      self.shortcode = SecureRandom.urlsafe_base64(14)
+      break if MediaAttachment.find_by(shortcode: shortcode).nil?
+    end
+  end
 end
diff --git a/db/migrate/20170105224407_add_shortcode_to_media_attachments.rb b/db/migrate/20170105224407_add_shortcode_to_media_attachments.rb
new file mode 100644 (file)
index 0000000..2685ae1
--- /dev/null
@@ -0,0 +1,14 @@
+class AddShortcodeToMediaAttachments < ActiveRecord::Migration[5.0]
+  def up
+    add_column :media_attachments, :shortcode, :string, null: true, default: nil
+    add_index :media_attachments, :shortcode, unique: true
+
+    # Migrate old links
+    MediaAttachment.local.update_all('shortcode = id')
+  end
+
+  def down
+       remove_index :media_attachments, :shortcode
+       remove_column :media_attachments, :shortcode
+  end
+end
index b9236d42fdced963d33d16e9a973e7ccd9390356..a535c5fdb1217a36bffcd13c14ba38b6b4995595 100644 (file)
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20161222204147) do
+ActiveRecord::Schema.define(version: 20170105224407) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -95,6 +95,8 @@ ActiveRecord::Schema.define(version: 20161222204147) do
     t.integer  "account_id"
     t.datetime "created_at",                     null: false
     t.datetime "updated_at",                     null: false
+    t.string   "shortcode"
+    t.index ["shortcode"], name: "index_media_attachments_on_shortcode", unique: true, using: :btree
     t.index ["status_id"], name: "index_media_attachments_on_status_id", using: :btree
   end