Skip to content

Build on windows support [WIP, No Merge] #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 15 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ reqwest = "0.9"
semver = "0.9"
slug = "=0.1.1"
env_logger = "0.6"
magic = "0.12"
r2d2 = "0.8"
r2d2_postgres = "0.14"
url = "1.4"
Expand All @@ -40,6 +39,7 @@ prometheus = { version = "0.7.0", default-features = false }
lazy_static = "1.0.0"
rustwide = "0.5.0"
tempdir = "0.3"
mime_guess = "2.0.1"

# iron dependencies
iron = "0.5"
Expand Down
52 changes: 25 additions & 27 deletions src/db/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,6 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
prefix: &str,
path: P)
-> Result<Json> {
use magic::{Cookie, flags};
let cookie = Cookie::open(flags::MIME_TYPE)?;
cookie.load::<&str>(&[])?;

let trans = conn.transaction()?;

use std::collections::HashMap;
Expand Down Expand Up @@ -191,32 +187,14 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
let bucket_path = Path::new(prefix).join(&file_path)
.into_os_string().into_string().unwrap();

let mime = {
let mime = cookie.buffer(&content)?;
// css's are causing some problem in browsers
// magic will return text/plain for css file types
// convert them to text/css
// do the same for javascript files
if mime == "text/plain" {
let e = file_path.extension().unwrap_or_default();
if e == "css" {
"text/css".to_owned()
} else if e == "js" {
"application/javascript".to_owned()
} else {
mime.to_owned()
}
} else {
mime.to_owned()
}
};
let mime = detect_mime(file_path);

if let Some(client) = &client {
futures.push(client.put_object(PutObjectRequest {
bucket: S3_BUCKET_NAME.into(),
key: bucket_path.clone(),
body: Some(content.clone().into()),
content_type: Some(mime.clone()),
content_type: mime.clone(),
..Default::default()
}).inspect(|_| {
crate::web::metrics::UPLOADED_FILES_TOTAL.inc_by(1);
Expand All @@ -236,7 +214,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
}
}

file_paths_and_mimes.insert(file_path.clone(), mime.clone());
file_paths_and_mimes.insert(file_path.clone(), mime.unwrap_or_default());
}

if !futures.is_empty() {
Expand Down Expand Up @@ -272,7 +250,14 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
file_list_to_json(file_list_with_mimes)
}


fn detect_mime(path: &Path) -> Option<String> {
let file_ext = path.extension().map(|ext| ext.to_str()).unwrap_or_default();
match file_ext {
Some("md") => Some("text/markdown".to_owned()),
None => None,
_ => mime_guess::from_path(path).first_raw().map(|m| m.to_owned())
}
}

fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {

Expand Down Expand Up @@ -339,7 +324,7 @@ pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
mod test {
extern crate env_logger;
use std::env;
use super::get_file_list;
use super::*;

#[test]
fn test_get_file_list() {
Expand All @@ -352,4 +337,17 @@ mod test {
let files = get_file_list(env::current_dir().unwrap().join("Cargo.toml")).unwrap();
assert_eq!(files[0], std::path::Path::new("Cargo.toml"));
}

#[test]
fn test_mime_types() {
assert_eq!(detect_mime(Path::new("hello.toml")).unwrap(), "text/x-toml");
assert_eq!(detect_mime(Path::new("hello.css")).unwrap(), "text/css");
assert_eq!(detect_mime(Path::new("hello.js")).unwrap(), "application/javascript");
assert_eq!(detect_mime(Path::new("hello.html")).unwrap(), "text/html");
assert_eq!(detect_mime(Path::new("hello.hello.md")).unwrap(), "text/markdown");
assert_eq!(detect_mime(Path::new("hello.markdown")).unwrap(), "text/markdown");
assert_eq!(detect_mime(Path::new("hello.json")).unwrap(), "application/json");
assert_eq!(detect_mime(Path::new("hello.txt")).unwrap(), "text/plain");
assert!(detect_mime(Path::new("blah")).is_none());
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern crate reqwest;
extern crate time;
extern crate semver;
extern crate slug;
extern crate magic;
extern crate mime_guess;
extern crate iron;
extern crate router;
extern crate staticfile;
Expand Down
37 changes: 23 additions & 14 deletions src/utils/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

use std::{env, thread};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::process::exit;
use std::fs::File;
use std::io::Write;
use std::time::Duration;
use std::path::PathBuf;
use libc::fork;
use time;
use docbuilder::RustwideBuilder;
use DocBuilderOptions;
use DocBuilder;
use utils::{update_release_activity, github_updater, pubsubhubbub};
use db::{connect_db, update_search_index};


#[cfg(not(target_os = "windows"))]
use ::{
libc::fork,
std::process::exit,
std::fs::File,
std::io::Write
};

pub fn start_daemon(background: bool) {
// first check required environment variables
Expand All @@ -36,15 +38,22 @@ pub fn start_daemon(background: bool) {
dbopts.check_paths().unwrap();

if background {
// fork the process
let pid = unsafe { fork() };
if pid > 0 {
let mut file = File::create(dbopts.prefix.join("cratesfyi.pid"))
.expect("Failed to create pid file");
writeln!(&mut file, "{}", pid).expect("Failed to write pid");

info!("cratesfyi {} daemon started on: {}", ::BUILD_VERSION, pid);
exit(0);
#[cfg(target_os = "windows")]
{
panic!("running in background not supported on windows");
}
#[cfg(not(target_os = "windows"))]
{
// fork the process
let pid = unsafe { fork() };
if pid > 0 {
let mut file = File::create(dbopts.prefix.join("cratesfyi.pid"))
.expect("Failed to create pid file");
writeln!(&mut file, "{}", pid).expect("Failed to write pid");

info!("cratesfyi {} daemon started on: {}", ::BUILD_VERSION, pid);
exit(0);
}
}
}

Expand Down