From dee9128cb3bd2012262c15b5e005cc4edae42d7f Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sun, 15 Sep 2024 14:18:35 -0500 Subject: [PATCH] fix up file uris/uri path code --- Rakefile | 7 ++++--- models/site.rb | 23 ++++++++++++++++------- tests/site_file_tests.rb | 4 ++-- views/dashboard/files.erb | 2 +- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Rakefile b/Rakefile index fc218d94..f07c06f0 100644 --- a/Rakefile +++ b/Rakefile @@ -139,12 +139,13 @@ desc 'generate_sitemap' task :generate_sitemap => [:environment] do sorted_sites = {} + # We pop off array, so highest scores go last. sites = Site. select(:id, :username, :updated_at, :profile_enabled). where(site_changed: true). exclude(updated_at: nil). exclude(is_deleted: true). - order(:follow_count, :updated_at). + order(:score). all site_files = [] @@ -152,13 +153,13 @@ task :generate_sitemap => [:environment] do sites.each do |site| site.site_files_dataset.exclude(path: 'not_found.html').where(path: /\.html?$/).all.each do |site_file| - if site.file_uri(site_file.path) == site.uri+'/' + if site.uri(site_file.path) == site.uri priority = 0.5 else priority = 0.4 end - site_files << [site.file_uri(site_file.path), site_file.updated_at.utc.iso8601, priority] + site_files << [site.uri(site_file.path), site_file.updated_at.utc.iso8601, priority] end end diff --git a/models/site.rb b/models/site.rb index 0964ff2e..babe693b 100644 --- a/models/site.rb +++ b/models/site.rb @@ -1244,8 +1244,10 @@ class Site < Sequel::Model def file_list(path='') list = Dir.glob(File.join(files_path(path), '*'), File::FNM_DOTMATCH).reject { |entry| File.basename(entry) == '.' || File.basename(entry) == '..' }.collect do |file_path| extname = File.extname file_path + path = file_path.gsub(base_files_path+'/', '') file = { - path: file_path.gsub(base_files_path+'/', ''), + path: path, + uri: uri(path), name: File.basename(file_path), ext: extname.gsub('.', ''), is_directory: File.directory?(file_path), @@ -1426,13 +1428,20 @@ class Site < Sequel::Model 'https' end - def uri - "#{default_schema}://#{host}" - end + def uri(path=nil) + uri = "#{default_schema}://#{host}" - def file_uri(path) - path = '/' + path unless path[0] == '/' - uri + (path =~ ROOT_INDEX_HTML_REGEX ? '/' : path) + return uri unless path + + path = '' if path == '/' || path =~ ROOT_INDEX_HTML_REGEX + path = path.sub(%r{^/}, '').sub(%r{/index\.html$}, '/').sub(/\.html$/, '') + + unless path.empty? + escaped_path = Rack::Utils.escape_path(path).gsub('?', '%3F') + uri += "/#{escaped_path}" + end + + uri end def title diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 1d81535b..786dada7 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -408,11 +408,11 @@ describe 'site_files' do _(File.exists?(@site.files_path('te[s]t.jpg'))).must_equal true end - it 'scrubs question marks' do + it 'works with question marks' do uploaded_file = Rack::Test::UploadedFile.new('./tests/files/te[s]t.jpg', 'image/jpeg') upload 'te?st.jpg' => uploaded_file _(last_response.body).must_match /successfully uploaded/i - _(File.exists?(@site.files_path('test.jpg'))).must_equal true + _(File.exists?(@site.files_path('te?st.jpg'))).must_equal true end it 'sets site changed to false if index is empty' do diff --git a/views/dashboard/files.erb b/views/dashboard/files.erb index 6d7cd3e9..b207e719 100644 --- a/views/dashboard/files.erb +++ b/views/dashboard/files.erb @@ -98,7 +98,7 @@ <% if file[:is_directory] %> <% else %> - + <% end %>