Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
memoriav
Memobase 2020
services
Import Process
Media Distributor Service
Commits
f7e9de53
Unverified
Commit
f7e9de53
authored
Jan 12, 2021
by
Sebastian Schüpbach
Browse files
implement basic functionality
parent
6cdd0344
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main.rs
View file @
f7e9de53
...
@@ -13,16 +13,17 @@ use std::collections::HashMap;
...
@@ -13,16 +13,17 @@ use std::collections::HashMap;
use
std
::
path
::
Path
;
use
std
::
path
::
Path
;
use
crate
::
media_folder_utils
::{
visit_dirs
,
MediaFileCache
};
use
crate
::
media_folder_utils
::{
visit_dirs
,
MediaFileCache
};
use
crate
::
service
::
MakeSvc
;
use
crate
::
service
::
MakeSvc
;
use
tokio
::
time
::
Duration
;
#[tokio::main]
#[tokio::main]
async
fn
main
()
->
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
{
async
fn
main
()
->
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
{
let
addr
=
SocketAddr
::
from
(([
127
,
0
,
0
,
1
],
3000
));
let
addr
=
SocketAddr
::
from
(([
127
,
0
,
0
,
1
],
3000
));
// TODO: Read path from settings file or args
let
args
=
env
::
args
();
let
args
=
env
::
args
();
let
base_dir
=
args
.skip
(
1
)
.take
(
1
)
.collect
::
<
String
>
();
let
media_cache
=
visit_dirs
(
&
Path
::
new
(
&
base_dir
))
.unwrap
();
let
server
=
Server
::
bind
(
&
addr
)
.serve
(
MakeSvc
{});
let
server
=
Server
::
bind
(
&
addr
)
.serve
(
MakeSvc
{
base_dir
:
args
.skip
(
1
)
.take
(
1
)
.collect
::
<
String
>
(),
outdated_after
:
Duration
::
new
(
7200
,
0
),
});
if
let
Err
(
e
)
=
server
.await
{
if
let
Err
(
e
)
=
server
.await
{
eprintln!
(
"server error: {}"
,
e
);
eprintln!
(
"server error: {}"
,
e
);
...
...
src/media_folder_utils.rs
View file @
f7e9de53
...
@@ -19,9 +19,9 @@ impl MediaFileCache {
...
@@ -19,9 +19,9 @@ impl MediaFileCache {
}
}
}
}
pub
fn
is_outdated
(
&
self
,
outdated_after
:
Duration
)
->
bool
{
pub
fn
is_outdated
(
&
self
,
outdated_after
:
&
Duration
)
->
bool
{
let
now
=
Instant
::
now
();
let
now
=
Instant
::
now
();
now
.duration_since
(
self
.created_on
)
>=
outdated_after
now
.duration_since
(
self
.created_on
)
>=
*
outdated_after
}
}
pub
fn
merge
(
&
mut
self
,
other
:
MediaFileCache
)
->
()
{
pub
fn
merge
(
&
mut
self
,
other
:
MediaFileCache
)
->
()
{
...
...
src/service.rs
View file @
f7e9de53
use
hyper
::{
Body
,
Request
,
Response
,
StatusCode
};
use
hyper
::{
Body
,
Request
,
Response
,
StatusCode
};
use
hyper
::
service
::
Service
;
use
hyper
::
service
::
Service
;
use
crate
::
media_folder_utils
::
MediaFileCache
;
use
crate
::
media_folder_utils
::
{
MediaFileCache
,
visit_dirs
}
;
use
std
::
future
::
Future
;
use
std
::
future
::
Future
;
use
std
::
pin
::
Pin
;
use
std
::
pin
::
Pin
;
use
std
::
task
::{
Context
,
Poll
};
use
std
::
task
::{
Context
,
Poll
};
use
std
::
time
::
Duration
;
use
std
::
path
::
Path
;
pub
struct
Svc
{
pub
struct
Svc
{
media_cache
:
MediaFileCache
,
media_cache
:
MediaFileCache
,
base_dir
:
String
,
outdated_after
:
Duration
,
}
impl
Svc
{
fn
fetch_file
(
&
mut
self
,
id
:
String
,
file_type
:
&
str
)
->
Result
<
Response
<
Body
>
,
hyper
::
Error
>
{
if
self
.media_cache
.is_outdated
(
&
self
.outdated_after
)
{
let
base_dir
=
self
.base_dir
.clone
();
self
.media_cache
=
visit_dirs
(
Path
::
new
(
&
base_dir
))
.unwrap
()
}
match
self
.media_cache
.get_file_path
(
&
id
,
file_type
)
{
Some
(
p
)
=>
Ok
(
Response
::
new
(
Body
::
from
(
p
))),
None
=>
{
let
mut
not_found
=
Response
::
default
();
*
not_found
.status_mut
()
=
StatusCode
::
NOT_FOUND
;
Ok
(
not_found
)
}
}
}
}
}
impl
Service
<
Request
<
Body
>>
for
Svc
{
impl
Service
<
Request
<
Body
>>
for
Svc
{
...
@@ -19,22 +40,18 @@ impl Service<Request<Body>> for Svc {
...
@@ -19,22 +40,18 @@ impl Service<Request<Body>> for Svc {
}
}
fn
call
(
&
mut
self
,
req
:
Request
<
Body
>
)
->
Self
::
Future
{
fn
call
(
&
mut
self
,
req
:
Request
<
Body
>
)
->
Self
::
Future
{
/* fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
/* fn mk_response(s: String) -> Result<Response<Body>, hyper::Error> {
Ok(Response::builder().body(Body::from(s)).unwrap())
Ok(Response::builder().body(Body::from(s)).unwrap())
} */
} */
fn
fetch_file
(
path
:
String
,
file_type
:
&
str
)
->
Result
<
Response
<
Body
>
,
hyper
::
Error
>
{
Ok
(
Response
::
new
(
Body
::
from
(
"awef"
)))
}
let
res
:
Result
<
Response
<
Body
>
,
hyper
::
Error
>
=
match
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
1
)
.take
(
1
)
.collect
::
<
String
>
()
.as_str
()
{
let
res
:
Result
<
Response
<
Body
>
,
hyper
::
Error
>
=
match
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
1
)
.take
(
1
)
.collect
::
<
String
>
()
.as_str
()
{
"media"
=>
{
"media"
=>
{
let
path
=
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
2
)
.take
(
1
)
.collect
::
<
String
>
();
let
path
=
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
2
)
.take
(
1
)
.collect
::
<
String
>
();
fetch_file
(
path
,
"media"
)
self
.
fetch_file
(
path
,
"media"
)
}
}
"thumbnail"
=>
{
"thumbnail"
=>
{
let
path
=
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
2
)
.take
(
1
)
.collect
::
<
String
>
();
let
path
=
req
.uri
()
.path
()
.split
(
"/"
)
.skip
(
2
)
.take
(
1
)
.collect
::
<
String
>
();
fetch_file
(
path
,
"media"
)
self
.
fetch_file
(
path
,
"media"
)
}
}
"refresh"
=>
{
"refresh"
=>
{
// refresh_cache
// refresh_cache
...
@@ -52,6 +69,8 @@ impl Service<Request<Body>> for Svc {
...
@@ -52,6 +69,8 @@ impl Service<Request<Body>> for Svc {
}
}
pub
struct
MakeSvc
{
pub
struct
MakeSvc
{
pub
base_dir
:
String
,
pub
outdated_after
:
Duration
,
}
}
impl
<
T
>
Service
<
T
>
for
MakeSvc
{
impl
<
T
>
Service
<
T
>
for
MakeSvc
{
...
@@ -64,7 +83,9 @@ impl<T> Service<T> for MakeSvc {
...
@@ -64,7 +83,9 @@ impl<T> Service<T> for MakeSvc {
}
}
fn
call
(
&
mut
self
,
_
:
T
)
->
Self
::
Future
{
fn
call
(
&
mut
self
,
_
:
T
)
->
Self
::
Future
{
let
fut
=
async
move
{
Ok
(
Svc
{
media_cache
:
MediaFileCache
::
new
()
})
};
let
outdated_after
=
self
.outdated_after
.to_owned
();
let
base_dir
=
self
.base_dir
.to_owned
();
let
fut
=
async
move
{
Ok
(
Svc
{
media_cache
:
MediaFileCache
::
new
(),
base_dir
,
outdated_after
})
};
Box
::
pin
(
fut
)
Box
::
pin
(
fut
)
}
}
}
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment