Skip to content
Snippets Groups Projects
Unverified Commit b3535bad authored by Volkor Barbarian Warrior's avatar Volkor Barbarian Warrior
Browse files

feature complete engine mode 1!

\o/
parent 8873b96e
No related branches found
No related tags found
No related merge requests found
......@@ -113,7 +113,7 @@ pub async fn add_file(
// This function checks if the filename actually exists in the DB, returning true if it does, false otherwise.
// This /can/ return true if there are multiple files with the same name, as we're not really interested in checking for errors here.
pub async fn check_filename(sqlconn: &Pool<Sqlite>, filename: String) -> bool {
let result = sqlx::query("SELECT COUNT(*) FROM files WHERE file = ?")
let result = sqlx::query("SELECT COUNT(*) FROM files WHERE file = ? and isDeleted = 0")
.bind(&filename)
.fetch_one(sqlconn)
.await
......@@ -135,7 +135,7 @@ pub async fn check_filename(sqlconn: &Pool<Sqlite>, filename: String) -> bool {
}
}
// This function checks if the admin actually exists in the DB, returning a Some(filename).
// Check if the adminkey corresponds to a file, and return Some(file), or None.
pub async fn check_adminkey(sqlconn: &Pool<Sqlite>, adminkey: String) -> Option<String> {
// Make sure isDeleted = 0, so we don't try to delete files that are already deleted.
// Making this function into a 'single use' per file.
......@@ -231,7 +231,7 @@ pub async fn get_old_files(sqlconn: &Pool<Sqlite>) -> Vec<String> {
return files;
}
// Setup the files vec
// Parse the result into the vec.
for line in result.unwrap() {
tracing::debug!("oldfile_loop(filename: {})", line.file);
......@@ -247,12 +247,6 @@ fn update_expiry_override() {
// implement this I guess.
}
// Get filename from adminkey
fn get_filename_from_adminkey() {
// implement this I guess.
// SELECT file FROM files WHERE deletekey = ?
}
// File Statistics
fn get_total_files_live() {
......
////////
/// This is just for the ugly functions that we don't really need in the main.rs file.
/// I don't really know what I'm doing, so this makes it looks like im a better developer than I actually am.
////////
use nanoid::nanoid;
use salvo::Request;
use sqlx::{Pool, Sqlite};
use sqlx::{Pool, Sqlite, SqlitePool};
use std::fs;
/// This is just for the ugly functions that we don't really need in the main.rs file.
/// I don't really know what I'm doing, so this makes it looks like im a better developer than I actually am.
////////
use tokio::{
sync::OnceCell,
task,
time::{self, Duration, Interval},
};
use crate::db;
......@@ -67,15 +72,15 @@ pub async fn generate_adminkey(sqlconn: &Pool<Sqlite>) -> String {
return adminkey;
}
pub async fn calculate_expiry(sqlthingy: &Pool<Sqlite>, filename: String, filesize: i32) -> i32 {
pub async fn calculate_expiry(sqlconn: &Pool<Sqlite>, filename: String, filesize: i32) -> i32 {
// TODO: Get config stuffs, so we can figure out what engine mode!
let engine = 1;
// Bruh ChatGPT coming in clutch here.
let expiry = match engine {
1 => Some(engine_1(sqlthingy, filename).await),
2 => Some(engine_2(sqlthingy, filename, filesize).await),
3 => Some(engine_3(sqlthingy, filename, filesize).await),
1 => Some(engine_1(sqlconn, filename).await),
2 => Some(engine_2(sqlconn, filename, filesize).await),
3 => Some(engine_3(sqlconn, filename, filesize).await),
_ => {
tracing::error!("Unknown engine mode: {}", engine);
std::process::exit(2);
......@@ -85,13 +90,13 @@ pub async fn calculate_expiry(sqlthingy: &Pool<Sqlite>, filename: String, filesi
}
// This engine works on accesstime + file_expiry_min
async fn engine_1(sqlthingy: &Pool<Sqlite>, filename: String) -> i32 {
async fn engine_1(sqlconn: &Pool<Sqlite>, filename: String) -> i32 {
// TODO: Read this from config
const file_expiry_min: i32 = 168;
const file_expiry_min: i32 = 604800; // 7 days in seconds.
// Get the last time the file was viewed (in unix time)
let accesstime = db::get_accesss_time(sqlthingy, filename).await;
let accesstime = db::get_accesss_time(sqlconn, filename).await;
// convert the file_expiry_min (which is in hours) into seconds
let expiry_seconds = file_expiry_min * 60 * 60;
let expiry_seconds = file_expiry_min;
// Now we can add expiry_seconds to accesstime, to get 7 days in the future!
return accesstime + expiry_seconds;
......
......@@ -9,8 +9,9 @@ use chrono::{DateTime, TimeZone, Utc};
use rand::Rng;
use std::fs::create_dir_all;
use std::path::Path;
use std::time::SystemTime;
use std::time::{Duration, SystemTime};
use std::{env, fs};
use tokio::{task, time};
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::fmt;
use tracing_subscriber::prelude::*;
......@@ -352,6 +353,9 @@ async fn main() {
// Setup the cleaner thread!
// Get the engine mode from the config
let interval = 1; // 30 Minutes
// Will awaiting on this wait until the loop is finished? I hope not....
cleaner_thread(interval);
// Create the tables if they don't already exist
let (filesdb, qrscandb) = tokio::join!(
......@@ -375,7 +379,7 @@ async fn main() {
.push(Router::with_path("/faq").get(serve_static))
.push(Router::with_path("/dmca").get(serve_static))
.push(Router::with_path("/welcome").get(serve_static))
.push(Router::with_path("/favicon.ico").get(StaticFile::new("static/favicon32.png")))
.push(Router::with_path("/favicon.ico").get(StaticFile::new("static/favicon32.webp")))
// Static File Serving
.push(
Router::with_path("static/<**path>").get(StaticDir::new("static/").with_listing(true)),
......@@ -402,3 +406,28 @@ async fn main() {
.serve(router)
.await;
}
// This spawns a tokio task to run a interval timer forever.
// the interval timer runs every 'period' seconds.
pub fn cleaner_thread(period: i32) {
let forever = task::spawn(async move {
let mut interval = time::interval(Duration::from_secs(period.try_into().unwrap()));
let sqlconn = SQLITE.get().unwrap();
loop {
// Wait for the next interval
interval.tick().await;
// Get a vec of files that will expire this loop.
// Also I'm sorta just realising how annoying it is to pass sqlconn through like 4 functions deep just to do something.
let old_files = db::get_old_files(sqlconn).await;
// For each file in old_files, delete them.
for file in old_files {
db::delete_file(sqlconn, file.clone()).await;
let files = engine::delete_file(file.clone()).await;
if files.is_err() {
tracing::info!("Failed to delete file: {:?}", file);
}
}
tracing::info!("Cleaner finished")
}
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment