# server.rb require 'sinatra' set :public_folder, 'public' get '/' do html = <<~HTML Photobomb

Photobomb

Welcome to your new Photobomb franchise!

You will soon be making an amazing income selling premium photographic gifts.

This state of-the-art web application is your gateway to this fantastic new life. Your wish is its command.

To get started, please click here! (the credentials are in your welcome pack).

If you have any problems with your printer, please call our Technical Support team on 4 4283 77468377.

HTML content_type :html return html end get '/printer' do images = '' checked = ' checked="checked" ' Dir.glob('public/ui_images/*.jpg') do |jpg_filename| img_src = jpg_filename.sub('public/', '') img_name = jpg_filename.sub('public/ui_images/', '') images += '' checked = '' end html = <<~HTML Photobomb

Photobomb

Select an image

#{images}
HTML content_type :html return html end post '/printer' do photo = params[:photo] filetype = params[:filetype] dimensions = params[:dimensions] # handle inputs if photo.match(/\.{2}|\//) halt 500, 'Invalid photo.' end if !FileTest.exist?( "source_images/" + photo ) halt 500, 'Source photo does not exist.' end if !filetype.match(/^(png|jpg)/) halt 500, 'Invalid filetype.' end if !dimensions.match(/^[0-9]+x[0-9]+$/) halt 500, 'Invalid dimensions.' end case filetype when 'png' content_type 'image/png' when 'jpg' content_type 'image/jpeg' end filename = photo.sub('.jpg', '') + '_' + dimensions + '.' + filetype response['Content-Disposition'] = "attachment; filename=#{filename}" if !File.exists?('resized_images/' + filename) command = 'convert source_images/' + photo + ' -resize ' + dimensions + ' resized_images/' + filename puts "Executing: #{command}" system(command) else puts "File already exists." end if File.exists?('resized_images/' + filename) halt 200, {}, IO.read('resized_images/' + filename) end #message = 'Failed to generate a copy of ' + photo + ' resized to ' + dimensions + ' with filetype ' + filetype message = 'Failed to generate a copy of ' + photo halt 500, message end