undo auto-lint changes from my editor

This commit is contained in:
parkedhampster 2024-03-26 11:41:06 -04:00
parent 25ca2a879b
commit 26b6f6f6c8
2 changed files with 87 additions and 69 deletions

View file

@ -2,7 +2,7 @@ require 'base64'
get '/api' do
@title = 'Developers API'
erb :api
erb :'api'
end
post '/api/upload_hash' do
@ -20,10 +20,10 @@ get '/api/list' do
files = []
file_list = if params[:path].nil? || params[:path].empty?
current_site.site_files
if params[:path].nil? || params[:path].empty?
file_list = current_site.site_files
else
current_site.file_list params[:path]
file_list = current_site.file_list params[:path]
end
file_list.each do |file|
@ -36,7 +36,7 @@ get '/api/list' do
files << new_file
end
files.each { |f| f[:path].sub!(%r{^/}, '') }
files.each {|f| f[:path].sub!(/^\//, '')}
api_success files: files
end
@ -46,10 +46,12 @@ def extract_files(params, files = [])
if params.is_a?(Array)
params.each do |item|
# Call extract_files on each item if it's an Array or Hash to handle nested structures
extract_files(item, files) if item.is_a?(Array) || item.is_a?(Hash)
if item.is_a?(Array) || item.is_a?(Hash)
extract_files(item, files)
end
end
elsif params.is_a?(Hash)
params.each do |_key, value|
params.each do |key, value|
# If the value is a Hash and contains a :tempfile key, it's considered an uploaded file.
if value.is_a?(Hash) && value.has_key?(:tempfile) && !value[:tempfile].nil?
files << {filename: value[:name], tempfile: value[:tempfile]}
@ -76,15 +78,17 @@ post '/api/upload' do
require_api_credentials
files = extract_files params
unless params[:username].blank?
if !params[:username].blank?
site = Site[username: params[:username]]
api_error 400, 'site_not_found', 'could not find site' if site.nil? || site.is_deleted
if site.nil? || site.is_deleted
api_error 400, 'site_not_found', "could not find site"
end
if site.owned_by?(current_site)
@_site = site
else
api_error 400, 'site_not_allowed', 'not allowed to change this site with your current logged in site'
api_error 400, 'site_not_allowed', "not allowed to change this site with your current logged in site"
end
end
@ -97,17 +101,17 @@ post '/api/upload' do
end
if current_site.too_many_files?(files.length)
api_error 400, 'too_many_files',
"cannot exceed the maximum site files limit (#{current_site.plan_feature(:maximum_site_files)})"
api_error 400, 'too_many_files', "cannot exceed the maximum site files limit (#{current_site.plan_feature(:maximum_site_files)})"
end
files.each do |file|
unless current_site.okay_to_upload?(file)
api_error 400, 'invalid_file_type',
"#{file[:filename]} is not an allowed file type for free sites, supporter required"
if !current_site.okay_to_upload?(file)
api_error 400, 'invalid_file_type', "#{file[:filename]} is not an allowed file type for free sites, supporter required"
end
api_error 400, 'directory_exists', "#{file[:filename]} being used by a directory" if File.directory? file[:filename]
if File.directory? file[:filename]
api_error 400, 'directory_exists', "#{file[:filename]} being used by a directory"
end
if current_site.file_size_too_large? file[:tempfile].size
api_error 400, 'file_too_large' "#{file[:filename]} is too large"
@ -118,7 +122,7 @@ post '/api/upload' do
end
if SiteFile.name_too_long? file[:filename]
api_error 400, 'file_name_too_long', "#{file[:filename]} filename is too long. (Longer than 255 characters)"
api_error 400, 'file_name_too_long', "#{file[:filename]} filename is too long (exceeds 255 characters)"
end
end
@ -129,21 +133,24 @@ end
post '/api/rename' do
require_api_credentials
if params[:path].blank? || params[:new_path].blank?
api_error 400, 'missing_arguments',
'you must provide path and new_path'
end
api_error 400, 'missing_arguments', 'you must provide path and new_path' if params[:path].blank? || params[:new_path].blank?
path = current_site.scrubbed_path params[:path]
new_path = current_site.scrubbed_path params[:new_path]
api_error 400, 'bad_path', "#{path} is not a valid path, cancelled renaming" unless path.is_a?(String)
unless path.is_a?(String)
api_error 400, 'bad_path', "#{path} is not a valid path, cancelled renaming"
end
api_error 400, 'bad_new_path', "#{new_path} is not a valid new_path, cancelled renaming" unless new_path.is_a?(String)
unless new_path.is_a?(String)
api_error 400, 'bad_new_path', "#{new_path} is not a valid new_path, cancelled renaming"
end
site_file = current_site.site_files.select {|sf| sf.path == path}.first
api_error 400, 'missing_file', "could not find #{path}" if site_file.nil?
if site_file.nil?
api_error 400, 'missing_file', "could not find #{path}"
end
res = site_file.rename new_path
@ -157,24 +164,23 @@ end
post '/api/delete' do
require_api_credentials
if params[:filenames].nil? || params[:filenames].empty?
api_error 400, 'missing_filenames',
'you must provide files to delete'
end
api_error 400, 'missing_filenames', 'you must provide files to delete' if params[:filenames].nil? || params[:filenames].empty?
paths = []
params[:filenames].each do |path|
api_error 400, 'bad_filename', "#{path} is not a valid filename, canceled deleting" unless path.is_a?(String)
unless path.is_a?(String)
api_error 400, 'bad_filename', "#{path} is not a valid filename, canceled deleting"
end
if current_site.files_path(path) == current_site.files_path
api_error 400, 'cannot_delete_site_directory', 'cannot delete the root directory of the site'
end
unless current_site.file_exists?(path)
if !current_site.file_exists?(path)
api_error 400, 'missing_files', "#{path} was not found on your site, canceled deleting"
end
if ['index.html', '/index.html'].include?(path)
if path == 'index.html' || path == '/index.html'
api_error 400, 'cannot_delete_index', 'you cannot delete your index.html file, canceled deleting'
end
@ -234,10 +240,7 @@ def require_api_credentials
if !request.env['HTTP_AUTHORIZATION'].nil?
init_api_credentials
if email_not_validated?
api_error(403, 'email_not_validated',
'you need to validate your email address before using the API')
end
api_error(403, 'email_not_validated', 'you need to validate your email address before using the API') if email_not_validated?
else
api_error_invalid_auth
end
@ -253,7 +256,7 @@ def init_api_credentials
else
user, pass = Base64.decode64(auth.match(/Basic (.+)/)[1]).split(':')
end
rescue StandardError
rescue
api_error_invalid_auth
end
@ -265,7 +268,9 @@ def init_api_credentials
api_error_invalid_auth
end
api_error_invalid_auth if site.nil? || site.is_banned || site.is_deleted
if site.nil? || site.is_banned || site.is_deleted
api_error_invalid_auth
end
DB['update sites set api_calls=api_calls+1 where id=?', site.id].first

View file

@ -13,27 +13,29 @@ class SiteFile < Sequel::Model
def self.path_too_long?(filename)
return true if filename.length > FILE_PATH_CHARACTER_LIMIT
false
end
def self.name_too_long?(filename)
return true if filename.length > FILE_NAME_CHARACTER_LIMIT
false
end
def before_destroy
if is_directory
site.site_files_dataset.where(path: %r{^#{Regexp.quote path}/}, is_directory: true).all.each do |site_file|
site.site_files_dataset.where(path: /^#{Regexp.quote path}\//, is_directory: true).all.each do |site_file|
begin
site_file.destroy
rescue Sequel::NoExistingObject
end
end
site.site_files_dataset.where(path: %r{^#{Regexp.quote path}/}, is_directory: false).all.each do |site_file|
site.site_files_dataset.where(path: /^#{Regexp.quote path}\//, is_directory: false).all.each do |site_file|
begin
site_file.destroy
rescue Sequel::NoExistingObject
end
end
begin
FileUtils.remove_dir site.files_path(path)
@ -56,26 +58,36 @@ class SiteFile < Sequel::Model
end
def rename(new_path)
current_path = path
current_path = self.path
new_path = site.scrubbed_path new_path
return false, 'new path too long' if new_path.length > FILE_PATH_CHARACTER_LIMIT
if new_path.length > FILE_PATH_CHARACTER_LIMIT
return false, 'new path too long'
end
return false, 'new filename too long' if File.basename(new_path).length > FILE_NAME_CHARACTER_LIMIT
if File.basename(new_path).length > FILE_NAME_CHARACTER_LIMIT
return false, 'new filename too long'
end
return false, 'cannot rename to empty path' if new_path == ''
if new_path == ''
return false, 'cannot rename to empty path'
end
return false, 'cannot rename or move root index.html' if current_path == 'index.html'
if current_path == 'index.html'
return false, 'cannot rename or move root index.html'
end
if site.site_files.select {|sf| sf.path == new_path}.length > 0
return false, "#{is_directory ? 'directory' : 'file'} already exists"
end
if is_directory
return false, 'directory name cannot end with .htm or .html' if new_path.match(/\.html?$/)
if new_path.match(/\.html?$/)
return false, 'directory name cannot end with .htm or .html'
end
else # a file
mime_type = Magic.guess_file_mime_type site.files_path(path)
mime_type = Magic.guess_file_mime_type site.files_path(self.path)
extname = File.extname new_path
unless site.supporter? || site.class.valid_file_mime_type_and_ext?(mime_type, extname)
@ -89,7 +101,6 @@ class SiteFile < Sequel::Model
site.generate_thumbnail_or_screenshot new_path
rescue Errno::ENOENT => e
return false, 'destination directory does not exist' if e.message =~ /No such file or directory/i
raise e
rescue ArgumentError => e
raise e unless e.message =~ /same file/
@ -97,13 +108,13 @@ class SiteFile < Sequel::Model
DB.transaction do
self.path = new_path
save_changes
self.save_changes
if is_directory
site_files_in_dir = site.site_files.select { |sf| sf.path =~ %r{^#{current_path}/} }
site_files_in_dir = site.site_files.select {|sf| sf.path =~ /^#{current_path}\//}
site_files_in_dir.each do |site_file|
original_site_file_path = site_file.path
site_file.path = site_file.path.gsub(%r{^#{current_path}/}, "#{new_path}\/")
site_file.path = site_file.path.gsub(/^#{current_path}\//, "#{new_path}\/")
site_file.save_changes
site.delete_thumbnail_or_screenshot original_site_file_path
site.generate_thumbnail_or_screenshot site_file.path
@ -116,12 +127,14 @@ class SiteFile < Sequel::Model
end
end
[true, nil]
return true, nil
end
def after_destroy
super
DB['update sites set space_used=space_used-? where id=?', size, site_id].first unless is_directory
unless is_directory
DB['update sites set space_used=space_used-? where id=?', size, site_id].first
end
site.purge_cache site.files_path(path)
SiteChangeFile.filter(site_id: site_id, filename: path).delete