Rust-HayStack
安装rust
curl https://sh.rustup.rs -sSf | sh
建立项目
cargo new upfile
编写图片服务器
src/main.rs
extern crate multipart;
extern crate iron;
extern crate time;
//image converter
extern crate image;
extern crate crypto;
extern crate rustc_serialize;
use rustc_serialize::json; use crypto::md5::Md5;
use crypto::digest::Digest; use std::fs::File;
use std::io::Read;
use std::path::Path;
use multipart::server::{Multipart, Entries, SaveResult}; use iron::prelude::*;
use iron::status;
use iron::mime::Mime; extern crate router;
use router::Router; const INDEX_HTML: &'static [u8] = include_bytes!("../index.html"); #[derive(Debug, RustcDecodable, RustcEncodable)]
struct JsonResult {
ret: bool,
data: String,
} fn main() {
let mut router = Router::new(); router.get("/", | _: &mut Request| {
let content_type = "text/html".parse::<Mime>().unwrap();
Ok(Response::with((content_type, status::Ok, INDEX_HTML)))
});
router.post("upload", process_upload);
//04fd905e6e449e50f9bf5095960bdb54
router.get("/:md5", process_query); router.get("error", |_: &mut Request| {
Ok(Response::with(status::BadRequest))
}); Iron::new(router).http("0.0.0.0:8080").unwrap(); // Iron::new(process_upload).http("localhost:8080").expect("Could not bind localhost:8080");
}
///process query
fn process_query(request: &mut Request) -> IronResult<Response> {
let ref md5 = request.extensions.get::<Router>().unwrap().find("md5").unwrap_or("/");
let content_type = "image/jpeg".parse::<Mime>().unwrap();
let img = match image::open(format!("{}.jpg",md5)) {
Ok(img) => img,
Err(e) => return Err(IronError::new(e, status::InternalServerError))
}; // let thumb = img.resize(128, 128, image::FilterType::Triangle);
let mut buffer = vec![]; match img.save(&mut buffer, image::JPEG) {
Ok(_) => Ok(Response::with((content_type,iron::status::Ok, buffer))),
Err(e) => Err(IronError::new(e, status::InternalServerError))
}
} /// Processes a request and returns response or an occured error.
fn process_upload(request: &mut Request) -> IronResult<Response> {
// Getting a multipart reader wrapper
match Multipart::from_request(request) {
Ok(mut multipart) => {
// Fetching all data and processing it.
// save_all() reads the request fully, parsing all fields and saving all files
// in a new temporary directory under the OS temporary directory.
match multipart.save_all() {
SaveResult::Full(entries) => process_entries(entries),
SaveResult::Partial(entries, error) => {
try!(process_entries(entries));
Err(IronError::new(error, status::InternalServerError))
}
SaveResult::Error(error) => Err(IronError::new(error, status::InternalServerError)),
}
}
Err(_) => {
Ok(Response::with((status::BadRequest, "The request is not multipart")))
}
}
} /// Processes saved entries from multipart request.
/// Returns an OK response or an error.
fn process_entries(entries: Entries) -> IronResult<Response> {
let mut md5s = String::new();
for (name, field) in entries.fields {
println!(r#"Field "{}": "{}""#, name, field);
} for (name, savedfile) in entries.files {
let filename = match savedfile.filename {
Some(s) => s,
None => "None".into(),
};
let file_start = time::now();
let mut file = match File::open(savedfile.path) {
Ok(file) => file,
Err(error) => {
return Err(IronError::new(error,
(status::InternalServerError,
"Server couldn't save file")))
}
};
let file_end = time::now();//
println!("file load!start : {},end :{},duration:{}",file_start.rfc3339(),file_end.rfc3339(),file_end-file_start);
//caculate md5
let mut buffer = Vec::new();
// read the whole file
file.read_to_end(&mut buffer).unwrap();
let mut hasher = Md5::new();
hasher.input(&buffer);
let md5 = hasher.result_str();
// println!("{}", md5);
md5s = md5s + &md5 + ",";
let md5_end = time::now();//
println!("md5 load!start : {},end :{},duration:{}",file_end.rfc3339(),md5_end.rfc3339(),md5_end-file_end);
//image file
let img = match image::load_from_memory(&buffer){
Ok(file) => file,
Err(error) => {
return Err(IronError::new(error,
(status::InternalServerError,
"Unsupported image format")))
}
};
let img_end = time::now();//
println!("img load!start : {},end :{},duration:{}",md5_end.rfc3339(),img_end.rfc3339(),img_end-md5_end); let ref mut fout = File::create(&Path::new(&*(md5+".jpg"))).unwrap();
// The dimensions method returns the images width and height
// println!("dimensions {:?}", img.dimensions());
// The color method returns the image's ColorType
// println!("{:?}", img.color()); // Write the contents of this image to the Writer in PNG format.
let _ = img.save(fout, image::JPEG).unwrap(); let save_end = time::now();//
println!("save file!start : {},end :{},duration:{}",img_end.rfc3339(),save_end.rfc3339(),save_end-img_end); println!(r#"Field "{}" is file "{}":"#, name, filename);
}
let content_type = "application/json".parse::<Mime>().unwrap();
let object = JsonResult{
ret:true,
data:md5s,
};
Ok(Response::with((content_type, status::Ok, json::encode(&object).unwrap())))
// Ok(Response::with((status::Ok, md5s)))
}
index.html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<head>
<script>
var totals = 1;
function addInput()
{
var newItemText=document.createTextNode("Choose file:");
var newItemInput=document.createElement("input");
newItemInput.name="userfile"+(totals++);
newItemInput.type="file";
var addfile=document.getElementById("bt_addfile");
var submit=document.getElementById("bt_submit");
var newItemBr=document.createElement("br"); var myform=document.getElementById("upform");
myform.appendChild(newItemText);
myform.appendChild(newItemInput);
myform.appendChild(addfile);
myform.appendChild(newItemBr);
myform.appendChild(submit);
}
</script>
</head>
<h1>Welcome to images' World!</h1>
<p>Upload image(s) to server:</p>
<form enctype="multipart/form-data" action="/upload" method=post target=_blank id="upform">
Choose file:<input name="userfile" type="file">
<input type="button" value="+" onclick="addInput()" id="bt_addfile">
</br>
<input type="submit" value="upload" id="bt_submit">
</form>
</html>
Cargo.toml
[package]
name = "upfile"
version = "0.1.0"
authors = ["mignet <mignetwee@gmail.com>"]
[dependencies]
iron = "0.2"
image = "*"
time = "*"
router = "*"
rustc-serialize = "*"
rust-crypto = "0.2.35"
[dependencies.multipart]
version = "0.5.1"
default-features = false
features = ["server", "iron"]
性能测试:
mignet@Master:~/rust-projects/upfile$ cargo run
Compiling upfile v0.1.0 (file:///home/mignet/rust-projects/upfile)
Running `target/debug/upfile`
file load!start : 2016-04-17T16:00:49+08:00,end :2016-04-17T16:00:49+08:00,duration:PT0.000094890S
md5 load!start : 2016-04-17T16:00:49+08:00,end :2016-04-17T16:00:49+08:00,duration:PT0.023411631S
img load!start : 2016-04-17T16:00:49+08:00,end :2016-04-17T16:00:52+08:00,duration:PT2.621793752S
save file!start : 2016-04-17T16:00:52+08:00,end :2016-04-17T16:00:55+08:00,duration:PT3.651583434S
Field "userfile" is file "StarUml.png":
---------------------------------------------------------------
mignet@Master:~/rust-projects/upfile$ cargo run --release
Compiling upfile v0.1.0 (file:///home/mignet/rust-projects/upfile)
Running `target/release/upfile`
file load!start : 2016-04-17T16:02:28+08:00,end :2016-04-17T16:02:28+08:00,duration:PT0.000099016S
md5 load!start : 2016-04-17T16:02:28+08:00,end :2016-04-17T16:02:28+08:00,duration:PT0.001155275S
img load!start : 2016-04-17T16:02:28+08:00,end :2016-04-17T16:02:28+08:00,duration:PT0.055703035S
save file!start : 2016-04-17T16:02:28+08:00,end :2016-04-17T16:02:28+08:00,duration:PT0.375560153S
Field "userfile" is file "StarUml.png":
部署访问地址:http://images.v5ent.com
Rust-HayStack的更多相关文章
- rust 学习之旅二,关键字和保留字
当前,以下关键字具有所描述的功能. as-执行原始类型转换,消除包含项目的特定特征的歧义,或在useand extern crate语句中重命名项目async-返回a Future而不是阻塞当前线程a ...
- Rust实战系列-基本语法
本文是<Rust in action>学习总结系列的第二部分,更多内容请看已发布文章: 一.Rust实战系列-Rust介绍 " 主要介绍 Rust 的语法.基本类型和数据结构,通 ...
- Rust语言的多线程编程
我写这篇短文的时候,正值Rust1.0发布不久,严格来说这是一门兼具C语言的执行效率和Java的开发效率的强大语言,它的所有权机制竟然让你无法写出线程不安全的代码,它是一门可以用来写操作系统的系统级语 ...
- Rust初步(七):格式化
在Rust中,如果要进行屏幕输出,或者写入到文件中,需要对数据进行格式化.这一篇总结一下它所支持的几种格式化方式. 这篇文章参考了以下官方文档,不过,按照我的风格,我还是会突出于C#语言的比较,这样可 ...
- Rust初步(六):在C#中使用Rust组件
上一篇文章,我们通过实例比较了一下C#和Rust的性能表现,应该说在Release模式下面,Rust进行计算密集型的运算还是有些比较明显的优势的.那么,我们有没有可能,在C#中做一些快速应用开发,而一 ...
- Rust初步(五):Rust与C#性能比较
我学习Rust的目的并不是说期望用它来取代掉现有的开发平台或语言.相反,我认为当前绝大部分研发团队,都不可能只用一个平台或者一个语言. 当组织增长,他们越来越依赖大量的编程语言.不同的编程语言有不同的 ...
- Rust初步(四):在rust中处理时间
这个看起来是一个很小的问题,我们如果是在.NET里面的话,很简单地可以直接使用System.DateTime.Now获取到当前时间,还可以进行各种不同的计算或者输出.但是这样一个问题,在rust里面, ...
- Rust初步(三):使用atom搭配racer进行rust编程
在rust.cc社区中有一个关于rust编辑器的讨论(话说很多人要学一个新语言,都会立即考虑编辑器的问题,包括我在内),主要关注的是,智能提示(这个真的太重要了).大家讨论下来有几个选择 1. ecl ...
- Rust初步(二):使用Visual Studio Code编写Rust程序(猜猜看游戏)
我是照着下面这篇帮助文档,完成了第一个完整的Rust程序: 猜猜看 游戏 http://kaisery.gitbooks.io/rust-book-chinese/content/content/3. ...
- Rust初步(一):介绍
最近在研究Rust这个新的语言.那么Rust是什么呢? Rust是一个注重安全与速度的现代系统编程语言,通过在没有垃圾回收的情况下保证内存安全来实现它的目标,这使它成为一个在很多其它语言不适合的用例中 ...
随机推荐
- [GDOI2016][树链剖分+主席树]疯狂动物城
题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...
- Android 自定义debug.keystore
场景分析: 有时候,我们要使用第三方的服务,需要提供自己的包名以及keystore的sha1值,比如微信支付,百度地图,都需要包名和keystore的sha1值作为唯一标识.这时候我们测试的时候,如果 ...
- laravel5.2总结--响应
1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...
- sed处理大txt文件(1G) 比如替换某一串字符串,或者删除一行
1.将11.sql文件中"prompt"替换为"--prompt",然后保存为111.sql文件 sed -e "s,prompt,--prompt, ...
- 【转】手动写一个Behavior Designer任务节点
http://blog.csdn.net/qq_33747722/article/details/53539532 自己手写一个类似于CanSeeObject.Seek等任务节点并不是一件难事 下面我 ...
- ansible中playbook使用
palybook使用 ####yaml语法ansible中使用的yaml基础元素:变量Inventory条件测试迭代 playbook的组成结构InventoryModulesAd Hoc Comma ...
- Codeforces 1158C Permutation recovery
https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...
- 论文笔记(一)Re-ranking by Multi-feature Fusion with Diffusion for Image Retrieval
0x00 预备知识 $\DeclareMathOperator{\vol}{vol}$ 无向图上的随机游走 无向图 $G=(V,E)$,边权函数 $w\colon V\times V \to R_+$ ...
- BZOJ2657 [Zjoi2012]旅游(journey) 【树的直径】
题目 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示,N个顶点表示N个入境/出境口 ...
- 3984: 玩具(toy)
3984: 玩具(toy) 题目描述 这个故事发生在很久以前,在 IcePrincess_1968 和 IcePrince_1968 都还在上幼儿园的时候. IcePrince_1968 最近迷上了一 ...