-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathfile.rs
54 lines (44 loc) · 1.7 KB
/
file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Simple module to store files in database.
//!
//! cratesfyi is generating more than 5 million files, they are small and mostly html files.
//! They are using so many inodes and it is better to store them in database instead of
//! filesystem. This module is adding files into database and retrieving them.
use crate::{docbuilder::Limits, error::Result, storage::Storage};
use postgres::Connection;
use serde_json::Value;
use std::path::{Path, PathBuf};
pub(crate) use crate::storage::Blob;
pub(crate) fn get_path(conn: &Connection, path: &str) -> Option<Blob> {
Storage::new(conn).get(path).ok()
}
/// Store all files in a directory and return [[mimetype, filename]] as Json
///
/// If there is an S3 Client configured, store files into an S3 bucket;
/// otherwise, stores files into the 'files' table of the local database.
///
/// The mimetype is detected using `magic`.
///
/// Note that this function is used for uploading both sources
/// and files generated by rustdoc.
pub fn add_path_into_database<P: AsRef<Path>>(
conn: &Connection,
prefix: &str,
path: P,
limits: &Limits,
) -> Result<Value> {
let mut backend = Storage::new(conn);
let file_list = backend.store_all(conn, prefix, path.as_ref(), limits)?;
file_list_to_json(file_list.into_iter().collect())
}
fn file_list_to_json(file_list: Vec<(PathBuf, String)>) -> Result<Value> {
let mut file_list_json: Vec<Value> = Vec::new();
for file in file_list {
let mut v = Vec::with_capacity(2);
v.push(Value::String(file.1));
v.push(Value::String(
file.0.into_os_string().into_string().unwrap(),
));
file_list_json.push(Value::Array(v));
}
Ok(Value::Array(file_list_json))
}