diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 2b0545dfa..40da4f273 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -29,14 +29,10 @@ class UploadsController < ApplicationController return render_404 unless Discourse.store.internal? return render_404 if SiteSetting.prevent_anons_from_downloading_files && current_user.nil? - id = params[:id].to_i - url = request.fullpath - - # the "url" parameter is here to prevent people from scanning the uploads using the id - if upload = (Upload.find_by(id: id, url: url) || Upload.find_by(sha1: params[:sha])) - opts = {filename: upload.original_filename} + if upload = Upload.find_by(sha1: params[:sha]) + opts = { filename: upload.original_filename } opts[:disposition] = 'inline' if params[:inline] - send_file(Discourse.store.path_for(upload),opts) + send_file(Discourse.store.path_for(upload), opts) else render_404 end diff --git a/app/serializers/upload_serializer.rb b/app/serializers/upload_serializer.rb index 1d3812e20..9e0d86624 100644 --- a/app/serializers/upload_serializer.rb +++ b/app/serializers/upload_serializer.rb @@ -1,5 +1,5 @@ class UploadSerializer < ApplicationSerializer - attributes :url, :original_filename, :filesize, :width, :height + attributes :id, :url, :original_filename, :filesize, :width, :height end diff --git a/config/nginx.sample.conf b/config/nginx.sample.conf index ca7f2d1cb..6dbd5a32d 100644 --- a/config/nginx.sample.conf +++ b/config/nginx.sample.conf @@ -136,7 +136,7 @@ server { try_files $uri =404; } # thumbnails & optimized images - location ~ /_optimized/ { + location ~ /_?optimized/ { try_files $uri =404; } diff --git a/config/routes.rb b/config/routes.rb index 7fe30be8f..3af436fad 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -289,8 +289,7 @@ Discourse::Application.routes.draw do get "stylesheets/:name.css" => "stylesheets#show", constraints: {name: /[a-z0-9_]+/} - get "uploads/:site/:id/:sha.:extension" => "uploads#show", constraints: {site: /\w+/, id: /\d+/, sha: /[a-z0-9]{15,16}/i, extension: /\w{2,}/} - get "uploads/:site/:sha" => "uploads#show", constraints: { site: /\w+/, sha: /[a-z0-9]{40}/} + get "uploads/:site/:sha" => "uploads#show", constraints: { site: /\w+/, sha: /[a-f0-9]{40}/} post "uploads" => "uploads#create" get "posts" => "posts#latest" diff --git a/lib/file_store/local_store.rb b/lib/file_store/local_store.rb index 9fbffa800..2fce96c36 100644 --- a/lib/file_store/local_store.rb +++ b/lib/file_store/local_store.rb @@ -51,10 +51,6 @@ module FileStore "#{public_dir}#{upload.url}" end - def avatar_template(avatar) - relative_avatar_template(avatar) - end - def purge_tombstone(grace_period) `find #{tombstone_dir} -mtime +#{grace_period} -type f -delete` end @@ -62,33 +58,16 @@ module FileStore private def get_path_for_upload(file, upload) - unique_sha1 = Digest::SHA1.hexdigest("#{Time.now}#{upload.original_filename}")[0..15] - extension = File.extname(upload.original_filename) - clean_name = "#{unique_sha1}#{extension}" - # path - "#{relative_base_url}/#{upload.id}/#{clean_name}" + get_path_for("original".freeze, upload.sha1, upload.extension) end def get_path_for_optimized_image(file, optimized_image) - # 1234567890ABCDEF_100x200.jpg - filename = [ - optimized_image.sha1[6..15], - "_#{optimized_image.width}x#{optimized_image.height}", - optimized_image.extension, - ].join - # path - "#{relative_base_url}/_optimized/#{optimized_image.sha1[0..2]}/#{optimized_image.sha1[3..5]}/#{filename}" + extension = "_#{optimized_image.width}x#{optimized_image.height}#{optimized_image.extension}" + get_path_for("optimized".freeze, optimized_image.sha1, extension) end - def relative_avatar_template(avatar) - File.join( - relative_base_url, - "avatars", - avatar.sha1[0..2], - avatar.sha1[3..5], - avatar.sha1[6..15], - "{size}#{avatar.extension}" - ) + def get_path_for(type, sha, extension) + "#{relative_base_url}/#{type}/#{sha[0]}/#{sha[1]}/#{sha}#{extension}" end def store_file(file, path) diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index e9b70107c..33403f154 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -118,7 +118,7 @@ describe CookedPostProcessor do it "generates overlay information" do cpp.post_process_images - expect(cpp.html).to match_html '