changeset 464:f11862e58af4

Canonicalize 0-prefixed ids as well
author nanaya <me@nanaya.pro>
date Mon, 10 Dec 2018 02:38:45 +0900
parents 4608d12ebe03
children f109a8aacad5
files app/controllers/pastes_controller.rb app/models/paste.rb
diffstat 2 files changed, 9 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/pastes_controller.rb	Mon Dec 10 02:35:08 2018 +0900
+++ b/app/controllers/pastes_controller.rb	Mon Dec 10 02:38:45 2018 +0900
@@ -1,12 +1,17 @@
 class PastesController < ApplicationController
-  before_action :lowercase_path, :only => :show
-
   # GET /1
   # GET /1.txt
   def show
     @paste = Paste.safe_find(params[:id])
     return head :not_found unless @paste
 
+    if params[:id] != @paste.to_param
+      redirect_to :action => :show,
+        :id => @paste.to_param,
+        :format => params[:format]
+      return
+    end
+
     respond_to do |format|
       format.html
       format.txt
@@ -60,13 +65,6 @@
 
   private
 
-  def lowercase_path
-    correct_path = request.fullpath.downcase
-    return if correct_path == request.fullpath
-
-    redirect_to correct_path, :status => :moved_permanently
-  end
-
   def paste_params
     params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key, :language)
   end
--- a/app/models/paste.rb	Mon Dec 10 02:35:08 2018 +0900
+++ b/app/models/paste.rb	Mon Dec 10 02:38:45 2018 +0900
@@ -15,9 +15,9 @@
   end
 
   def self.safe_find(raw_id)
-    /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/ =~ raw_id.to_s
+    /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/i =~ raw_id.to_s
 
-    find_by(:secret => secret, :id => id)
+    find_by(:secret => secret.try(:downcase), :id => id)
   end
 
   def self.graceful_create(params)