Skip to content

Instantly share code, notes, and snippets.

@adam12
Created November 6, 2012 21:28
Show Gist options
  • Save adam12/4027696 to your computer and use it in GitHub Desktop.
Save adam12/4027696 to your computer and use it in GitHub Desktop.
class Application < Sinatra::Base
enable :inline_templates
get '/:folder' do |folder|
if File.directory? 'uploads/' + folder
@files = Dir.glob('uploads/' + folder + '/*')
@folder = folder
erb :index
else
'No files'
end
end
get '/:folder/:file' do |folder, file|
if File.directory?('uploads/' + folder) && File.exist?('uploads/' + folder + '/' + file)
send_file 'uploads/' + folder + '/' + file
else
'No such file'
end
end
post '/:folder' do |folder|
if File.directory? 'uploads/' + folder
File.open 'uploads/' + folder + '/' + params['myfile'][:filename], 'w' do |f|
f.write params['myfile'][:tempfile].read
end
redirect to('/' + folder)
# 'The file was successfully uploaded!'
else
"Unable to upload to #{folder}"
end
end
end
__END__
@@ index
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Files</h1>
<table>
<thead>
<tr>
<th>Filename</th>
</tr>
</thead>
<tbody>
<% @files.each do |file| %>
<tr>
<td><a href="<%= @folder %>/<%= File.basename file %>"><%= File.basename file %></a></td>
</tr>
<% end %>
</tbody>
</table>
<h1>Upload new file</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="myfile"><br>
<input type="submit" value="Upload!">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment