Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Ephemeral
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Volkor Barbarian Warrior
Ephemeral
Commits
b3535bad
Unverified
Commit
b3535bad
authored
2 years ago
by
Volkor Barbarian Warrior
Browse files
Options
Downloads
Patches
Plain Diff
feature complete engine mode 1!
\o/
parent
8873b96e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/db.rs
+3
-9
3 additions, 9 deletions
src/db.rs
src/engine.rs
+17
-12
17 additions, 12 deletions
src/engine.rs
src/main.rs
+31
-2
31 additions, 2 deletions
src/main.rs
with
51 additions
and
23 deletions
src/db.rs
+
3
−
9
View file @
b3535bad
...
...
@@ -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 c
heck
s
if the admin
actually exists in the DB,
return
ing a
Some(file
name)
.
//
C
heck if the admin
key 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 diff is collapsed.
Click to expand it.
src/engine.rs
+
17
−
12
View file @
b3535bad
////////
/// 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
(
sql
thingy
:
&
Pool
<
Sqlite
>
,
filename
:
String
,
filesize
:
i32
)
->
i32
{
pub
async
fn
calculate_expiry
(
sql
conn
:
&
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
(
sql
thingy
,
filename
)
.await
),
2
=>
Some
(
engine_2
(
sql
thingy
,
filename
,
filesize
)
.await
),
3
=>
Some
(
engine_3
(
sql
thingy
,
filename
,
filesize
)
.await
),
1
=>
Some
(
engine_1
(
sql
conn
,
filename
)
.await
),
2
=>
Some
(
engine_2
(
sql
conn
,
filename
,
filesize
)
.await
),
3
=>
Some
(
engine_3
(
sql
conn
,
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
(
sql
thingy
:
&
Pool
<
Sqlite
>
,
filename
:
String
)
->
i32
{
async
fn
engine_1
(
sql
conn
:
&
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
(
sql
thingy
,
filename
)
.await
;
let
accesstime
=
db
::
get_accesss_time
(
sql
conn
,
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
;
...
...
This diff is collapsed.
Click to expand it.
src/main.rs
+
31
−
2
View file @
b3535bad
...
...
@@ -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"
)
}
});
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment