meteoritus/
lib.rs

1#![doc(
2    html_logo_url = "https://github.com/kallebysantos/meteoritus/raw/main/assets/logo-boxed-rounded.png"
3)]
4#![doc(
5    html_favicon_url = "https://github.com/kallebysantos/meteoritus/raw/main/assets/favicon.ico"
6)]
7//!  # Meteoritus - API Documentation
8//!
9//! Hello, and welcome to the Meteoritus API documentation!
10//!
11//! ## Usage
12//!
13//! Meteoritus is a [`Fairing`] that implements tus protocol on top of [`Rocket`] framework, so in
14//! order to use it you'll need the following dependencies in `Cargo.toml`:
15//!
16//! ```toml
17//! [dependencies]
18//! rocket = "0.5.1"
19//! meteoritus = "0.2.1"
20//! ```
21//!
22//! Then attach [`Meteoritus`] to your [`Rocket`] server on launch:
23//!
24//! ```rust,no_run
25//! #[macro_use] extern crate rocket;
26//! use rocket::data::ByteUnit;
27//! use meteoritus::Meteoritus;
28//!
29//! #[get("/")]
30//! fn hello() -> &'static str {
31//!     "Hello, world!"
32//! }
33//!
34//! #[launch]
35//! fn rocket() -> _ {
36//!     let meteoritus = Meteoritus::new()
37//!         .mount_to("/api/files")
38//!         .with_temp_path("./tmp/uploads")
39//!         .with_max_size(ByteUnit::Gibibyte(1))
40//!           .on_creation(|ctx| {
41//!                 println!("on_creation: {:?}", ctx);
42//!                 Ok(())
43//!            })
44//!           .on_created(|ctx| {
45//!                 println!("on_created: {:?}", ctx);
46//!            })
47//!           .on_completed(|ctx| {
48//!                println!("on_completed: {:?}", ctx);
49//!            })
50//!           .on_termination(|ctx| {
51//!                println!("on_termination: {:?}", ctx);
52//!            })
53//!         .build();
54//!
55//!     rocket::build()
56//!         .attach(meteoritus)
57//!         .mount("/", routes![hello])
58//! }
59//! ```
60//! [`Rocket`]: https://api.rocket.rs/v0.5/rocket/index.html
61//! [`Fairing`]: https://api.rocket.rs/v0.5/rocket/fairing/index.html
62
63/// These are public dependencies! Update docs if these are changed, especially
64/// figment's version number in docs.
65
66#[macro_use]
67extern crate rocket;
68
69use rocket::http::Header;
70
71mod meteoritus;
72pub use crate::meteoritus::Meteoritus;
73
74mod fs;
75pub use crate::fs::{
76    Built, Completed, Created, FileInfo, Metadata, MetadataError, Terminated,
77    Vault,
78};
79
80mod handlers;
81pub use crate::handlers::HandlerContext;
82
83/// Represents the tus protocol headers.
84pub enum MeteoritusHeaders {
85    MaxSize(u64),
86    Extensions(&'static [&'static str]),
87    Version(&'static [&'static str]),
88    Resumable(&'static str),
89}
90
91impl Into<Header<'_>> for MeteoritusHeaders {
92    fn into(self) -> Header<'static> {
93        match self {
94            MeteoritusHeaders::MaxSize(size) => {
95                Header::new("Tus-Max-Size", size.to_string())
96            }
97            MeteoritusHeaders::Extensions(ext) => {
98                Header::new("Tus-Extension", ext.join(","))
99            }
100            MeteoritusHeaders::Version(ver) => {
101                Header::new("Tus-Version", ver.join(","))
102            }
103            MeteoritusHeaders::Resumable(ver) => {
104                Header::new("Tus-Resumable", ver)
105            }
106        }
107    }
108}