Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 7th, 2020 200 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(proc_macro_hygiene, decl_macro)]
  2.  
  3. use std::fs;
  4. use std::fs::File;
  5. use std::io::{Read, Write};
  6.  
  7. #[macro_use] extern crate rocket;
  8. use rocket::Data;
  9.  
  10. use tempfile::NamedTempFile;
  11.  
  12. use sha3::{Digest, Sha3_256};
  13.  
  14. const MAX_FILE_SIZE: u64 = 1024 * 1024;
  15.  
  16. #[get("/")]
  17. fn index() -> &'static str {
  18.     "POST /upload/<file_extension>"
  19. }
  20.  
  21. #[get("/<id>")]
  22. fn download(id: String) -> Option<File> {
  23.     let extloc = id.rfind('.')?;
  24.     let noext: String = id.chars().take(extloc).collect();
  25.     let ext: String = id.chars().skip(extloc).collect();
  26.     let filename = format!("./files/{}{}", noext, ext);
  27.     File::open(&filename).ok()
  28. }
  29.  
  30. #[post("/upload/<ext>", data = "<paste>")]
  31. fn upload(ext: String, paste: Data) -> Result<String, std::io::Error> {
  32.     // write data to file and hasher
  33.     let mut tmp_file = NamedTempFile::new()?;
  34.     let mut reader = paste.open().take(MAX_FILE_SIZE);
  35.     let mut buffer: [u8; 1024] = [0; 1024];
  36.     let mut hasher = Sha3_256::new();
  37.  
  38.     while let Ok(read_bytes) = reader.read(&mut buffer) {
  39.         if read_bytes == 0 { // EOF has been reached
  40.             break ;
  41.         }
  42.         hasher.input(&buffer[0..read_bytes]);
  43.         tmp_file.write_all(&buffer[0..read_bytes])?;
  44.     }
  45.  
  46.     // generate filename
  47.     let mut filename = String::new();
  48.     let digest = hasher.result();
  49.  
  50.     for idx in 0..32 {
  51.         filename.push_str(&format!("{:x}", digest[idx]));
  52.     }
  53.  
  54.     // save tmpfile
  55.     tmp_file.persist(format!("./files/{}", filename))?;
  56.  
  57.     // return filename
  58.     Ok(format!("{}.{}", filename, ext))
  59. }
  60.  
  61. fn main() {
  62.  
  63.     if let Err(_) = fs::read_dir("./files/") {
  64.         println!("Failed to open the directory ./files/");
  65.         fs::create_dir("./files/").unwrap();
  66.     }
  67.  
  68.     rocket::ignite()
  69.         .mount("/", routes![index, download, upload])
  70.         .launch();
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top