fix up file uris/uri path code

This commit is contained in:
Kyle Drake 2024-09-15 14:18:35 -05:00
parent 1794a4bcba
commit dee9128cb3
4 changed files with 23 additions and 13 deletions

View file

@ -139,12 +139,13 @@ desc 'generate_sitemap'
task :generate_sitemap => [:environment] do task :generate_sitemap => [:environment] do
sorted_sites = {} sorted_sites = {}
# We pop off array, so highest scores go last.
sites = Site. sites = Site.
select(:id, :username, :updated_at, :profile_enabled). select(:id, :username, :updated_at, :profile_enabled).
where(site_changed: true). where(site_changed: true).
exclude(updated_at: nil). exclude(updated_at: nil).
exclude(is_deleted: true). exclude(is_deleted: true).
order(:follow_count, :updated_at). order(:score).
all all
site_files = [] site_files = []
@ -152,13 +153,13 @@ task :generate_sitemap => [:environment] do
sites.each do |site| sites.each do |site|
site.site_files_dataset.exclude(path: 'not_found.html').where(path: /\.html?$/).all.each do |site_file| 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 priority = 0.5
else else
priority = 0.4 priority = 0.4
end 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
end end

View file

@ -1244,8 +1244,10 @@ class Site < Sequel::Model
def file_list(path='') 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| 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 extname = File.extname file_path
path = file_path.gsub(base_files_path+'/', '')
file = { file = {
path: file_path.gsub(base_files_path+'/', ''), path: path,
uri: uri(path),
name: File.basename(file_path), name: File.basename(file_path),
ext: extname.gsub('.', ''), ext: extname.gsub('.', ''),
is_directory: File.directory?(file_path), is_directory: File.directory?(file_path),
@ -1426,13 +1428,20 @@ class Site < Sequel::Model
'https' 'https'
end end
def uri def uri(path=nil)
"#{default_schema}://#{host}" uri = "#{default_schema}://#{host}"
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 end
def file_uri(path) uri
path = '/' + path unless path[0] == '/'
uri + (path =~ ROOT_INDEX_HTML_REGEX ? '/' : path)
end end
def title def title

View file

@ -408,11 +408,11 @@ describe 'site_files' do
_(File.exists?(@site.files_path('te[s]t.jpg'))).must_equal true _(File.exists?(@site.files_path('te[s]t.jpg'))).must_equal true
end 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') uploaded_file = Rack::Test::UploadedFile.new('./tests/files/te[s]t.jpg', 'image/jpeg')
upload 'te?st.jpg' => uploaded_file upload 'te?st.jpg' => uploaded_file
_(last_response.body).must_match /successfully uploaded/i _(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 end
it 'sets site changed to false if index is empty' do it 'sets site changed to false if index is empty' do

View file

@ -98,7 +98,7 @@
<% if file[:is_directory] %> <% if file[:is_directory] %>
<a class="link-overlay" href="?dir=<%= Rack::Utils.escape file[:path] %>" title="View <%= file[:path] %>"></a> <a class="link-overlay" href="?dir=<%= Rack::Utils.escape file[:path] %>" title="View <%= file[:path] %>"></a>
<% else %> <% else %>
<a class="link-overlay" href="<%= current_site.file_uri Rack::Utils.escape(file[:path]) %>" title="View <%= file[:path] == '/index.html' ? 'your site index' : file[:path] %>" target="_blank"></a> <a class="link-overlay" href="<%= file[:uri] %>" title="View <%= file[:path] %>" target="_blank"></a>
<% end %> <% end %>
</div> </div>
</div> </div>