Skip to content

Instantly share code, notes, and snippets.

@felixfong227
Created May 29, 2017 17:58
Show Gist options
  • Save felixfong227/8f83f848821831780da9a2395719ee40 to your computer and use it in GitHub Desktop.
Save felixfong227/8f83f848821831780da9a2395719ee40 to your computer and use it in GitHub Desktop.
A web server that uploads file
require "kemal"
sha256 = OpenSSL::Digest.new("sha256")
get "/:name" do |env|
name = env.params.url["name"]
render "public/index.ecr"
end
post "/upload" do |env|
file = env.params.files["image1"]
if !file.filename.is_a?(String)
p "No filename included in upload"
else
new_file_id = sha256.update("#{SecureRandom.uuid}#{file.filename}")
file_path = ::File.join("#{Kemal.config.public_folder}/uploads/#{new_file_id}#{file.filename}")
File.write(file_path, file.tmpfile)
end
end
Kemal.run
name: webserver
version: 0.1.0
authors:
- felixfong227 <moongod101@hotmail.com>
dependencies:
kemal:
github: kemalcr/kemal
branch: master
description: |
Just a testing web server
license: MIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment