about summary refs log tree commit diff
path: root/src/media
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-05-10 00:42:01 +0300
committerVika <vika@fireburn.ru>2022-05-10 00:42:01 +0300
commitd95ae14555b421eb62f9ffaeb7c869c540961fa4 (patch)
tree59a9ba5911977620c62ebbfec7f6a6c791d0219a /src/media
parent974613c21479300284922e851447a51afb40057a (diff)
downloadkittybox-d95ae14555b421eb62f9ffaeb7c869c540961fa4.tar.zst
media: move to separate subtree
This will ease future extraction of the media endpoint to a separate
crate. This is highly desirable since it will allow Kittybox's media
endpoint to be used separately in instances where a standalone media
endpoint is desirable (e.g. custom solutions using my code to polyfill
for desired functionality that is undesirable to implement by oneself)
Diffstat (limited to 'src/media')
-rw-r--r--src/media/mod.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/media/mod.rs b/src/media/mod.rs
new file mode 100644
index 0000000..0d46e0c
--- /dev/null
+++ b/src/media/mod.rs
@@ -0,0 +1,46 @@
+use futures_util::StreamExt;
+use bytes::buf::Buf;
+use warp::{Filter, Rejection, Reply, multipart::{FormData, Part}};
+
+pub fn query() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
+    warp::get()
+        .and(crate::util::require_host())
+        .map(|host| "media endpoint query...")
+}
+
+pub fn options() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
+    warp::options()
+        .map(|| warp::reply::json::<Option<()>>(&None))
+        .with(warp::reply::with::header("Allow", "GET, POST"))
+}
+
+pub fn upload() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
+    warp::post()
+        .and(crate::util::require_host())
+        .and(warp::multipart::form().max_length(1024*1024*150/*mb*/))
+        .and_then(|host, mut form: FormData| async move {
+            // TODO get rid of the double unwrap() here
+            let file: Part = form.next().await.unwrap().unwrap();
+            log::debug!("Uploaded: {:?}, type: {:?}", file.filename(), file.content_type());
+
+            let mut data = file.stream();
+            while let Some(buf) = data.next().await {
+                // TODO save it into a file
+                log::debug!("buffer length: {:?}", buf.map(|b| b.remaining()));
+            }
+            Ok::<_, warp::Rejection>(warp::reply::with_header(
+                warp::reply::with_status(
+                    "",
+                    warp::http::StatusCode::CREATED
+                ),
+                "Location",
+                "./awoo.png"
+            ))
+        })
+}
+
+pub fn media() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
+    upload()
+        .or(query())
+        .or(options())
+}