require 'sinatra' require 'json'
post '/upload' do unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename]) return { error: 'Please select a file to upload' }.to_json end
target_dir = './uploads' Dir.mkdir(target_dir) unless Dir.exist?(target_dir) target_path = File.join(target_dir, name) File.open(target_path, 'wb') { |f| f.write(tmpfile.read) }
{ success: true, message: "File '#{name}' has been uploaded successfully.", file: { name: name, size: File.size(target_path), url: "/uploads/#{name}" } }.to_json end
main.rb595 chars7 lines
gistlibby LogSnag