├── .gitignore ├── LICENSE ├── files └── .gitkeep ├── pastebin.cr └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | files/* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to -------------------------------------------------------------------------------- /files/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlvzk/pastebin-cr/3fb6218e3bbd86dadcd906ed7ba43d4288596643/files/.gitkeep -------------------------------------------------------------------------------- /pastebin.cr: -------------------------------------------------------------------------------- 1 | require "http" 2 | require "file" 3 | require "random/secure" 4 | 5 | server = HTTP::Server.new do |context| 6 | if context.request.method == "POST" 7 | next if context.request.content_length.not_nil! > 256 * 1024 * 1024 8 | HTTP::FormData.parse(context.request) do |part| 9 | next if part.name != "file" 10 | 11 | generated_name = Random::Secure.urlsafe_base64 6 12 | File.open("files/#{generated_name}", "w") { |f| IO.copy(part.body, f) } 13 | context.response << generated_name 14 | end 15 | elsif context.request.method == "GET" 16 | path = "files/#{context.request.path.split('/')[1]}" 17 | File.open(path) { |f| IO.copy f, context.response.output } 18 | end 19 | end 20 | 21 | server.bind_tcp 3000 22 | server.listen 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 19 LOC pastebin in Crystal 2 | 3 | To try it out send a POST request with a form containing a `file`, the response body is the pastebin ID. 4 | 5 | Sample `curl`: 6 | ```sh 7 | $ curl -i -X POST -F file=@x.txt localhost:3000 8 | HTTP/1.1 200 OK 9 | Connection: keep-alive 10 | Content-Length: 8 11 | 12 | slaa7MN5 13 | ``` 14 | 15 | To read it, send a `GET` request to localhost:3000/slaa7MN5 16 | --------------------------------------------------------------------------------