Skip to content

Commit b6f1586

Browse files
Move all code that deals with files table to db::file
1 parent b5f8b1d commit b6f1586

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

src/db/file.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::path::{PathBuf, Path};
99
use postgres::Connection;
1010
use rustc_serialize::json::{Json, ToJson};
11-
use std::fs::File;
11+
use std::fs;
1212
use std::io::Read;
1313
use error::Result;
1414
use failure::err_msg;
@@ -52,6 +52,32 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>> {
5252
Ok(files)
5353
}
5454

55+
pub struct Blob {
56+
pub path: String,
57+
pub mime: String,
58+
pub date_added: time::Timespec,
59+
pub date_updated: time::Timespec,
60+
pub content: Vec<u8>,
61+
}
62+
63+
pub fn get_path(conn: &Connection, path: &str) -> Option<Blob> {
64+
let rows = conn.query("SELECT path, mime, date_added, date_updated, content
65+
FROM files
66+
WHERE path = $1", &[&path]).unwrap();
67+
68+
if rows.len() == 0 {
69+
None
70+
} else {
71+
let row = rows.get(0);
72+
Some(Blob {
73+
path: row.get(0),
74+
mime: row.get(1),
75+
date_added: row.get(2),
76+
date_updated: row.get(3),
77+
content: row.get(4),
78+
})
79+
}
80+
}
5581

5682
/// Adds files into database and returns list of files with their mime type in Json
5783
pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
@@ -71,7 +97,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
7197
let path = Path::new(path.as_ref()).join(&file_path);
7298
// Some files have insufficient permissions (like .lock file created by cargo in
7399
// documentation directory). We are skipping this files.
74-
let mut file = match File::open(path) {
100+
let mut file = match fs::File::open(path) {
75101
Ok(f) => f,
76102
Err(_) => continue,
77103
};

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use r2d2;
1212
use r2d2_postgres;
1313

1414
mod add_package;
15-
mod file;
15+
pub mod file;
1616
mod migrate;
1717

1818

src/web/file.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,35 @@ use time;
55
use postgres::Connection;
66
use iron::{Handler, Request, IronResult, Response, IronError};
77
use 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

1913
impl 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

src/web/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
222222

223223
let mut content = RustdocPage::default();
224224

225-
let file_content = ctry!(String::from_utf8(file.content));
225+
let file_content = ctry!(String::from_utf8(file.0.content));
226226

227227
let (head, body, mut body_class) = ctry!(utils::extract_head_and_body(&file_content));
228228
content.head = head;

src/web/source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
222222

223223
let (content, is_rust_source) = if let Some(file) = file {
224224
// serve the file with DatabaseFileHandler if file isn't text and not empty
225-
if !file.mime.starts_with("text") && !file.is_empty() {
225+
if !file.0.mime.starts_with("text") && !file.is_empty() {
226226
return Ok(file.serve());
227-
} else if file.mime.starts_with("text") && !file.is_empty() {
228-
(String::from_utf8(file.content).ok(), file.path.ends_with(".rs"))
227+
} else if file.0.mime.starts_with("text") && !file.is_empty() {
228+
(String::from_utf8(file.0.content).ok(), file.0.path.ends_with(".rs"))
229229
} else {
230230
(None, false)
231231
}

0 commit comments

Comments
 (0)