Created
May 29, 2017 17:58
-
-
Save felixfong227/8f83f848821831780da9a2395719ee40 to your computer and use it in GitHub Desktop.
A web server that uploads file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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