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
8e4b4d6c
Verified
Commit
8e4b4d6c
authored
1 year ago
by
Volkor Barbarian Warrior
Browse files
Options
Downloads
Patches
Plain Diff
fix: list the files in a more sensible way
it even generates an nice error!
parent
2b7251fd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/handlers/list_files.rs
+55
-29
55 additions, 29 deletions
src/handlers/list_files.rs
src/handlers/mod.rs
+11
-1
11 additions, 1 deletion
src/handlers/mod.rs
with
66 additions
and
30 deletions
src/handlers/list_files.rs
+
55
−
29
View file @
8e4b4d6c
use
salvo
::{
handler
,
Request
,
Response
,
hyper
::
header
::
HOST
,
prelude
::
StatusCode
};
use
crate
::{
db
,
SQLITE
,
handlers
::{
TemplateStruct
,
render_template
,
convert_file_size
,
convert_unix_timestamp
}};
use
crate
::{
db
,
SQLITE
,
handlers
::{
TemplateStruct
,
render_template
,
convert_file_size
,
convert_unix_timestamp
,
count_file_metrics
}};
use
super
::
guess_ip
;
...
...
@@ -24,40 +24,66 @@ pub async fn list_files(req: &mut Request, res: &mut Response) {
let
mut
html
=
String
::
new
();
// For each file in the list, add the html to the rendered string
for
f
in
files
.unwrap
()
{
// Change the filename to a file url
let
fileurl
=
format!
(
"/{}"
,
&
f
.filename
);
// Change the filesize to a human readable value
let
human_size
=
convert_file_size
(
f
.filesize
);
// Change the expiry to a human readable value
let
human_time
=
convert_unix_timestamp
(
f
.expiry
);
// If isDeleted, change colour to red, else green
let
mut
colour
:
String
=
String
::
new
();
if
f
.is_deleted
{
colour
=
"background-color: #ff00000a;"
.to_string
()
}
else
{
colour
=
"background-color: #00ff100a;"
.to_string
()
match
files
.as_ref
()
{
Ok
(
file_metrics
)
=>
{
for
f
in
file_metrics
{
// Change the filename to a file url
let
fileurl
=
format!
(
"/{}"
,
&
f
.filename
);
// Change the filesize to a human readable value
let
human_size
=
convert_file_size
(
f
.filesize
);
// Change the expiry to a human readable value
let
human_time
=
convert_unix_timestamp
(
f
.expiry
);
// If isDeleted, change colour to red, else green
let
mut
colour
:
String
=
String
::
new
();
if
f
.is_deleted
{
colour
=
"background-color: #ff00000a;"
.to_string
()
}
else
{
colour
=
"background-color: #00ff100a;"
.to_string
()
}
// Add a new file to the rendered string
html
.push_str
(
&
format!
(
"<tr style='{}'><td><a href={}>{}</a></td><td data-sort='{}'>{}</td><td>{}</td><td>{}</td><td data-sort='{}'>{}</td></tr>"
,
colour
,
fileurl
.as_str
(),
f
.filename
.as_str
(),
f
.filesize
,
human_size
.as_str
(),
f
.mimetype
.as_str
(),
f
.views
.to_owned
(),
f
.expiry
.to_owned
(),
human_time
.to_owned
(),
));
}
}
Err
(
err
)
=>
{
// Log error to console
tracing
::
error!
(
"Error retrieving file metrics: {:?}"
,
err
);
// Add a new file to the rendered string
html
.push_str
(
&
format!
(
"<tr style='{}'><td><a href={}>{}</a></td><td data-sort='{}'>{}</td><td>{}</td><td>{}</td><td data-sort='{}'>{}</td></tr>"
,
colour
,
fileurl
.as_str
(),
f
.filename
.as_str
(),
f
.filesize
,
human_size
.as_str
(),
f
.mimetype
.as_str
(),
f
.views
.to_owned
(),
f
.expiry
.to_owned
(),
human_time
.to_owned
(),
));
// Render a nice error page for the poor user
let
template_filename
=
"error.html"
;
let
template
=
TemplateStruct
{
domain
:
String
::
from
(
headers
[
HOST
]
.to_str
()
.unwrap
()),
message1
:
String
::
from
(
"Error 500: Internal Server Error"
),
message2
:
String
::
from
(
"Something went wrong while reading the result from the database, uh oh :("
,
),
..
Default
::
default
()
};
let
status_code
=
StatusCode
::
INTERNAL_SERVER_ERROR
;
render_template
(
res
,
headers
,
template_filename
,
template
,
status_code
)
.await
;
}
}
// Close the table when all files are 'rendered'
html
.push_str
(
"</table>"
);
// Log this to the server console
tracing
::
info!
(
"New Request: /my_files ({} Files"
,
count_file_metrics
(
&
files
)
);
let
template_filename
=
"my_files.html"
;
let
template
=
TemplateStruct
{
domain
:
String
::
from
(
headers
[
HOST
]
.to_str
()
.unwrap
()),
...
...
This diff is collapsed.
Click to expand it.
src/handlers/mod.rs
+
11
−
1
View file @
8e4b4d6c
...
...
@@ -10,8 +10,9 @@ use salvo::{
};
use
chrono
::{
DateTime
,
Utc
};
use
chrono_humanize
::
Humanize
;
use
sqlx
::
Error
;
use
crate
::
CONFIG
;
use
crate
::
{
CONFIG
,
db
::
FileMetric
}
;
// Submodules for each request handler
pub
mod
delete_file
;
...
...
@@ -197,4 +198,13 @@ fn convert_unix_timestamp(unix_timestamp: i64) -> String {
let
relative_date
=
datetime
.humanize
();
relative_date
.to_string
()
}
/// Calculate the number of files in the FileMetric result
// I'm lazy so this doesn't really need to be it's own function but it was easier to do this.
fn
count_file_metrics
(
file_metrics
:
&
Result
<
Vec
<
FileMetric
>
,
Error
>
)
->
usize
{
match
file_metrics
{
Ok
(
metrics
)
=>
metrics
.len
(),
Err
(
_
)
=>
0
,
}
}
\ No newline at end of file
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