It’s like vim but with lsp support out of the box and the keybindings make sense
- 1 Post
- 42 Comments
Note that there are many security concerns with this, notably the fact that there is no input validation on the
id
path segment which means you can get the content of any file (e.g.http://localhost:3000/src%2Fmain.rs
). It’s also very easy to scrape the content of all the files because the IDs are easy to predict. When the server reboots, you will overwrite previously written files because the counter starts back at zero. Using aUUID
would probably mostly solve both these issues.
Here’s a slightly more idiomatic version:
use std::{ fs, sync::atomic::{AtomicUsize, Ordering}, }; use axum::{extract::Path, http::StatusCode, routing::get, routing::post, Router}; const MAX_FILE_SIZE: usize = 1024 * 1024 * 10; static FILE_COUNT: AtomicUsize = AtomicUsize::new(0); async fn handle(Path(id): Path<String>) -> (StatusCode, String) { match fs::read_to_string(id) { Ok(content) => (StatusCode::OK, content), Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), } } async fn submit_handle(bytes: String) -> (StatusCode, String) { dbg!(&bytes); if bytes.len() > MAX_FILE_SIZE { // Don't store the file if it exceeds max size return ( StatusCode::BAD_REQUEST, "ERROR: max size exceeded".to_string(), ); } let path = FILE_COUNT.fetch_add(1, Ordering::SeqCst); if let Err(e) = fs::write(path.to_string(), bytes) { return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()); } (StatusCode::CREATED, format!("http://localhost:3000/%7Bpath%7D")) } #[tokio::main] async fn main() { let app = Router::new() .route("/", get(|| async { "Paste something in pastebin! use curl -X POST http://localhost:3000/submit -d 'this is some data'" })) .route("/{id}", get(handle)) .route("/submit", post(submit_handle)); let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await .unwrap(); axum::serve(listener, app).await.unwrap(); }
Note that there are no
unwrap
in the handlers which would absolutely want to avoid (it would crash your server). The endpoints now also return the correct HTTP code for each case. Some minor changes regarding creating the string values (use offormat!
andto_string()
on string slices). Lemmy messes with the curly braces in theformat!
macro, there should be curly braces around thepath
variable name.
Fetch add will return the old value before updating it so you don’t need the “.load” call above it!
I will probably post an improved version (if you like) but the main point is that you do not need the atomic to be mut, and so you don’t need unsafe. Have a look at https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html#method.fetch_add too
beeb@lemm.eeto Rust@programming.dev•What is the best way to handle file upload in Rust?7·5 months agoDid you check out the Examples ?
beeb@lemm.eeto Rust@programming.dev•My frustrations with Rust. Why is this the most loved language?1·8 months agoThat was my point exactly :) glad you got it
beeb@lemm.eeto Rust@programming.dev•My frustrations with Rust. Why is this the most loved language?3·8 months agoLiterally copy pasted from a random repo as an illustration
beeb@lemm.eeto Rust@programming.dev•My frustrations with Rust. Why is this the most loved language?8·8 months agoOP: “typescript is easy and rust is ugly”
Typescript :
export type PayloadActionCreator< P = void, T extends string = string, PA extends PrepareAction<P> | void = void > = IfPrepareActionMethodProvided< PA, _ActionCreatorWithPreparedPayload<PA, T>, // else IsAny< P, ActionCreatorWithPayload<any, T>, IsUnknownOrNonInferrable< P, ActionCreatorWithNonInferrablePayload<T>, // else IfVoid< P, ActionCreatorWithoutPayload<T>, // else IfMaybeUndefined< P, ActionCreatorWithOptionalPayload<P, T>, // else ActionCreatorWithPayload<P, T> > > > > >
beeb@lemm.eeto Selfhosted@lemmy.world•GitHub - Owez/yark: YouTube archiving made simple.English6·10 months agoAnd install python and install those dependencies before you can even run the thing
beeb@lemm.eeOPto Piracy: ꜱᴀɪʟ ᴛʜᴇ ʜɪɢʜ ꜱᴇᴀꜱ@lemmy.dbzer0.com•Alternative to dockerized Transmission for arr stackEnglish3·10 months agoThanks for all the suggestions! I switched to qbittorrent and it works nicely for now. The web ui is fine for the little I use it so all good! I’ll report if something starts failing again which would indicate another issue with me setup.
beeb@lemm.eeOPto Piracy: ꜱᴀɪʟ ᴛʜᴇ ʜɪɢʜ ꜱᴇᴀꜱ@lemmy.dbzer0.com•Alternative to dockerized Transmission for arr stackEnglish2·10 months agoSeems to work very nicely and it’s much more responsive than transmission. Great, thanks!
beeb@lemm.eeOPto Piracy: ꜱᴀɪʟ ᴛʜᴇ ʜɪɢʜ ꜱᴇᴀꜱ@lemmy.dbzer0.com•Alternative to dockerized Transmission for arr stackEnglish3·10 months agoI’m not set on transmission, I tried qbittorrent just now and it seems to work for me. But thanks! I anyway use a separate container and network for vpn.
beeb@lemm.eeOPto Piracy: ꜱᴀɪʟ ᴛʜᴇ ʜɪɢʜ ꜱᴇᴀꜱ@lemmy.dbzer0.com•Alternative to dockerized Transmission for arr stackEnglish2·10 months agoI’ll give it a go! Is there a GUI for it? I like to sometimes check the progress live and transmission provides that out of the box.
Started contributing to https://github.com/mario-eth/soldeer , mainly refactoring but also helping with new features.
Yeah their security track record as of late is pretty bad…
beeb@lemm.eeto Piracy: ꜱᴀɪʟ ᴛʜᴇ ʜɪɢʜ ꜱᴇᴀꜱ@lemmy.dbzer0.com•streaming or torrenting today vs. 5 years agoEnglish1·1 year agoSomething like this is a good start! I use transmission as the torrent client instead of deluge. Also added a service for my vpn and use that as the network for the tranmission service so all traffic is routed through the vpn. https://ochoaprojects.github.io/posts/PlexAutomation/
beeb@lemm.eeto Selfhosted@lemmy.world•Secure portal between Internet and internal servicesEnglish1·1 year agodeleted by creator
beeb@lemm.eeto Selfhosted@lemmy.world•Proton Pass breaks prowlarr on firefox since todayEnglish2·1 year agoI reported the bug to Proton support and they said they are investigating but suggested I uninstall and reinstall the extension. I did and lo, my forms started working again. I can recommend you try to uninstall and reinstall too (disabling it was not enough).
Cargo dist! Here’s a nice workflow you can use : https://blog.orhun.dev/automated-rust-releases/