add random letters to filename

This commit is contained in:
2021-06-12 18:11:13 +05:30
parent d4b05635b1
commit f77fe175ff
4 changed files with 96 additions and 41 deletions

82
Cargo.lock generated
View File

@@ -94,7 +94,7 @@ dependencies = [
"mime",
"percent-encoding",
"pin-project 1.0.7",
"rand",
"rand 0.7.3",
"regex",
"serde",
"serde_json",
@@ -469,7 +469,7 @@ dependencies = [
"log",
"mime",
"percent-encoding",
"rand",
"rand 0.7.3",
"serde",
"serde_json",
"serde_urlencoded",
@@ -927,7 +927,18 @@ checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
@@ -1437,11 +1448,23 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"getrandom 0.1.16",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.2",
"rand_hc 0.3.0",
]
[[package]]
@@ -1451,7 +1474,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.2",
]
[[package]]
@@ -1460,7 +1493,16 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
"getrandom 0.1.16",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom 0.2.3",
]
[[package]]
@@ -1469,7 +1511,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core 0.6.2",
]
[[package]]
@@ -1634,9 +1685,8 @@ dependencies = [
"actix-web",
"async-std",
"futures",
"rand 0.8.3",
"sanitize-filename",
"serde",
"serde_json",
]
[[package]]
@@ -1901,7 +1951,7 @@ dependencies = [
"idna",
"lazy_static",
"log",
"rand",
"rand 0.7.3",
"smallvec",
"thiserror",
"tokio",
@@ -2066,6 +2116,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.74"

View File

@@ -11,5 +11,4 @@ actix-multipart = "0.3"
futures = "0.3.5"
async-std = "1.8.0"
sanitize-filename = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.59"
rand = "0.8.3"

View File

@@ -1,6 +1,6 @@
mod post;
use actix_files::NamedFile;
use actix_web::{web, App, HttpRequest, HttpServer};
use actix_web::{web, App, HttpRequest, HttpServer, HttpResponse};
use std::{io, path::PathBuf};
const ROOT_DIR: &str = "/files/";
@@ -17,12 +17,26 @@ async fn get_file(req: HttpRequest) -> Result<NamedFile,io::Error> {
}
}
async fn index() -> Result<HttpResponse, io::Error> {
let html =
"<html>
<body>
Use curl to upload: <br> curl -F file=@\"[file]\" --header \"key: [key]\" https://f.weirdnatto.in
<br> Replace [file] with your local file and contact natto#5209 or natto#1264 on discord for [key]
<br> An URL will be returned after the upload, use that to access your content
<br> Content will be deleted by me roughly every 7 days
</body>
</html>";
Ok(HttpResponse::Ok().body(html))
}
#[actix_web::main]
async fn main() -> io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/{file}", web::get().to(get_file))
.route("/", web::post().to(post::post))
.route("/", web::get().to(index))
})
.bind(("0.0.0.0", 8888))?
.run()

View File

@@ -1,30 +1,12 @@
use actix_multipart::Multipart;
use futures::{StreamExt, TryStreamExt};
use actix_web::{HttpRequest, HttpResponse};
use async_std::prelude::*;
use futures::{StreamExt, TryStreamExt};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use sanitize_filename::sanitize;
use serde::Serialize;
use std::{env, io};
#[derive(Serialize)]
struct ReturnData {
file: String,
host: String,
protocol: String,
url: String,
}
impl ReturnData {
fn new(_file:&str, _host: &str, _protocol: &str) -> Self {
Self {
file: String::from(_file),
host: String::from(_host),
protocol: String::from(_protocol),
url: format!("{}://{}/{}", _protocol, _host, _file),
}
}
}
const ROOT_DIR: &'static str = "/files/";
fn get_token() -> String {
@@ -44,11 +26,15 @@ pub async fn post(req: HttpRequest, mut payload: Multipart) -> Result<HttpRespon
return Ok(HttpResponse::Unauthorized().body("Invalid key provided"));
}
let rand_stuff: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(4)
.map(char::from)
.collect();
let mut filename = String::from("");
while let Ok(Some(mut field)) = payload.try_next().await {
filename = sanitize(
field.content_disposition().unwrap().get_filename().unwrap(),
);
filename = format!("{}-{}", rand_stuff, sanitize(field.content_disposition().unwrap().get_filename().unwrap()));
let filepath = format!("{}{}", ROOT_DIR, filename);
let mut f = async_std::fs::File::create(filepath).await?;
@@ -57,5 +43,5 @@ pub async fn post(req: HttpRequest, mut payload: Multipart) -> Result<HttpRespon
f.write_all(&data).await?;
}
}
Ok(HttpResponse::Ok().body(serde_json::to_string(&ReturnData::new(&filename, "f.weirdnatto.in", "https")).unwrap()))
Ok(HttpResponse::Ok().body(format!("https://f.weirdnatto.in/{}", filename)))
}