1
1
//! Database based file handler
2
2
3
3
use crate :: storage:: { Blob , Storage } ;
4
- use crate :: { error:: Result , Config , Metrics } ;
5
- use iron:: { status, Handler , IronResult , Request , Response } ;
4
+ use crate :: { error:: Result , Config } ;
5
+ use iron:: { status, Response } ;
6
6
7
7
#[ derive( Debug ) ]
8
8
pub ( crate ) struct File ( pub ( crate ) Blob ) ;
9
9
10
10
impl File {
11
11
/// Gets file from database
12
- pub fn from_path ( storage : & Storage , path : & str , config : & Config ) -> Result < File > {
12
+ pub ( super ) fn from_path ( storage : & Storage , path : & str , config : & Config ) -> Result < File > {
13
13
let max_size = if path. ends_with ( ".html" ) {
14
14
config. max_file_size_html
15
15
} else {
@@ -20,7 +20,7 @@ impl File {
20
20
}
21
21
22
22
/// Consumes File and creates a iron response
23
- pub fn serve ( self ) -> Response {
23
+ pub ( super ) fn serve ( self ) -> Response {
24
24
use iron:: headers:: { CacheControl , CacheDirective , ContentType , HttpDate , LastModified } ;
25
25
26
26
let mut response = Response :: with ( ( status:: Ok , self . 0 . content ) ) ;
@@ -44,38 +44,11 @@ impl File {
44
44
}
45
45
46
46
/// Checks if mime type of file is "application/x-empty"
47
- pub fn is_empty ( & self ) -> bool {
47
+ pub ( super ) fn is_empty ( & self ) -> bool {
48
48
self . 0 . mime == "application/x-empty"
49
49
}
50
50
}
51
51
52
- /// Database based file handler for iron
53
- ///
54
- /// This is similar to staticfile crate, but its using getting files from database.
55
- pub struct DatabaseFileHandler ;
56
-
57
- impl Handler for DatabaseFileHandler {
58
- fn handle ( & self , req : & mut Request ) -> IronResult < Response > {
59
- let path = req. url . path ( ) . join ( "/" ) ;
60
- let storage = extension ! ( req, Storage ) ;
61
- let config = extension ! ( req, Config ) ;
62
- if let Ok ( file) = File :: from_path ( & storage, & path, & config) {
63
- let metrics = extension ! ( req, Metrics ) ;
64
-
65
- // Because all requests that don't hit another handler go through here, we will get all
66
- // requests successful or not recorded by the RequestRecorder, so we inject an extra
67
- // "database success" route to keep track of how often we succeed vs 404
68
- metrics
69
- . routes_visited
70
- . with_label_values ( & [ "database success" ] )
71
- . inc ( ) ;
72
- Ok ( file. serve ( ) )
73
- } else {
74
- Err ( super :: error:: Nope :: CrateNotFound . into ( ) )
75
- }
76
- }
77
- }
78
-
79
52
#[ cfg( test) ]
80
53
mod tests {
81
54
use super :: * ;
0 commit comments