1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use actix_web::{HttpRequest, HttpResponse, web};
use actix_web::http::StatusCode;
use minijinja::{context, Environment};
use serde::Deserialize;
use url::form_urlencoded;

use crate::{constant, squire};
use crate::routes;

/// Represents the payload structure for deserializing data from the request query parameters.
#[derive(Deserialize)]
pub struct Payload {
    file: String,
}

/// Represents the paths and filenames for subtitles, including both SRT and VTT formats.
struct Subtitles {
    srt: PathBuf,
    vtt: PathBuf,
    vtt_file: String,
}

/// URL encodes the provided path string.
///
/// This function takes a reference to a `String` representing a path,
/// encodes it using the `form_urlencoded` crate, and returns the encoded string.
///
/// # Arguments
///
/// * `path` - The input path string to be URL encoded.
///
/// ## References
/// - [RustJobs](https://rustjobs.dev/blog/how-to-url-encode-strings-in-rust/)
///
/// # Returns
///
/// Returns a URL encoded string.
fn url_encode(path: &String) -> String {
    form_urlencoded::byte_serialize(path.as_bytes())
        .collect::<Vec<_>>()
        .join("")
}

/// Constructs a `Subtitles` struct based on the provided `target` path and `target_str`.
///
/// # Arguments
///
/// * `true_path` - True path of the requested video file.
/// * `relative_path` - The string representation of the relative video path.
///
/// # Returns
///
/// Returns a `Subtitles` struct containing paths and filenames for both SRT and VTT subtitle files.
fn subtitles(true_path: PathBuf, relative_path: &String) -> Subtitles {
    // Set srt and vtt extensions to true path to check if they exist
    let srt = true_path.with_extension("srt");
    let vtt = true_path.with_extension("vtt");

    // Set vtt extension to the relative path, so it could be used as a parameter in HTML
    let vtt_filepath = PathBuf::new().join(relative_path).with_extension("vtt");
    let vtt_file = vtt_filepath.to_string_lossy().to_string();

    Subtitles { srt, vtt, vtt_file }
}

/// Handles requests for the '/track/{track_path:.*}' endpoint, serving track files.
///
/// # Arguments
///
/// * `config` - The configuration settings.
/// * `request` - The HTTP request.
/// * `track_path` - The path parameter representing the track file.
///
/// # Returns
///
/// Returns an `HttpResponse` containing the track file content or an error response.
#[get("/track")]
pub async fn track(config: web::Data<Arc<squire::settings::Config>>,
                   request: HttpRequest, info: web::Query<Payload>) -> HttpResponse {
    let auth_response = squire::authenticator::verify_token(&request, &config);
    if !auth_response.ok {
        return routes::auth::failed_auth(auth_response, &config);
    }
    squire::logger::log_connection(&request);
    log::debug!("{}", auth_response.detail);
    log::debug!("Track requested: {}", &info.file);
    let filepath = Path::new(&config.video_source).join(&info.file);
    log::debug!("Track file lookup: {}", &filepath.to_string_lossy());
    match std::fs::read_to_string(&filepath) {
        Ok(content) => HttpResponse::Ok()
            .content_type("text/plain")
            .body(content),
        Err(_) => HttpResponse::NotFound().json(routes::auth::DetailError {
            detail: format!("'{}' was not found", &info.file)
        })
    }
}

