Skip to content

Commit

Permalink
Display confirmation window to public space uploads
Browse files Browse the repository at this point in the history
Update docstrings and bump version to prerelease
  • Loading branch information
dormant-user committed Mar 12, 2024
1 parent a409913 commit bd1b8fc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "RuStream"
version = "1.2.0"
version = "1.3.0-a"
description = "Self-hosted Streaming Engine, that can render media files via authenticated sessions."
license = "MIT"
documentation = "https://docs.rs/RuStream"
Expand Down
24 changes: 21 additions & 3 deletions src/squire/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use minijinja::Template;

// todo: write docstrings

/// Frames a response for Not Found [404] into an error page.
///
/// # Arguments
///
/// * `error` - Jinja template for the error page.
/// * `description` - Description to be rendered in the UI.
///
/// # Returns
///
/// Returns an HTTPResponse with 404 error code formatted as HTML.
pub fn not_found(error: Template, description: &String) -> HttpResponse {
HttpResponse::build(StatusCode::NOT_FOUND)
.content_type("text/html; charset=utf-8")
Expand All @@ -15,8 +23,18 @@ pub fn not_found(error: Template, description: &String) -> HttpResponse {
)).unwrap())
}

/// Frames a response for Forbidden [403] into an error page.
///
/// # Arguments
///
/// * `error` - Jinja template for the error page.
/// * `username` - Username whose access is forbidden.
///
/// # Returns
///
/// Returns an HTTPResponse with 403 error code formatted as HTML.
pub fn restricted(error: Template, username: &String) -> HttpResponse {
HttpResponse::build(StatusCode::UNAUTHORIZED)
HttpResponse::build(StatusCode::FORBIDDEN)
.content_type("text/html; charset=utf-8")
.body(error.render(minijinja::context!(
title => "RESTRICTED SECTION",
Expand Down
48 changes: 40 additions & 8 deletions src/squire/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,29 @@ fn load_env_vars() -> settings::Config {
}
}

/// Get the current time in a specific format.
///
/// # Arguments
///
/// * `utc` - Boolean flag to return the time in UTC timezone.
///
/// # Returns
///
/// Returns the current datetime as a `String`.
fn get_time(utc: bool) -> String {
if utc {
Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string()
} else {
Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
Local::now().format("%Y-%m-%dT%H:%M:%SZ").to_string()
}
}

/// Validates the directory structure to ensure that the secure index is present in media source's root.
///
/// # Arguments
///
/// * `config` - Configuration data for the application.
/// * `cargo` - Package specific information loaded in a struct.
fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) {
let source = &config.media_source.to_string_lossy().to_string();
let mut errors = String::new();
Expand Down Expand Up @@ -254,13 +269,22 @@ fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) {
}
}
if errors.is_empty() {
for (username, _) in &config.authorization {
for username in config.authorization.keys() {
let secure_path = &config.media_source.join(format!("{}_{}", &username, constant::SECURE_INDEX));
if !secure_path.exists() {
match std::fs::create_dir(&secure_path) {
Ok(_) => println!("[{}\x1b[32m INFO\x1b[0m {}] '{}' has been created",
get_time(config.utc_logging), cargo.crate_name,
&secure_path.to_str().unwrap()),
match std::fs::create_dir(secure_path) {
Ok(_) => {
// keep formatting similar to logging
if config.utc_logging {
println!("[{}\x1b[32m INFO\x1b[0m {}] '{}' has been created",
get_time(config.utc_logging), cargo.crate_name,
&secure_path.to_str().unwrap())
} else {
println!("[{} INFO {}] '{}' has been created",
get_time(config.utc_logging), cargo.crate_name,
&secure_path.to_str().unwrap())
}
},
Err(err) => panic!("{}", err)
}
}
Expand All @@ -272,6 +296,10 @@ fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) {

/// Validates all the required environment variables with the required settings.
///
/// # Arguments
///
/// * `cargo` - Package specific information loaded in a struct.
///
/// # Returns
///
/// Returns the `Config` struct containing the required parameters.
Expand Down Expand Up @@ -304,12 +332,16 @@ fn validate_vars(cargo: &Cargo) -> settings::Config {
if !errors.is_empty() {
panic!("{}", errors);
}
validate_dir_structure(&config, &cargo);
validate_dir_structure(&config, cargo);
config
}

/// Retrieves the environment variables and parses as the data-type specified in Config struct.
///
/// # Arguments
///
/// * `cargo` - Package specific information loaded in a struct.
///
/// # Returns
///
/// Converts the config struct into an `Arc` and returns it.
Expand Down
16 changes: 15 additions & 1 deletion src/templates/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn get_content() -> String {
<h1>Upload Files</h1>
<p>PDF, Images, Videos and Subtitles are allowed</p>
<br>
<input type="checkbox" id="dedicated" name="dedicated" title="Files will be stored in a secured location, which can only be accessed by '{{ user }}'">
<input type="checkbox" id="dedicated" name="dedicated" title="Files will be stored in a secured location, which can only be accessed by '{{ user }}'" checked>
<label for="dedicated" title="Files will be stored in a secured location, which can only be accessed by '{{ user }}'"><i class="fa-solid fa-lock"></i></i>&nbsp;&nbsp;Upload files to '{{ user }}' directory</label>
</div>
<div class="drop-section">
Expand Down Expand Up @@ -501,6 +501,20 @@ pub fn get_content() -> String {
window.history.back();
}
</script>
<script>
document.getElementById("dedicated").addEventListener("change", function() {
if (!this.checked) {
let confirmation = confirm(
"Uploading to public space will not only make your uploads accessible " +
"to other users, but it will also overwrite any existing files " +
"with the same name.\n\nAre you sure you want to proceed?"
);
if (!confirmation) {
this.checked = true;
}
}
});
</script>
</body>
</html>"#.to_string()
}

0 comments on commit bd1b8fc

Please sign in to comment.