|
|
|
@ -9,12 +9,16 @@ use std::{
@@ -9,12 +9,16 @@ use std::{
|
|
|
|
|
use std::os::unix::fs::DirBuilderExt; |
|
|
|
|
|
|
|
|
|
use dashmap::DashMap; |
|
|
|
|
|
|
|
|
|
const TEXT_DIR: String = "text".to_string(); |
|
|
|
|
const FILE_DIR: String = "files".to_string(); |
|
|
|
|
const REDIRECTS_FILE: String = "redirects".to_string(); |
|
|
|
|
const STATS_FILE: String = "stats".to_string(); |
|
|
|
|
const USERS_FILE: String = "users".to_string(); |
|
|
|
|
use lazy_static::lazy_static; |
|
|
|
|
|
|
|
|
|
lazy_static! { |
|
|
|
|
static ref TEXT_DIR: String = std::env::var("V_TEXT_DIR").unwrap_or("text".to_string()); |
|
|
|
|
static ref FILE_DIR: String = std::env::var("V_FILE_DIR").unwrap_or("files".to_string()); |
|
|
|
|
static ref REDIRECTS_FILE: String = |
|
|
|
|
std::env::var("V_REDIRECTS_FILE").unwrap_or("redirects".to_string()); |
|
|
|
|
static ref STATS_FILE: String = std::env::var("V_STATS_FILE").unwrap_or("stats".to_string()); |
|
|
|
|
static ref USERS_FILE: String = std::env::var("V_USERS_FILE").unwrap_or("users".to_string()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)] |
|
|
|
|
pub enum Error { |
|
|
|
@ -79,6 +83,7 @@ struct UserData {
@@ -79,6 +83,7 @@ struct UserData {
|
|
|
|
|
pub pw_hash: String, |
|
|
|
|
pub is_admin: bool, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl UserData { |
|
|
|
|
pub fn parse(line: &str, is_admin: bool) -> Result<Self, Error> { |
|
|
|
|
let (username, pw_hash) = line.split_once(": ").ok_or(Error::Metadata)?; |
|
|
|
@ -130,8 +135,8 @@ impl DataStorage {
@@ -130,8 +135,8 @@ impl DataStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dir_builder.create(self.base_dir)?; |
|
|
|
|
dir_builder.create(self.base_dir.join(FILE_DIR))?; |
|
|
|
|
dir_builder.create(self.base_dir.join(TEXT_DIR))?; |
|
|
|
|
dir_builder.create(self.base_dir.join(*FILE_DIR))?; |
|
|
|
|
dir_builder.create(self.base_dir.join(*TEXT_DIR))?; |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
@ -140,7 +145,7 @@ impl DataStorage {
@@ -140,7 +145,7 @@ impl DataStorage {
|
|
|
|
|
let f = File::options() |
|
|
|
|
.create(true) |
|
|
|
|
.read(true) |
|
|
|
|
.open(self.base_dir.join(REDIRECTS_FILE))?; |
|
|
|
|
.open(self.base_dir.join(*REDIRECTS_FILE))?; |
|
|
|
|
|
|
|
|
|
// TODO: replace with meta_str_to_map and then self.redirects.extend
|
|
|
|
|
|
|
|
|
@ -156,7 +161,7 @@ impl DataStorage {
@@ -156,7 +161,7 @@ impl DataStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn load_text(&mut self) -> Result<(), Error> { |
|
|
|
|
for thing in std::fs::read_dir(self.base_dir.join(TEXT_DIR)).expect("fs error") { |
|
|
|
|
for thing in std::fs::read_dir(self.base_dir.join(*TEXT_DIR)).expect("fs error") { |
|
|
|
|
let dir_entry = thing?; // i'm so good at naming things
|
|
|
|
|
let id = dir_entry.file_name().into_string().or(Err(Error::FS))?; |
|
|
|
|
let (raw_metadata, text) = read_to_string(dir_entry.path())? |
|
|
|
@ -175,7 +180,7 @@ impl DataStorage {
@@ -175,7 +180,7 @@ impl DataStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn load_files(&mut self) -> Result<(), Error> { |
|
|
|
|
for thing in std::fs::read_dir(self.base_dir.join(FILE_DIR)).expect("fs error") { |
|
|
|
|
for thing in std::fs::read_dir(self.base_dir.join(*FILE_DIR)).expect("fs error") { |
|
|
|
|
let dir_entry = thing?; // i'm so good at naming things
|
|
|
|
|
let filename = dir_entry |
|
|
|
|
.file_name() |
|
|
|
@ -193,7 +198,7 @@ impl DataStorage {
@@ -193,7 +198,7 @@ impl DataStorage {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn assert_all_files_present(&self) -> Result<(), Error> { |
|
|
|
|
let files_folder = self.base_dir.join(FILE_DIR); |
|
|
|
|
let files_folder = self.base_dir.join(*FILE_DIR); |
|
|
|
|
for (id, _) in self.files.into_iter() { |
|
|
|
|
files_folder |
|
|
|
|
.join(id) |
|
|
|
@ -205,7 +210,7 @@ impl DataStorage {
@@ -205,7 +210,7 @@ impl DataStorage {
|
|
|
|
|
|
|
|
|
|
fn load_users(&mut self) -> Result<(), Error> { |
|
|
|
|
let is_admin = true; |
|
|
|
|
for line in read_to_string(self.base_dir.join(USERS_FILE))?.lines() { |
|
|
|
|
for line in read_to_string(self.base_dir.join(*USERS_FILE))?.lines() { |
|
|
|
|
if line.starts_with("---") { |
|
|
|
|
is_admin = false; |
|
|
|
|
break; |
|
|
|
|