@@ -5,59 +5,35 @@ use time;
55use postgres:: Connection ;
66use iron:: { Handler , Request , IronResult , Response , IronError } ;
77use iron:: status;
8+ use crate :: db;
89
910
10- pub struct File {
11- pub path : String ,
12- pub mime : String ,
13- pub date_added : time:: Timespec ,
14- pub date_updated : time:: Timespec ,
15- pub content : Vec < u8 > ,
16- }
17-
11+ pub struct File ( pub db:: file:: Blob ) ;
1812
1913impl File {
2014 /// Gets file from database
2115 pub fn from_path ( conn : & Connection , path : & str ) -> Option < File > {
22-
23- let rows = conn. query ( "SELECT path, mime, date_added, date_updated, content
24- FROM files
25- WHERE path = $1" ,
26- & [ & path] )
27- . unwrap ( ) ;
28-
29- if rows. len ( ) == 0 {
30- None
31- } else {
32- let row = rows. get ( 0 ) ;
33- Some ( File {
34- path : row. get ( 0 ) ,
35- mime : row. get ( 1 ) ,
36- date_added : row. get ( 2 ) ,
37- date_updated : row. get ( 3 ) ,
38- content : row. get ( 4 ) ,
39- } )
40- }
16+ Some ( File ( db:: file:: get_path ( conn, path) ?) )
4117 }
4218
4319
4420 /// Consumes File and creates a iron response
4521 pub fn serve ( self ) -> Response {
4622 use iron:: headers:: { CacheControl , LastModified , CacheDirective , HttpDate , ContentType } ;
4723
48- let mut response = Response :: with ( ( status:: Ok , self . content ) ) ;
24+ let mut response = Response :: with ( ( status:: Ok , self . 0 . content ) ) ;
4925 let cache = vec ! [ CacheDirective :: Public ,
5026 CacheDirective :: MaxAge ( super :: STATIC_FILE_CACHE_DURATION as u32 ) ] ;
51- response. headers . set ( ContentType ( self . mime . parse ( ) . unwrap ( ) ) ) ;
27+ response. headers . set ( ContentType ( self . 0 . mime . parse ( ) . unwrap ( ) ) ) ;
5228 response. headers . set ( CacheControl ( cache) ) ;
53- response. headers . set ( LastModified ( HttpDate ( time:: at ( self . date_updated ) ) ) ) ;
29+ response. headers . set ( LastModified ( HttpDate ( time:: at ( self . 0 . date_updated ) ) ) ) ;
5430 response
5531 }
5632
5733
5834 /// Checks if mime type of file is "application/x-empty"
5935 pub fn is_empty ( & self ) -> bool {
60- self . mime == "application/x-empty"
36+ self . 0 . mime == "application/x-empty"
6137 }
6238}
6339
0 commit comments