From 7a31ab5028e5996a67629cedb735865f024db240 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 29 Mar 2023 17:29:19 +0200 Subject: [PATCH] Added uploads service stub --- gymboard-uploads/.gitignore | 16 ++++++++++++++++ gymboard-uploads/README.md | 20 ++++++++++++++++++++ gymboard-uploads/dub.json | 13 +++++++++++++ gymboard-uploads/dub.selections.json | 8 ++++++++ gymboard-uploads/source/app.d | 24 ++++++++++++++++++++++++ gymboard-uploads/source/handlers.d | 25 +++++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 gymboard-uploads/.gitignore create mode 100644 gymboard-uploads/README.md create mode 100644 gymboard-uploads/dub.json create mode 100644 gymboard-uploads/dub.selections.json create mode 100644 gymboard-uploads/source/app.d create mode 100644 gymboard-uploads/source/handlers.d diff --git a/gymboard-uploads/.gitignore b/gymboard-uploads/.gitignore new file mode 100644 index 0000000..f93d37f --- /dev/null +++ b/gymboard-uploads/.gitignore @@ -0,0 +1,16 @@ +.dub +docs.json +__dummy.html +docs/ +/gymboard-uploads +gymboard-uploads.so +gymboard-uploads.dylib +gymboard-uploads.dll +gymboard-uploads.a +gymboard-uploads.lib +gymboard-uploads-test-* +*.exe +*.pdb +*.o +*.obj +*.lst diff --git a/gymboard-uploads/README.md b/gymboard-uploads/README.md new file mode 100644 index 0000000..18a9670 --- /dev/null +++ b/gymboard-uploads/README.md @@ -0,0 +1,20 @@ +# Gymboard Uploads + +A service for handling large file uploads and processing. This service manages user-submitted video files, processing of the video files, and rapidly serving the raw file content. + +## Upload Logical Flow + +When a user is creating a new Gymboard submission, they need to upload their video, which must be processed to compress and prepare it for web usage. The following series of steps describe how this is done. + +1. User uploads raw video to **gymboard-uploads**, receives an `upload-id`. +2. User posts the submission, including the `upload-id`, to **gymboard-api**. +3. **gymboard-api** validates the submission, then sends a request to **gymboard-uploads** to start processing the video identified by `upload-id`, and marks the submission as _processing_. +4. **gymboard-uploads** processes the video, and upon completion, sends a request to **gymboard-api** indicating that the processed video is available at `video-url`. +5. **gymboard-api** updates the user's submission to include the `video-url`, and marks the submission as no longer _processing_. + +Therefore, **gymboard-uploads** is responsible for the following actions: + +- Receiving raw video uploads from users and storing them temporarily until **gymboard-api** requests that we start processing, or they'll be discarded. +- Processing of raw videos to reduce file size and optimize for web. This will most likely be done using FFMPEG in the backend. +- Reporting the status of video processing for recently uploaded videos. +- Serving processed video files. diff --git a/gymboard-uploads/dub.json b/gymboard-uploads/dub.json new file mode 100644 index 0000000..8568a98 --- /dev/null +++ b/gymboard-uploads/dub.json @@ -0,0 +1,13 @@ +{ + "authors": [ + "Andrew Lalis" + ], + "copyright": "Copyright © 2023, Andrew Lalis", + "dependencies": { + "handy-httpd": "~>5.7.0", + "slf4d": "~>2.1.1" + }, + "description": "Service for handling Gymboard file uploads.", + "license": "proprietary", + "name": "gymboard-uploads" +} \ No newline at end of file diff --git a/gymboard-uploads/dub.selections.json b/gymboard-uploads/dub.selections.json new file mode 100644 index 0000000..349e857 --- /dev/null +++ b/gymboard-uploads/dub.selections.json @@ -0,0 +1,8 @@ +{ + "fileVersion": 1, + "versions": { + "handy-httpd": "5.7.0", + "httparsed": "1.2.1", + "slf4d": "2.1.1" + } +} diff --git a/gymboard-uploads/source/app.d b/gymboard-uploads/source/app.d new file mode 100644 index 0000000..addb69c --- /dev/null +++ b/gymboard-uploads/source/app.d @@ -0,0 +1,24 @@ +import std.stdio; +import handy_httpd; +import handy_httpd.handlers.path_delegating_handler; + +import handlers; + +void main() { + PathDelegatingHandler pathHandler = new PathDelegatingHandler(); + pathHandler.addMapping("GET", "/status", (ref HttpRequestContext ctx) { + ctx.response.writeBodyString("online"); + }); + pathHandler.addMapping("POST", "/uploads", new VideoUploadHandler()); + + HttpServer server = new HttpServer(pathHandler, getServerConfig()); + server.start(); +} + +private ServerConfig getServerConfig() { + ServerConfig serverConfig = ServerConfig.defaultValues(); + serverConfig.port = 8085; + serverConfig.workerPoolSize = 10; + serverConfig.reuseAddress = true; + return serverConfig; +} diff --git a/gymboard-uploads/source/handlers.d b/gymboard-uploads/source/handlers.d new file mode 100644 index 0000000..151c0b1 --- /dev/null +++ b/gymboard-uploads/source/handlers.d @@ -0,0 +1,25 @@ +module handlers; + +import handy_httpd; +import std.conv : to; + +const ulong MAX_UPLOAD_SIZE = 1024 * 1024 * 1024; + +class VideoUploadHandler : HttpRequestHandler { + public void handle(ref HttpRequestContext ctx) { + if ("Content-Length" !in ctx.request.headers) { + ctx.response.status = 411; + ctx.response.statusText = "Length Required"; + return; + } + + ulong contentLength = ctx.request.headers["Content-Length"].to!ulong; + if (contentLength == 0 || contentLength > MAX_UPLOAD_SIZE) { + ctx.response.status = 413; + ctx.response.statusText = "Payload Too Large"; + return; + } + + // TODO: Implement this! + } +} \ No newline at end of file