Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
espresso
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor 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
Eduardo Trujillo
espresso
Commits
10449e6e
Commit
10449e6e
authored
4 years ago
by
Eduardo Trujillo
Browse files
Options
Downloads
Patches
Plain Diff
Address formatting issues
parent
92a5f064
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1
Rust Rewrite
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/files/chunked.rs
+67
-67
67 additions, 67 deletions
src/files/chunked.rs
with
67 additions
and
67 deletions
src/files/chunked.rs
+
67
−
67
View file @
10449e6e
use
actix_web
::{
error
::{
BlockingError
,
Error
,
ErrorInternalServerError
},
web
,
error
::{
BlockingError
,
Error
,
ErrorInternalServerError
},
web
,
};
use
bytes
::
Bytes
;
use
futures_core
::
Stream
;
use
futures_util
::
future
::{
FutureExt
,
LocalBoxFuture
};
use
io
::
Seek
;
use
std
::{
cmp
,
fs
::
File
,
future
::
Future
,
io
,
io
::
Read
,
pin
::
Pin
,
task
::{
Context
,
Poll
},
cmp
,
fs
::
File
,
future
::
Future
,
io
,
io
::
Read
,
pin
::
Pin
,
task
::{
Context
,
Poll
},
};
fn
handle_error
(
err
:
BlockingError
<
io
::
Error
>
)
->
Error
{
match
err
{
BlockingError
::
Error
(
err
)
=>
err
.into
(),
BlockingError
::
Canceled
=>
ErrorInternalServerError
(
"Unexpected error"
),
}
match
err
{
BlockingError
::
Error
(
err
)
=>
err
.into
(),
BlockingError
::
Canceled
=>
ErrorInternalServerError
(
"Unexpected error"
),
}
}
#[doc(hidden)]
/// A helper created from a `std::fs::File` which reads the file
/// chunk-by-chunk on a `ThreadPool`.
pub
struct
ChunkedReadFile
{
size
:
u64
,
offset
:
u64
,
file
:
Option
<
File
>
,
fut
:
Option
<
LocalBoxFuture
<
'static
,
Result
<
(
File
,
Bytes
),
BlockingError
<
io
::
Error
>>>>
,
counter
:
u64
,
}
impl
ChunkedReadFile
{
pub
fn
new
(
size
:
u64
,
offset
:
u64
,
file
:
Option
<
File
>
,
fut
:
Option
<
LocalBoxFuture
<
'static
,
Result
<
(
File
,
Bytes
),
BlockingError
<
io
::
Error
>>>>
,
counter
:
u64
,
}
impl
ChunkedReadFile
{
pub
fn
new
(
size
:
u64
,
offset
:
u64
,
file
:
Option
<
File
>
,
fut
:
Option
<
LocalBoxFuture
<
'static
,
Result
<
(
File
,
Bytes
),
BlockingError
<
io
::
Error
>>>>
,
counter
:
u64
,
)
->
ChunkedReadFile
{
ChunkedReadFile
{
size
,
offset
,
file
,
fut
,
counter
,
}
)
->
ChunkedReadFile
{
ChunkedReadFile
{
size
,
offset
,
file
,
fut
,
counter
,
}
}
}
impl
Stream
for
ChunkedReadFile
{
type
Item
=
Result
<
Bytes
,
Error
>
;
type
Item
=
Result
<
Bytes
,
Error
>
;
fn
poll_next
(
mut
self
:
Pin
<&
mut
Self
>
,
cx
:
&
mut
Context
)
->
Poll
<
Option
<
Self
::
Item
>>
{
if
let
Some
(
ref
mut
fut
)
=
self
.fut
{
return
match
Pin
::
new
(
fut
)
.poll
(
cx
)
{
Poll
::
Ready
(
Ok
((
file
,
bytes
)))
=>
{
self
.fut
.take
();
self
.file
=
Some
(
file
);
self
.offset
+=
bytes
.len
()
as
u64
;
self
.counter
+=
bytes
.len
()
as
u64
;
Poll
::
Ready
(
Some
(
Ok
(
bytes
)))
}
Poll
::
Ready
(
Err
(
e
))
=>
Poll
::
Ready
(
Some
(
Err
(
handle_error
(
e
)))),
Poll
::
Pending
=>
Poll
::
Pending
,
};
fn
poll_next
(
mut
self
:
Pin
<&
mut
Self
>
,
cx
:
&
mut
Context
)
->
Poll
<
Option
<
Self
::
Item
>>
{
if
let
Some
(
ref
mut
fut
)
=
self
.fut
{
return
match
Pin
::
new
(
fut
)
.poll
(
cx
)
{
Poll
::
Ready
(
Ok
((
file
,
bytes
)))
=>
{
self
.fut
.take
();
self
.file
=
Some
(
file
);
self
.offset
+=
bytes
.len
()
as
u64
;
self
.counter
+=
bytes
.len
()
as
u64
;
Poll
::
Ready
(
Some
(
Ok
(
bytes
)))
}
Poll
::
Ready
(
Err
(
e
))
=>
Poll
::
Ready
(
Some
(
Err
(
handle_error
(
e
)))),
Poll
::
Pending
=>
Poll
::
Pending
,
};
}
let
size
=
self
.size
;
let
offset
=
self
.offset
;
let
counter
=
self
.counter
;
let
size
=
self
.size
;
let
offset
=
self
.offset
;
let
counter
=
self
.counter
;
if
size
==
counter
{
Poll
::
Ready
(
None
)
}
else
{
let
mut
file
=
self
.file
.take
()
.expect
(
"Use after completion"
);
self
.fut
=
Some
(
web
::
block
(
move
||
{
let
max_bytes
:
usize
;
max_bytes
=
cmp
::
min
(
size
.saturating_sub
(
counter
),
65_536
)
as
usize
;
let
mut
buf
=
Vec
::
with_capacity
(
max_bytes
);
file
.seek
(
io
::
SeekFrom
::
Start
(
offset
))
?
;
let
nbytes
=
file
.by_ref
()
.take
(
max_bytes
as
u64
)
.read_to_end
(
&
mut
buf
)
?
;
if
nbytes
==
0
{
return
Err
(
io
::
ErrorKind
::
UnexpectedEof
.into
());
}
Ok
((
file
,
Bytes
::
from
(
buf
)))
})
.boxed_local
(),
);
self
.poll_next
(
cx
)
}
if
size
==
counter
{
Poll
::
Ready
(
None
)
}
else
{
let
mut
file
=
self
.file
.take
()
.expect
(
"Use after completion"
);
self
.fut
=
Some
(
web
::
block
(
move
||
{
let
max_bytes
:
usize
;
max_bytes
=
cmp
::
min
(
size
.saturating_sub
(
counter
),
65_536
)
as
usize
;
let
mut
buf
=
Vec
::
with_capacity
(
max_bytes
);
file
.seek
(
io
::
SeekFrom
::
Start
(
offset
))
?
;
let
nbytes
=
file
.by_ref
()
.take
(
max_bytes
as
u64
)
.read_to_end
(
&
mut
buf
)
?
;
if
nbytes
==
0
{
return
Err
(
io
::
ErrorKind
::
UnexpectedEof
.into
());
}
Ok
((
file
,
Bytes
::
from
(
buf
)))
})
.boxed_local
(),
);
self
.poll_next
(
cx
)
}
}
}
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