file uploading for dashboard

This commit is contained in:
Kyle Drake 2014-04-19 19:52:46 -07:00
parent 4b19f3c091
commit 1b7430f1db
4 changed files with 150 additions and 166 deletions

49
app.rb
View file

@ -122,13 +122,13 @@ post '/plan/end' do
redirect '/plan' unless current_site.supporter? && !current_site.plan_ended
recaptcha_is_valid = ENV['RACK_ENV'] == 'test' || recaptcha_valid?
if !recaptcha_is_valid
@error = 'Recaptcha was filled out incorrectly, please try re-entering.'
@plan_name = get_plan_name current_site.stripe_customer_id
halt erb :'plan/end'
end
customer = Stripe::Customer.retrieve current_site.stripe_customer_id
subscriptions = customer.subscriptions.all
@ -389,33 +389,50 @@ get '/site_files/upload' do
slim :'site_files/upload'
end
def file_upload_response(error=nil)
http_error_code = 406
if params[:from_button]
if error
@error = error
halt 200, erb(:'dashboard')
else
redirect '/dashboard'
end
else
halt http_error_code, error if error
halt 200, 'File(s) successfully uploaded.'
end
end
post '/site_files/upload' do
require_login
@errors = []
http_error_code = 406
if params[:newfile] == '' || params[:newfile].nil?
@errors << 'You must select a file to upload.'
halt http_error_code, 'Did not receive file upload.'
params[:files].each do |file|
if current_site.file_size_too_large? file[:tempfile].size
file_upload_response "#{file[:filename]} is too large, upload cancelled."
end
if !Site.valid_file_type? file
file_upload_response "#{file[:filename]}: file type is not allowed on Neocities, upload cancelled."
end
end
if current_site.file_size_too_large? params[:newfile][:tempfile].size
@errors << 'File size must be smaller than available space.'
halt http_error_code, 'File size must be smaller than available space.'
uploaded_size = params[:files].collect {|f| f[:tempfile].size}.inject{|sum,x| sum + x }
if current_site.file_size_too_large? uploaded_size
file_upload_response "File(s) do not fit in your available space, upload cancelled."
end
unless Site.valid_file_type? params[:newfile]
@errors << 'File must me one of the following: HTML, Text, Image (JPG PNG GIF JPEG SVG), JS, CSS, Markdown.'
halt http_error_code, 'File type is not supported.' # slim(:'site_files/new')
params[:files].each do |file|
current_site.store_file Site.sanitize_filename(file[:filename]), file[:tempfile]
end
sanitized_filename = params[:newfile][:filename].gsub(/[^a-zA-Z0-9_\-.]/, '')
current_site.store_file sanitized_filename, params[:newfile][:tempfile]
current_site.increment_changed_count
flash[:success] = "Successfully uploaded file #{sanitized_filename}."
redirect '/dashboard'
file_upload_response
end
post '/site_files/delete' do