/// Handles requests for the '/stream/{video_path:.*}' endpoint, serving video files and directories.
///
/// # Arguments
///
/// * `config` - The configuration settings.
/// * `request` - The HTTP request.
/// * `video_path` - The path parameter representing the video file or directory.
///
/// # Returns
///
/// Returns an `HttpResponse` containing the video content or directory listing, or an error response.
#[get("/stream/{video_path:.*}")]
pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
                    environment: web::Data<Arc<Mutex<Environment<'static>>>>,
                    request: HttpRequest, video_path: web::Path<String>) -> HttpResponse {
    let auth_response = squire::authenticator::verify_token(&request, &config);
    if !auth_response.ok {
        return routes::auth::failed_auth(auth_response, &config);
    }
    squire::logger::log_connection(&request);
    log::debug!("{}", auth_response.detail);
    let filepath = video_path.to_string();
    // True path of the video file
    let __target = config.video_source.join(&filepath);
    if !__target.exists() {
        return HttpResponse::NotFound().json(routes::auth::DetailError {
            detail: format!("'{}' was not found", filepath)
        });
    }
    // True path of the video file as a String
    let __target_str = __target.to_string_lossy().to_string();
    let __filename = __target.file_name().unwrap().to_string_lossy().to_string();
    let template = environment.lock().unwrap();
    if __target.is_file() {
        let landing = template.get_template("landing").unwrap();
        let rust_iter = squire::content::get_iter(&__target, &config.file_formats);
        let render_path = format!("/video?file={}", url_encode(&filepath));
        // Rust doesn't allow re-assignment, so might as well create a mutable variable
        // Load the default response body and re-construct with subtitles if present
        let mut response_body = landing.render(context!(
            video_title => &filepath, path => render_path,
            previous => &rust_iter.previous,
            next => &rust_iter.next,
            previous_title => &rust_iter.previous,
            next_title => &rust_iter.next,
        )).unwrap();
        let subtitle = subtitles(__target, &filepath);
        if subtitle.vtt.exists() {
            let sfx_file = format!("/track?file={}", url_encode(&subtitle.vtt_file));
            response_body = landing.render(context!(
                video_title => &filepath, path => render_path,
                previous => &rust_iter.previous,
                next => &rust_iter.next,
                previous_title => &rust_iter.previous,
                next_title => &rust_iter.next,
                track => sfx_file
            )).unwrap();
        } else if subtitle.srt.exists() {
            log::info!("Converting {:?} to {:?} for subtitles",
                subtitle.srt.file_name().unwrap(),
                subtitle.vtt.file_name().unwrap());
            match squire::subtitles::srt_to_vtt(&subtitle.srt) {
                Ok(_) => {
                    log::debug!("Successfully converted srt to vtt file");
                    let sfx_file = format!("/track?file={}", url_encode(&subtitle.vtt_file));
                    response_body = landing.render(context!(
                        video_title => &filepath, path => render_path,
                        previous => &rust_iter.previous,
                        next => &rust_iter.next,
                        previous_title => &rust_iter.previous,
                        next_title => &rust_iter.next,
                        track => sfx_file
                    )).unwrap();
                }
                Err(err) => log::error!("Failed to convert srt to vtt: {}", err),
            }
        }
        return HttpResponse::build(StatusCode::OK)
            .content_type("text/html; charset=utf-8").body(response_body);
    } else if __target.is_dir() {
        let child_dir = __target.iter().last().unwrap().to_string_lossy().to_string();
        let listing_page = squire::content::get_dir_stream_content(&__target_str, &child_dir, &config.file_formats);
        let listing = template.get_template("listing").unwrap();
        return HttpResponse::build(StatusCode::OK)
            .content_type("text/html; charset=utf-8")
            .body(listing.render(context!(
                files => listing_page.files, directories => listing_page.directories)
            ).unwrap());
    }
    log::error!("Something went really wrong");
    log::error!("Video Path: {}", filepath);
    log::error!("Target: {}", __target_str);
    HttpResponse::ExpectationFailed().json(routes::auth::DetailError {
        detail: format!("'{}' was neither a file nor a folder", filepath)
    })
}

/// Handles requests for the '/video' endpoint, serving video content for streaming.
///
/// # Arguments
///
/// * `config` - The configuration settings.
/// * `request` - The HTTP request.
/// * `info` - The query parameter containing the file information.
///
/// # Returns
///
/// Returns an `HttpResponse` containing the video content or an error response.
#[get("/video")]
pub async fn streaming_endpoint(config: web::Data<Arc<squire::settings::Config>>,
                                request: HttpRequest, info: web::Query<Payload>) -> HttpResponse {
    let auth_response = squire::authenticator::verify_token(&request, &config);
    if !auth_response.ok {
        return routes::auth::failed_auth(auth_response, &config);
    }
    squire::logger::log_connection(&request);
    let host = request.connection_info().host().to_owned();
    let video_path = config.video_source.join(&info.file);
    if video_path.exists() {
        let file = actix_files::NamedFile::open_async(video_path).await.unwrap();
        // Check if the host is making a continued connection streaming the same file
        let mut tracker = constant::HOST_SERVE.lock().unwrap();
        if tracker.get(&host).unwrap() != &info.file {
            log::info!("Streaming {}", info.file);
            tracker.insert(request.connection_info().host().to_string(), info.file.to_string());
        }
        return file.into_response(&request);
    }
    let error = format!("File {:?} not found", video_path);
    log::error!("{}", error);
    HttpResponse::NotFound().body(error)
}