diff --git a/Cargo.lock b/Cargo.lock index 20ec430d0b31bcabad08e876effff098151690c8..ac0894e31f84c072c1ca35410b30aa295462f3d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1905,6 +1905,27 @@ dependencies = [ "semver 1.0.4", ] +[[package]] +name = "rustus" +version = "0.1.0" +dependencies = [ + "actix-files", + "actix-web", + "async-std", + "async-trait", + "chrono", + "derive_more", + "log", + "serde", + "serde_json", + "simple-logging", + "sqlx", + "structopt", + "thiserror", + "url", + "uuid", +] + [[package]] name = "ryu" version = "1.0.9" @@ -2541,27 +2562,6 @@ dependencies = [ "trust-dns-proto", ] -[[package]] -name = "tuser" -version = "0.1.0" -dependencies = [ - "actix-files", - "actix-web", - "async-std", - "async-trait", - "chrono", - "derive_more", - "log", - "serde", - "serde_json", - "simple-logging", - "sqlx", - "structopt", - "thiserror", - "url", - "uuid", -] - [[package]] name = "typenum" version = "1.14.0" diff --git a/Cargo.toml b/Cargo.toml index d9228585e5c98028f32737dde25a12f5b61eeaa5..8e5de99ba9ae0fb105a0665c5502ba51d7751101 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "tuser" +name = "rustus" version = "0.1.0" edition = "2021" description = "TUS protocol implementation written in Rust." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [[bin]] -name = "tuser" +name = "rustus" [dependencies] actix-web = "^3.3.2" diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 4013041f6ddf9511bf5888930d95b132bee1abd8..ebd5b60e864844f045cedfb3a106313a9f6d1c82 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -11,9 +11,9 @@ COPY --from=planner /app/recipe.json recipe.json RUN cargo chef cook --release --recipe-path recipe.json # Build application COPY . . -RUN cargo build --release --bin tuser +RUN cargo build --release --bin rustus FROM debian:buster-20211201-slim AS runtime WORKDIR app -COPY --from=builder /app/target/release/tuser /usr/local/bin/ -ENTRYPOINT ["/usr/local/bin/tuser"] +COPY --from=builder /app/target/release/rustus /usr/local/bin/ +ENTRYPOINT ["/usr/local/bin/rustus"] diff --git a/src/config.rs b/src/config.rs index 6d0377285eb48504bd5dca17ceb31f45922f9345..69f185883d99c13fb4f615e4421dbc177791ecbd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,16 +2,16 @@ use std::path::PathBuf; use structopt::StructOpt; -use crate::errors::TuserError; +use crate::errors::RustusError; use crate::storages::AvailableStores; #[derive(StructOpt, Debug, Clone)] pub struct StorageOptions { - /// Tuser storage type. - #[structopt(long, short, default_value = "file_storage", env = "TUSER_STORAGE")] + /// Rustus storage type. + #[structopt(long, short, default_value = "file_storage", env = "RUSTUS_STORAGE")] pub storage: AvailableStores, - /// Tuser data directory + /// Rustus data directory /// /// This directory is used to store files /// for all *file_storage storages. @@ -36,37 +36,44 @@ pub struct StorageOptions { } #[derive(Debug, StructOpt, Clone)] -#[structopt(name = "tuser", about = "Tus server implementation in Rust.")] -pub struct TuserConf { - /// Tuser host - #[structopt(short, long, default_value = "0.0.0.0", env = "TUSER_HOST")] +#[structopt(name = "rustus")] +/// Tus protocol implementation. +/// +/// This program is a web-server that +/// implements protocol for resumable uploads. +/// +/// You can read more about protocol +/// [here](https://tus.io/). +pub struct RustusConf { + /// Rustus host + #[structopt(short, long, default_value = "0.0.0.0", env = "RUSTUS_HOST")] pub host: String, - /// Tuser server port - #[structopt(short, long, default_value = "1081", env = "TUSER_PORT")] + /// Rustus server port + #[structopt(short, long, default_value = "1081", env = "RUSTUS_PORT")] pub port: u16, - /// Tuser base API url - #[structopt(long, default_value = "/files", env = "TUSER_URL")] + /// Rustus base API url + #[structopt(long, default_value = "/files", env = "RUSTUS_URL")] pub url: String, /// Enabled hooks for http events - #[structopt(long, default_value = "pre-create,post-finish", env = "TUSER_HOOKS")] + #[structopt(long, default_value = "pre-create,post-finish", env = "RUSTUS_HOOKS")] pub enabled_hooks: String, - /// Tuser maximum log level - #[structopt(long, default_value = "INFO", env = "TUSER_LOG_LEVEL")] + /// Rustus maximum log level + #[structopt(long, default_value = "INFO", env = "RUSTUS_LOG_LEVEL")] pub log_level: log::LevelFilter, /// Number of actix workers default value = number of cpu cores. - #[structopt(long, short, env = "TUSER_WORKERS")] + #[structopt(long, short, env = "RUSTUS_WORKERS")] pub workers: Option<usize>, /// Enabled extensions for TUS protocol. #[structopt( long, default_value = "creation,creation-with-upload,getting", - env = "TUSER_EXTENSIONS" + env = "RUSTUS_EXTENSIONS" )] pub extensions: String, @@ -84,7 +91,7 @@ pub enum ProtocolExtensions { } impl TryFrom<String> for ProtocolExtensions { - type Error = TuserError; + type Error = RustusError; /// Parse string to protocol extension. /// @@ -95,7 +102,7 @@ impl TryFrom<String> for ProtocolExtensions { "creation-with-upload" => Ok(ProtocolExtensions::CreationWithUpload), "termination" => Ok(ProtocolExtensions::Termination), "getting" => Ok(ProtocolExtensions::Getting), - _ => Err(TuserError::UnknownExtension(value.clone())), + _ => Err(RustusError::UnknownExtension(value.clone())), } } } @@ -113,13 +120,13 @@ impl From<ProtocolExtensions> for String { } } -impl TuserConf { +impl RustusConf { /// Function to parse CLI parametes. /// /// This is a workaround for issue mentioned /// [here](https://www.reddit.com/r/rust/comments/8ddd19/confusion_with_splitting_mainrs_into_smaller/). - pub fn from_args() -> TuserConf { - <TuserConf as StructOpt>::from_args() + pub fn from_args() -> RustusConf { + <RustusConf as StructOpt>::from_args() } /// Base API url. diff --git a/src/errors.rs b/src/errors.rs index 173b3feaa61d3d0634b35daad6a437032e2375ab..cc269354b338119e9cd20f02d2ff884f7ade7a74 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,10 +4,10 @@ use actix_web::dev::HttpResponseBuilder; use actix_web::http::StatusCode; use actix_web::{HttpResponse, ResponseError}; -pub type TuserResult<T> = Result<T, TuserError>; +pub type RustusResult<T> = Result<T, RustusError>; #[derive(thiserror::Error, Debug)] -pub enum TuserError { +pub enum RustusError { #[error("Not found")] FileNotFound, #[error("File with id {0} already exists")] @@ -32,13 +32,13 @@ pub enum TuserError { UnknownExtension(String), } -impl From<TuserError> for Error { - fn from(err: TuserError) -> Self { +impl From<RustusError> for Error { + fn from(err: RustusError) -> Self { Error::new(ErrorKind::Other, err) } } -impl ResponseError for TuserError { +impl ResponseError for RustusError { fn error_response(&self) -> HttpResponse { HttpResponseBuilder::new(self.status_code()) .set_header("Content-Type", "text/html; charset=utf-8") @@ -47,8 +47,8 @@ impl ResponseError for TuserError { fn status_code(&self) -> StatusCode { match self { - TuserError::FileNotFound => StatusCode::NOT_FOUND, - TuserError::WrongOffset => StatusCode::CONFLICT, + RustusError::FileNotFound => StatusCode::NOT_FOUND, + RustusError::WrongOffset => StatusCode::CONFLICT, _ => StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/src/main.rs b/src/main.rs index 1ead5e97e8e8a2712d42360ec567c318ac115d31..73a3f341e5245119f954245814b7268821532ce8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use actix_web::{ }; use log::error; -use config::TuserConf; +use config::RustusConf; use crate::storages::Storage; @@ -33,7 +33,7 @@ mod storages; /// given address. pub fn create_server( storage: Box<dyn Storage + Send + Sync>, - app_conf: TuserConf, + app_conf: RustusConf, ) -> Result<Server, std::io::Error> { let host = app_conf.host.clone(); let port = app_conf.port; @@ -81,7 +81,7 @@ pub fn create_server( /// Main program entrypoint. #[actix_web::main] async fn main() -> std::io::Result<()> { - let app_conf = TuserConf::from_args(); + let app_conf = RustusConf::from_args(); simple_logging::log_to_stderr(app_conf.log_level); let mut storage = app_conf.storage_opts.storage.get(&app_conf); diff --git a/src/protocol/core/mod.rs b/src/protocol/core/mod.rs index c76a19dd10582c87d9f213d1f7e34f97aa166b9e..59a2f847ace6d8b63ea7eaab436130b45de2e215 100644 --- a/src/protocol/core/mod.rs +++ b/src/protocol/core/mod.rs @@ -1,6 +1,6 @@ use actix_web::{guard, middleware, web}; -use crate::TuserConf; +use crate::RustusConf; mod routes; @@ -12,7 +12,7 @@ mod routes; /// OPTIONS /api - to get info about the app. /// HEAD /api/file - to get info about the file. /// PATCH /api/file - to add bytes to file. -pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &TuserConf) { +pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &RustusConf) { web_app .service( // PATCH /base/{file_id} diff --git a/src/protocol/core/routes.rs b/src/protocol/core/routes.rs index 9b5662244077758df94b8f30028853c0721f170a..2966b36ca683083ca9cb614c632f0348deb40ba6 100644 --- a/src/protocol/core/routes.rs +++ b/src/protocol/core/routes.rs @@ -6,10 +6,10 @@ use actix_web::{ HttpRequest, HttpResponse, }; -use crate::{Storage, TuserConf}; +use crate::{RustusConf, Storage}; #[allow(clippy::needless_pass_by_value)] -pub fn server_info(app_conf: web::Data<TuserConf>) -> HttpResponse { +pub fn server_info(app_conf: web::Data<RustusConf>) -> HttpResponse { let ext_str = app_conf .extensions_vec() .into_iter() diff --git a/src/protocol/creation/mod.rs b/src/protocol/creation/mod.rs index 0b0245429b3672832b6a7f2d24cc66b05366ade0..2ecaf127e9de83ce0806fd89374eb44ffd704614 100644 --- a/src/protocol/creation/mod.rs +++ b/src/protocol/creation/mod.rs @@ -1,6 +1,6 @@ use actix_web::{guard, web}; -use crate::TuserConf; +use crate::RustusConf; mod routes; @@ -8,7 +8,7 @@ mod routes; /// /// This extension allows you /// to create file before sending data. -pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &TuserConf) { +pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &RustusConf) { web_app.service( // Post /base // URL for creating files. diff --git a/src/protocol/creation_with_upload/mod.rs b/src/protocol/creation_with_upload/mod.rs index b02b0cf235280e2a2fb46b88a0c6e1f5b0cfb0e9..797113ae8489699503bd62f5209373029b952bc4 100644 --- a/src/protocol/creation_with_upload/mod.rs +++ b/src/protocol/creation_with_upload/mod.rs @@ -1,10 +1,10 @@ use actix_web::{guard, web}; -use crate::TuserConf; +use crate::RustusConf; mod routes; -pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &TuserConf) { +pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &RustusConf) { web_app.service( // Post /base // URL for creating files. diff --git a/src/protocol/getting/mod.rs b/src/protocol/getting/mod.rs index a63ceb7935f12dbbd98feb7034a6bf00b3c75f5b..e80e4485069e0aff5dddb0669711877ed9c77644 100644 --- a/src/protocol/getting/mod.rs +++ b/src/protocol/getting/mod.rs @@ -1,6 +1,6 @@ use actix_web::{guard, web}; -use crate::TuserConf; +use crate::RustusConf; mod routes; @@ -10,7 +10,7 @@ mod routes; /// to get uploaded file. /// /// This is unofficial extension. -pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &TuserConf) { +pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &RustusConf) { web_app.service( // GET /base/file web::resource(app_conf.file_url().as_str()) diff --git a/src/protocol/getting/routes.rs b/src/protocol/getting/routes.rs index cdcebaff5f64a48cefc3c87b719fe7d5275d11be..fdd918d7dc9ddad17c42c2e155209299cacbba1e 100644 --- a/src/protocol/getting/routes.rs +++ b/src/protocol/getting/routes.rs @@ -1,6 +1,6 @@ -use actix_web::{HttpRequest, Responder, web}; +use actix_web::{web, HttpRequest, Responder}; -use crate::errors::TuserError; +use crate::errors::RustusError; use crate::Storage; pub async fn get_file( @@ -11,6 +11,6 @@ pub async fn get_file( if let Some(file_id) = file_id_opt { storage.get_contents(file_id.as_str()).await } else { - Err(TuserError::FileNotFound) + Err(RustusError::FileNotFound) } } diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 64faf840ed90ce251826e7944e2d321ad7d6008f..605e791e7744f3a87bd373e6336dc8c07accd147 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -1,7 +1,7 @@ use actix_web::web; use crate::config::ProtocolExtensions; -use crate::TuserConf; +use crate::RustusConf; mod core; mod creation; @@ -13,7 +13,7 @@ mod termination; /// /// This function resolves all protocol extensions /// provided by CLI into services and adds it to the application. -pub fn setup(app_conf: TuserConf) -> Box<dyn Fn(&mut web::ServiceConfig)> { +pub fn setup(app_conf: RustusConf) -> Box<dyn Fn(&mut web::ServiceConfig)> { Box::new(move |web_app| { for extension in app_conf.extensions_vec() { match extension { diff --git a/src/protocol/termination/mod.rs b/src/protocol/termination/mod.rs index ae8c62cf57540cd4faf9cfc82761486246800b2f..3774fff828fd9835234041dfa8767e15bbc1a85f 100644 --- a/src/protocol/termination/mod.rs +++ b/src/protocol/termination/mod.rs @@ -1,6 +1,6 @@ use actix_web::{guard, web}; -use crate::TuserConf; +use crate::RustusConf; mod routes; @@ -8,7 +8,7 @@ mod routes; /// /// This extension allows you /// to terminate file upload. -pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &TuserConf) { +pub fn add_extension(web_app: &mut web::ServiceConfig, app_conf: &RustusConf) { web_app.service( // DELETE /base/file web::resource(app_conf.file_url().as_str()) diff --git a/src/protocol/termination/routes.rs b/src/protocol/termination/routes.rs index 4125a20cbf88f23297b11e927d45cdb28b979257..2ae818cc664b44d27663062c8c2a04032d15b08b 100644 --- a/src/protocol/termination/routes.rs +++ b/src/protocol/termination/routes.rs @@ -2,7 +2,7 @@ use actix_web::dev::HttpResponseBuilder; use actix_web::http::StatusCode; use actix_web::{web, HttpRequest, HttpResponse}; -use crate::errors::TuserResult; +use crate::errors::RustusResult; use crate::Storage; /// Terminate uploading. @@ -12,7 +12,7 @@ use crate::Storage; pub async fn terminate( storage: web::Data<Box<dyn Storage + Send + Sync>>, request: HttpRequest, -) -> TuserResult<HttpResponse> { +) -> RustusResult<HttpResponse> { let file_id_opt = request.match_info().get("file_id").map(String::from); if let Some(file_id) = file_id_opt { storage.remove_file(file_id.as_str()).await?; diff --git a/src/routes.rs b/src/routes.rs index ce07d5828e87df3474b2f54be73ce62fde16385a..ba878f8b21fd9d28d4a10f420d9f56f9f80f9117 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,9 +1,9 @@ use actix_web::HttpResponse; -use crate::errors::{TuserError, TuserResult}; +use crate::errors::{RustusError, RustusResult}; /// Default response to all unknown URLs. #[allow(clippy::unused_async)] -pub async fn not_found() -> TuserResult<HttpResponse> { - Err(TuserError::FileNotFound) +pub async fn not_found() -> RustusResult<HttpResponse> { + Err(RustusError::FileNotFound) } diff --git a/src/storages/file_storage.rs b/src/storages/file_storage.rs index 26d1fc77393f50726277c60941374d7449bd7928..a9caf166ec5449e6e93c32b1434c58f3b706f1a9 100644 --- a/src/storages/file_storage.rs +++ b/src/storages/file_storage.rs @@ -8,17 +8,17 @@ use async_trait::async_trait; use log::error; use uuid::Uuid; -use crate::errors::{TuserError, TuserResult}; +use crate::errors::{RustusError, RustusResult}; use crate::storages::{FileInfo, Storage}; -use crate::TuserConf; +use crate::RustusConf; #[derive(Clone)] pub struct FileStorage { - app_conf: TuserConf, + app_conf: RustusConf, } impl FileStorage { - pub fn new(app_conf: TuserConf) -> FileStorage { + pub fn new(app_conf: RustusConf) -> FileStorage { FileStorage { app_conf } } @@ -36,29 +36,29 @@ impl FileStorage { #[async_trait] impl Storage for FileStorage { - async fn prepare(&mut self) -> TuserResult<()> { + async fn prepare(&mut self) -> RustusResult<()> { if !self.app_conf.storage_opts.data.exists() { DirBuilder::new() .create(self.app_conf.storage_opts.data.as_path()) .await - .map_err(|err| TuserError::UnableToPrepareStorage(err.to_string()))?; + .map_err(|err| RustusError::UnableToPrepareStorage(err.to_string()))?; } Ok(()) } - async fn get_file_info(&self, file_id: &str) -> TuserResult<FileInfo> { + async fn get_file_info(&self, file_id: &str) -> RustusResult<FileInfo> { let info_path = self.info_file_path(file_id); if !info_path.exists() { - return Err(TuserError::FileNotFound); + return Err(RustusError::FileNotFound); } let contents = read_to_string(info_path).await.map_err(|err| { error!("{:?}", err); - TuserError::UnableToReadInfo + RustusError::UnableToReadInfo })?; - serde_json::from_str::<FileInfo>(contents.as_str()).map_err(TuserError::from) + serde_json::from_str::<FileInfo>(contents.as_str()).map_err(RustusError::from) } - async fn set_file_info(&self, file_info: &FileInfo) -> TuserResult<()> { + async fn set_file_info(&self, file_info: &FileInfo) -> RustusResult<()> { let mut file = OpenOptions::new() .write(true) .create(true) @@ -66,13 +66,13 @@ impl Storage for FileStorage { .await .map_err(|err| { error!("{:?}", err); - TuserError::UnableToWrite(err.to_string()) + RustusError::UnableToWrite(err.to_string()) })?; file.write_all(serde_json::to_string(&file_info)?.as_bytes()) .await .map_err(|err| { error!("{:?}", err); - TuserError::UnableToWrite( + RustusError::UnableToWrite( self.info_file_path(file_info.id.as_str()) .as_path() .display() @@ -82,10 +82,10 @@ impl Storage for FileStorage { Ok(()) } - async fn get_contents(&self, file_id: &str) -> TuserResult<NamedFile> { + async fn get_contents(&self, file_id: &str) -> RustusResult<NamedFile> { NamedFile::open(self.data_file_path(file_id)).map_err(|err| { error!("{:?}", err); - TuserError::FileNotFound + RustusError::FileNotFound }) } @@ -94,10 +94,10 @@ impl Storage for FileStorage { file_id: &str, request_offset: usize, bytes: &[u8], - ) -> TuserResult<usize> { + ) -> RustusResult<usize> { let mut info = self.get_file_info(file_id).await?; if info.offset != request_offset { - return Err(TuserError::WrongOffset); + return Err(RustusError::WrongOffset); } let mut file = OpenOptions::new() .write(true) @@ -107,11 +107,11 @@ impl Storage for FileStorage { .await .map_err(|err| { error!("{:?}", err); - TuserError::UnableToWrite(err.to_string()) + RustusError::UnableToWrite(err.to_string()) })?; file.write_all(bytes).await.map_err(|err| { error!("{:?}", err); - TuserError::UnableToWrite(self.data_file_path(file_id).as_path().display().to_string()) + RustusError::UnableToWrite(self.data_file_path(file_id).as_path().display().to_string()) })?; info.offset += bytes.len(); self.set_file_info(&info).await?; @@ -122,7 +122,7 @@ impl Storage for FileStorage { &self, file_size: Option<usize>, metadata: Option<HashMap<String, String>>, - ) -> TuserResult<String> { + ) -> RustusResult<String> { let file_id = Uuid::new_v4().simple().to_string(); let mut file = OpenOptions::new() @@ -133,13 +133,13 @@ impl Storage for FileStorage { .await .map_err(|err| { error!("{:?}", err); - TuserError::FileAlreadyExists(file_id.clone()) + RustusError::FileAlreadyExists(file_id.clone()) })?; // We write empty file here. file.write_all(b"").await.map_err(|err| { error!("{:?}", err); - TuserError::UnableToWrite( + RustusError::UnableToWrite( self.data_file_path(file_id.as_str()) .as_path() .display() @@ -162,22 +162,22 @@ impl Storage for FileStorage { Ok(file_id) } - async fn remove_file(&self, file_id: &str) -> TuserResult<()> { + async fn remove_file(&self, file_id: &str) -> RustusResult<()> { let info_path = self.info_file_path(file_id); if !info_path.exists() { - return Err(TuserError::FileNotFound); + return Err(RustusError::FileNotFound); } let data_path = self.data_file_path(file_id); if !data_path.exists() { - return Err(TuserError::FileNotFound); + return Err(RustusError::FileNotFound); } remove_file(info_path).await.map_err(|err| { error!("{:?}", err); - TuserError::UnableToRemove(String::from(file_id)) + RustusError::UnableToRemove(String::from(file_id)) })?; remove_file(data_path).await.map_err(|err| { error!("{:?}", err); - TuserError::UnableToRemove(String::from(file_id)) + RustusError::UnableToRemove(String::from(file_id)) })?; Ok(()) } diff --git a/src/storages/mod.rs b/src/storages/mod.rs index ee35ba3963d43da13534665b5d3e214507d6ad4f..9b11b3c560c5a2f94c5f2a1052fd6e3b6ba0d305 100644 --- a/src/storages/mod.rs +++ b/src/storages/mod.rs @@ -9,8 +9,8 @@ use derive_more::{Display, From}; use serde::{Deserialize, Serialize}; use sqlx::FromRow; -use crate::errors::TuserResult; -use crate::TuserConf; +use crate::errors::RustusResult; +use crate::RustusConf; pub mod file_storage; pub mod sqlite_file_storage; @@ -45,9 +45,9 @@ impl AvailableStores { /// Convert `AvailableStores` to the Storage. /// /// # Params - /// `config` - Tuser configuration. + /// `config` - Rustus configuration. /// - pub fn get(&self, config: &TuserConf) -> Box<dyn Storage + Send + Sync> { + pub fn get(&self, config: &RustusConf) -> Box<dyn Storage + Send + Sync> { match self { Self::FileStorage => Box::new(file_storage::FileStorage::new(config.clone())), Self::SqliteFileStorage => { @@ -118,7 +118,7 @@ pub trait Storage { /// Function to check if configuration is correct /// and prepare storage E.G. create connection pool, /// or directory for files. - async fn prepare(&mut self) -> TuserResult<()>; + async fn prepare(&mut self) -> RustusResult<()>; /// Get file information. /// @@ -126,7 +126,7 @@ pub trait Storage { /// /// # Params /// `file_id` - unique file identifier. - async fn get_file_info(&self, file_id: &str) -> TuserResult<FileInfo>; + async fn get_file_info(&self, file_id: &str) -> RustusResult<FileInfo>; /// Set file info /// @@ -134,7 +134,7 @@ pub trait Storage { /// /// # Params /// `file_info` - information about current upload. - async fn set_file_info(&self, file_info: &FileInfo) -> TuserResult<()>; + async fn set_file_info(&self, file_info: &FileInfo) -> RustusResult<()>; /// Get contents of a file. /// @@ -143,7 +143,7 @@ pub trait Storage { /// /// # Params /// `file_id` - unique file identifier. - async fn get_contents(&self, file_id: &str) -> TuserResult<NamedFile>; + async fn get_contents(&self, file_id: &str) -> RustusResult<NamedFile>; /// Add bytes to the file. /// @@ -159,7 +159,7 @@ pub trait Storage { file_id: &str, request_offset: usize, bytes: &[u8], - ) -> TuserResult<usize>; + ) -> RustusResult<usize>; /// Create file in storage. /// @@ -172,7 +172,7 @@ pub trait Storage { &self, file_size: Option<usize>, metadata: Option<HashMap<String, String>>, - ) -> TuserResult<String>; + ) -> RustusResult<String>; /// Remove file from storage /// @@ -181,5 +181,5 @@ pub trait Storage { /// /// # Params /// `file_id` - unique file identifier; - async fn remove_file(&self, file_id: &str) -> TuserResult<()>; + async fn remove_file(&self, file_id: &str) -> RustusResult<()>; } diff --git a/src/storages/sqlite_file_storage.rs b/src/storages/sqlite_file_storage.rs index fcd401d3a56201868d83c36317eb10887a214da8..94ee774f8d6d3449d5fd59b1d7c050607a99ef4b 100644 --- a/src/storages/sqlite_file_storage.rs +++ b/src/storages/sqlite_file_storage.rs @@ -8,18 +8,18 @@ use sqlx::sqlite::SqlitePoolOptions; use sqlx::SqlitePool; use thiserror::private::PathAsDisplay; -use crate::errors::{TuserError, TuserResult}; +use crate::errors::{RustusError, RustusResult}; use crate::storages::{FileInfo, Storage}; -use crate::TuserConf; +use crate::RustusConf; #[derive(Clone)] pub struct SQLiteFileStorage { - app_conf: TuserConf, + app_conf: RustusConf, pool: Option<SqlitePool>, } impl SQLiteFileStorage { - pub fn new(app_conf: TuserConf) -> SQLiteFileStorage { + pub fn new(app_conf: RustusConf) -> SQLiteFileStorage { SQLiteFileStorage { app_conf, pool: None, @@ -27,29 +27,29 @@ impl SQLiteFileStorage { } #[allow(dead_code)] - pub fn get_pool(&self) -> TuserResult<&SqlitePool> { + pub fn get_pool(&self) -> RustusResult<&SqlitePool> { if let Some(pool) = &self.pool { Ok(pool) } else { error!("Pool doesn't exist."); - Err(TuserError::Unknown) + Err(RustusError::Unknown) } } } #[async_trait] impl Storage for SQLiteFileStorage { - async fn prepare(&mut self) -> TuserResult<()> { + async fn prepare(&mut self) -> RustusResult<()> { if !self.app_conf.storage_opts.data.exists() { DirBuilder::new() .create(self.app_conf.storage_opts.data.as_path()) .await - .map_err(|err| TuserError::UnableToPrepareStorage(err.to_string()))?; + .map_err(|err| RustusError::UnableToPrepareStorage(err.to_string()))?; } if !self.app_conf.storage_opts.sqlite_dsn.exists() { File::create(self.app_conf.storage_opts.sqlite_dsn.clone()) .await - .map_err(|err| TuserError::UnableToPrepareStorage(err.to_string()))?; + .map_err(|err| RustusError::UnableToPrepareStorage(err.to_string()))?; } let pool = SqlitePoolOptions::new() .max_connections(10) @@ -65,7 +65,7 @@ impl Storage for SQLiteFileStorage { .as_str(), ) .await - .map_err(TuserError::from)?; + .map_err(RustusError::from)?; sqlx::query( "CREATE TABLE IF NOT EXISTS \ fileinfo(\ @@ -84,15 +84,15 @@ impl Storage for SQLiteFileStorage { Ok(()) } - async fn get_file_info(&self, _file_id: &str) -> TuserResult<FileInfo> { + async fn get_file_info(&self, _file_id: &str) -> RustusResult<FileInfo> { todo!() } - async fn set_file_info(&self, _file_info: &FileInfo) -> TuserResult<()> { + async fn set_file_info(&self, _file_info: &FileInfo) -> RustusResult<()> { todo!() } - async fn get_contents(&self, _file_id: &str) -> TuserResult<NamedFile> { + async fn get_contents(&self, _file_id: &str) -> RustusResult<NamedFile> { todo!() } @@ -101,7 +101,7 @@ impl Storage for SQLiteFileStorage { _file_id: &str, _request_offset: usize, _bytes: &[u8], - ) -> TuserResult<usize> { + ) -> RustusResult<usize> { todo!() } @@ -109,11 +109,11 @@ impl Storage for SQLiteFileStorage { &self, _file_size: Option<usize>, _metadata: Option<HashMap<String, String>>, - ) -> TuserResult<String> { + ) -> RustusResult<String> { todo!() } - async fn remove_file(&self, _file_id: &str) -> TuserResult<()> { + async fn remove_file(&self, _file_id: &str) -> RustusResult<()> { todo!() } }