11//! Database based file handler
22
33use 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 } ;
66
77#[ derive( Debug ) ]
88pub ( crate ) struct File ( pub ( crate ) Blob ) ;
99
1010impl File {
1111 /// 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 > {
1313 let max_size = if path. ends_with ( ".html" ) {
1414 config. max_file_size_html
1515 } else {
@@ -20,7 +20,7 @@ impl File {
2020 }
2121
2222 /// Consumes File and creates a iron response
23- pub fn serve ( self ) -> Response {
23+ pub ( super ) fn serve ( self ) -> Response {
2424 use iron:: headers:: { CacheControl , CacheDirective , ContentType , HttpDate , LastModified } ;
2525
2626 let mut response = Response :: with ( ( status:: Ok , self . 0 . content ) ) ;
@@ -44,38 +44,11 @@ impl File {
4444 }
4545
4646 /// 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 {
4848 self . 0 . mime == "application/x-empty"
4949 }
5050}
5151
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-
7952#[ cfg( test) ]
8053mod tests {
8154 use super :: * ;
0 commit comments