Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sparkplug
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephen D
Sparkplug
Commits
2026185d
Commit
2026185d
authored
3 years ago
by
Stephen D
Browse files
Options
Downloads
Plain Diff
Merge branch 'file_size_limit' into 'master'
Add file size limit for downloads Closes
#8
See merge request
!8
parents
cdbe8ef8
f8301bbf
No related branches found
No related tags found
1 merge request
!8
Add file size limit for downloads
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
debian/config.toml
+1
-0
1 addition, 0 deletions
debian/config.toml
src/bin/worker.rs
+5
-9
5 additions, 9 deletions
src/bin/worker.rs
src/config.rs
+3
-0
3 additions, 0 deletions
src/config.rs
src/download.rs
+15
-0
15 additions, 0 deletions
src/download.rs
src/lib.rs
+1
-0
1 addition, 0 deletions
src/lib.rs
with
25 additions
and
9 deletions
debian/config.toml
+
1
−
0
View file @
2026185d
...
...
@@ -4,6 +4,7 @@ key = "changeme"
server_name
=
"http://localhost/content/"
port
=
3030
max_retries
=
3
max_size_bytes
=
10_000_000
[[formats]]
name
=
"original"
...
...
This diff is collapsed.
Click to expand it.
src/bin/worker.rs
+
5
−
9
View file @
2026185d
use
crate
::
config
::
Format
;
use
bytes
::
Bytes
;
use
futures
::
StreamExt
;
use
image
::
imageops
::
FilterType
;
use
image
::
io
::
Reader
as
ImageReader
;
use
image
::
GenericImageView
;
use
sparkplug
::
config
;
use
sparkplug
::
download
;
use
sqlx
::
PgPool
;
use
std
::
fs
::
File
;
use
std
::
io
::
prelude
::
*
;
...
...
@@ -12,13 +12,6 @@ use std::io::Cursor;
use
std
::
path
::
Path
;
use
tokio
::
task
;
async
fn
download_file
(
url
:
&
str
)
->
Option
<
Bytes
>
{
let
resp
=
reqwest
::
get
(
url
)
.await
.ok
()
?
;
let
content
=
resp
.bytes
()
.await
.ok
()
?
;
Some
(
content
)
}
fn
write_file
(
path
:
&
Path
,
bytes
:
&
[
u8
])
->
Option
<
()
>
{
let
mut
file
=
File
::
create
(
path
)
.ok
()
?
;
file
.write_all
(
bytes
)
.ok
()
?
;
...
...
@@ -28,11 +21,12 @@ fn write_file(path: &Path, bytes: &[u8]) -> Option<()> {
async
fn
process_file
(
url
:
&
str
,
max_size
:
usize
,
output_directory
:
String
,
output_id
:
String
,
formats
:
Vec
<
Format
>
,
)
->
Option
<
String
>
{
let
content
=
download
_file
(
url
)
.await
?
;
let
content
=
download
::
download_file
(
url
,
max_size
)
.await
?
;
let
extension
=
task
::
spawn_blocking
(
move
||
{
let
reader
=
ImageReader
::
new
(
Cursor
::
new
(
content
))
...
...
@@ -71,6 +65,7 @@ async fn main() {
let
config
=
config
::
load_config
();
let
image_directory
=
&
config
.image_directory
;
let
formats
=
&
config
.formats
;
let
max_size
=
&
config
.max_size_bytes
;
let
pool
=
PgPool
::
connect
(
&
config
.database_url
)
.await
...
...
@@ -101,6 +96,7 @@ LIMIT 1000
|(
row
,
pool
)|
async
move
{
match
process_file
(
&
row
.original_url
,
*
max_size
,
image_directory
.to_string
(),
row
.id
.to_string
(),
formats
.clone
(),
...
...
This diff is collapsed.
Click to expand it.
src/config.rs
+
3
−
0
View file @
2026185d
...
...
@@ -10,6 +10,7 @@ struct TomlConfig {
pub
server_name
:
String
,
pub
port
:
u16
,
pub
max_retries
:
u16
,
pub
max_size_bytes
:
usize
,
pub
formats
:
Vec
<
Format
>
,
}
...
...
@@ -29,6 +30,7 @@ pub struct Config {
pub
server_name
:
Url
,
pub
port
:
u16
,
pub
max_retries
:
i32
,
pub
max_size_bytes
:
usize
,
pub
formats
:
Vec
<
Format
>
,
}
...
...
@@ -45,6 +47,7 @@ impl From<TomlConfig> for Config {
.expect
(
"Invalid server_name - Not a valid URL"
),
port
:
config
.port
,
max_retries
:
config
.max_retries
as
i32
,
max_size_bytes
:
config
.max_size_bytes
,
formats
:
config
.formats
,
}
...
...
This diff is collapsed.
Click to expand it.
src/download.rs
0 → 100644
+
15
−
0
View file @
2026185d
pub
async
fn
download_file
(
url
:
&
str
,
max_size
:
usize
)
->
Option
<
Vec
<
u8
>>
{
let
mut
resp
=
reqwest
::
get
(
url
)
.await
.ok
()
?
;
let
mut
body_bytes
=
vec!
[];
let
mut
len
=
0
;
while
let
Some
(
chunk
)
=
resp
.chunk
()
.await
.ok
()
?
{
len
+=
chunk
.len
();
if
len
>
max_size
{
return
None
;
}
body_bytes
.extend
(
chunk
);
}
Some
(
body_bytes
)
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
1
−
0
View file @
2026185d
pub
mod
config
;
pub
mod
download
;
pub
mod
ext
;
pub
mod
routes
;
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