- Removing partial module from streaming branch
+ Moving DB_PATH into db module
This commit is contained in:
parent
0d9991c557
commit
88a938d0c2
@ -4,13 +4,18 @@
|
|||||||
|
|
||||||
// WARN: at the moment there are no guarantees as far as data integrity is
|
// WARN: at the moment there are no guarantees as far as data integrity is
|
||||||
// concerned. This means there are no real transactions
|
// concerned. This means there are no real transactions
|
||||||
|
use std::env;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::prelude::Write;
|
|
||||||
use std::io::{BufWriter, BufReader};
|
use std::io::{BufWriter, BufReader};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use rocket::serde::{Serialize, Deserialize};
|
use rocket::serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref DB_PATH: String = {
|
||||||
|
env::var("DB_PATH").unwrap_or("keys.db".into())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
@ -83,7 +88,6 @@ impl Database {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod db_tests {
|
mod db_tests {
|
||||||
use super::Database;
|
use super::Database;
|
||||||
use rocket::tokio;
|
|
||||||
|
|
||||||
const DB: &'static str = "new.db";
|
const DB: &'static str = "new.db";
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ mod category;
|
|||||||
mod video;
|
mod video;
|
||||||
mod thumbnail;
|
mod thumbnail;
|
||||||
mod common;
|
mod common;
|
||||||
mod partial;
|
|
||||||
|
|
||||||
#[cfg(feature = "admin")]
|
#[cfg(feature = "admin")]
|
||||||
mod admin;
|
mod admin;
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
use rocket::http::Status;
|
|
||||||
use rocket::request::{Outcome, Request, FromRequest};
|
|
||||||
|
|
||||||
// Only single byte ranges are supported right now
|
|
||||||
pub struct Range {
|
|
||||||
start: u32,
|
|
||||||
end: Option<u32>
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum RangeError {
|
|
||||||
Invalid,
|
|
||||||
Malformed
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[rocket::async_trait]
|
|
||||||
impl<'r> FromRequest<'r> for Range {
|
|
||||||
type Error = RangeError;
|
|
||||||
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
||||||
// No range basically just means get the whole file
|
|
||||||
let range_raw = req.headers().get_one("range");
|
|
||||||
if range_raw.is_none() {
|
|
||||||
return Outcome::Success(Self { start: 0, end: None })
|
|
||||||
}
|
|
||||||
let range_raw = range_raw.unwrap();
|
|
||||||
|
|
||||||
// First make sure that we only ever handle byte ranges
|
|
||||||
const PREFIX: &'static str = "bytes=";
|
|
||||||
if !range_raw.starts_with(PREFIX) {
|
|
||||||
return Outcome::Failure((Status::BadRequest, RangeError::Invalid))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next strip the 'bytes=' out and grab the only everything until either
|
|
||||||
// ',' <whitespace> <end-of-line>
|
|
||||||
// ! Strip the comma if its there so we end up with a slice that matches
|
|
||||||
// [0-9]+-[0-9]+
|
|
||||||
let range_raw = range_raw
|
|
||||||
.strip_prefix(PREFIX).unwrap();
|
|
||||||
|
|
||||||
if let Some(ranges) = range_raw.split_once('-') {
|
|
||||||
let start = ranges.0.parse::<u32>();
|
|
||||||
let end = ranges.1.parse::<u32>().ok();
|
|
||||||
println!("Range collected {:?} -> {:?}", start, end);
|
|
||||||
match (start, end) {
|
|
||||||
(Ok(start), Some(end)) => {
|
|
||||||
if end <= start {
|
|
||||||
println!("Start end range invalid {}->{}", start, end);
|
|
||||||
return Outcome::Failure((Status::BadRequest, RangeError::Malformed))
|
|
||||||
}
|
|
||||||
return Outcome::Success(Self {
|
|
||||||
start, end: Some(end)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
(Ok(start), None) => {
|
|
||||||
return Outcome::Success(Self { start, end: None })
|
|
||||||
}
|
|
||||||
_ => Outcome::Failure((Status::BadRequest, RangeError::Invalid))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Outcome::Failure((Status::BadRequest, RangeError::Invalid));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user