From 164811388fc12782ed351946d332ef736ea2a9d2 Mon Sep 17 00:00:00 2001 From: NotNite Date: Fri, 28 Apr 2023 13:19:02 -0400 Subject: [PATCH] maintenance --- .gitignore | 2 + Cargo.lock | 42 +++++------ Cargo.toml | 23 +++++- README.md | 73 +++++++++++++++++++ client/Cargo.lock | 7 -- client/Cargo.toml | 8 -- client/src/main.rs | 3 - .../20230304225311_init.sql | 0 server/Cargo.toml | 21 ------ {server/src => src}/auth.rs | 0 {server/src => src}/main.rs | 0 {server/src => src}/routes/delete.rs | 0 {server/src => src}/routes/i.rs | 0 {server/src => src}/routes/mod.rs | 0 {server/src => src}/routes/new_key.rs | 0 {server/src => src}/routes/upload.rs | 0 {server/src => src}/state.rs | 0 {server/src => src}/utils.rs | 0 upload.sh | 15 ++++ 19 files changed, 130 insertions(+), 64 deletions(-) delete mode 100644 client/Cargo.lock delete mode 100644 client/Cargo.toml delete mode 100644 client/src/main.rs rename {server/migrations => migrations}/20230304225311_init.sql (100%) delete mode 100644 server/Cargo.toml rename {server/src => src}/auth.rs (100%) rename {server/src => src}/main.rs (100%) rename {server/src => src}/routes/delete.rs (100%) rename {server/src => src}/routes/i.rs (100%) rename {server/src => src}/routes/mod.rs (100%) rename {server/src => src}/routes/new_key.rs (100%) rename {server/src => src}/routes/upload.rs (100%) rename {server/src => src}/state.rs (100%) rename {server/src => src}/utils.rs (100%) create mode 100644 upload.sh diff --git a/.gitignore b/.gitignore index 2626d13..8f9400a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ files/ .env u.db* + +upload-local.sh diff --git a/Cargo.lock b/Cargo.lock index 9a85b17..4e766fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,10 +154,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "client" -version = "0.1.0" - [[package]] name = "cpufeatures" version = "0.2.5" @@ -964,25 +960,6 @@ dependencies = [ "serde", ] -[[package]] -name = "server" -version = "0.1.0" -dependencies = [ - "anyhow", - "async-trait", - "axum", - "dotenvy", - "hyper", - "mime_guess", - "nanoid", - "serde", - "sqlx", - "tokio", - "tokio-stream", - "tower", - "tower-http", -] - [[package]] name = "sha1" version = "0.10.5" @@ -1366,6 +1343,25 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "u" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "axum", + "dotenvy", + "hyper", + "mime_guess", + "nanoid", + "serde", + "sqlx", + "tokio", + "tokio-stream", + "tower", + "tower-http", +] + [[package]] name = "unicase" version = "2.6.0" diff --git a/Cargo.toml b/Cargo.toml index abeddfe..2f7fd1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,21 @@ -[workspace] -members = ["client", "server"] +[package] +name = "u" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.69" +async-trait = "0.1.65" +axum = { version = "0.6.10", features = ["multipart", "headers"] } +dotenvy = "0.15.6" +hyper = "0.14.24" +mime_guess = "2.0.4" +nanoid = "0.4.0" +serde = { version = "1.0.152", features = ["derive"] } +sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls"] } +tokio = { version = "1.26.0", features = ["full"] } +tokio-stream = { version = "0.1.12", features = ["net"] } +tower = "0.4.13" +tower-http = { version = "0.4.0", features = ["fs"] } diff --git a/README.md b/README.md index 4f5195e..be0dde5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,76 @@ # u blazingly-fast super-concurrent webscale file server + +## setup + +requirements: + +- git +- rust toolchain + +```sh +git clone https://git.n2.pm/NotNite/u.git +cd u + +cat <> .env +DATABASE_URL="sqlite:/somewhere/u.db" +U_PORT="8075" +U_FILES_DIR="/somewhere/files" +EOF + +cargo build --release +``` + +on first run, it'll print out the admin key - keep that safe. you need it to create more API keys. + +while losing the database isn't the end of the world (only contains API keys and revocation keys for files), obviously keep your files directory safe. + +## setup (client) + +requirements: + +- api key for an existing u server +- coreutils, `curl`, `jq`, `xclip` + +```sh +export U_API_KEY="..." +export U_HOST="https://.../" +export U_REVOCATION_KEY_PATH="$HOME/.u_revocation_keys" + +./upload.sh /path/to/file +``` + +## using u + +examples are with curl - you can (obviously) use any HTTP client. + +### creating a new API key + +```sh +curl -X POST -H "Authorization: $U_API_KEY" "$U_HOST/api/new_key" +``` + +requires an admin key to execute. you can give this API key to someone else so they can use your instance. + +you can't modify API keys (delete or make admin) through the API - you'll have to open your database in your favorite sqlite client and edit the row manually. + +### uploading a file + +```sh +curl -F file=@/path/to/file -H "Authorization: $U_API_KEY" "$U_HOST/api/upload" +``` + +the `upload.sh` in this repository is just a wrapper around this curl command. + +### deleting a file + +```sh +curl -X DELETE "$U_HOST/i/$file_id?revocation_key=$revocation_key" +``` + +`file_id` and `revocation_key` are the values returned by the upload endpoint. if using `upload.sh`, the revocation key is stored in `$U_REVOCATION_KEY_PATH`. + +### accessing files + +files can be accessed with the URL `$U_HOST/i/$file_id`. you can add a file extension to the path if you want the server to return a specific mime type. diff --git a/client/Cargo.lock b/client/Cargo.lock deleted file mode 100644 index b69a06d..0000000 --- a/client/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "client" -version = "0.1.0" diff --git a/client/Cargo.toml b/client/Cargo.toml deleted file mode 100644 index 729587b..0000000 --- a/client/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "client" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/client/src/main.rs b/client/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/client/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/server/migrations/20230304225311_init.sql b/migrations/20230304225311_init.sql similarity index 100% rename from server/migrations/20230304225311_init.sql rename to migrations/20230304225311_init.sql diff --git a/server/Cargo.toml b/server/Cargo.toml deleted file mode 100644 index ce74d89..0000000 --- a/server/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "server" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -anyhow = "1.0.69" -async-trait = "0.1.65" -axum = { version = "0.6.10", features = ["multipart", "headers"] } -dotenvy = "0.15.6" -hyper = "0.14.24" -mime_guess = "2.0.4" -nanoid = "0.4.0" -serde = { version = "1.0.152", features = ["derive"] } -sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls"] } -tokio = { version = "1.26.0", features = ["full"] } -tokio-stream = { version = "0.1.12", features = ["net"] } -tower = "0.4.13" -tower-http = { version = "0.4.0", features = ["fs"] } diff --git a/server/src/auth.rs b/src/auth.rs similarity index 100% rename from server/src/auth.rs rename to src/auth.rs diff --git a/server/src/main.rs b/src/main.rs similarity index 100% rename from server/src/main.rs rename to src/main.rs diff --git a/server/src/routes/delete.rs b/src/routes/delete.rs similarity index 100% rename from server/src/routes/delete.rs rename to src/routes/delete.rs diff --git a/server/src/routes/i.rs b/src/routes/i.rs similarity index 100% rename from server/src/routes/i.rs rename to src/routes/i.rs diff --git a/server/src/routes/mod.rs b/src/routes/mod.rs similarity index 100% rename from server/src/routes/mod.rs rename to src/routes/mod.rs diff --git a/server/src/routes/new_key.rs b/src/routes/new_key.rs similarity index 100% rename from server/src/routes/new_key.rs rename to src/routes/new_key.rs diff --git a/server/src/routes/upload.rs b/src/routes/upload.rs similarity index 100% rename from server/src/routes/upload.rs rename to src/routes/upload.rs diff --git a/server/src/state.rs b/src/state.rs similarity index 100% rename from server/src/state.rs rename to src/state.rs diff --git a/server/src/utils.rs b/src/utils.rs similarity index 100% rename from server/src/utils.rs rename to src/utils.rs diff --git a/upload.sh b/upload.sh new file mode 100644 index 0000000..daa8f93 --- /dev/null +++ b/upload.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env sh +set -eux + +path=$1 +extension=$(echo "$path" | cut -d'.' -f2) + +data=$(curl -F file=@$path -H "Authorization: $U_API_KEY" "$U_HOST/api/upload") +image_id=$(echo "$data" | jq -r .id) + +image_revocation_key=$(echo "$data" | jq -r .revocation_key) +echo "$image_id $image_revocation_key" >> $U_REVOCATION_KEY_PATH + +image_url="https://u.n2.pm/i/$image_id.$extension" +echo "$image_url" +echo "$image_url" | xclip -selection clipboard