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是一个注重安全与速度的现代系统编程语言,通过在没有垃圾回收的情况下保证内存安全来实现它的目标,这使它成为一个在很多其它语言不适合的用例中 ...
随机推荐
- DAG上的动态规划——嵌套矩阵问题
问题描述:有n个矩形,每个矩形可以用两个整数a,b描述,表示它的长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d,或者b<c,a<d(相当于把矩形X旋 ...
- Leetcode 96. 不同的二叉搜索树
题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n ...
- char* 与char[]区别
[转] 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误.一时也不能说得很明白,网上也 ...
- datetime模块详解
时间运算 >>> datetime.datetime.now() #当前年月 时间datetime.datetime(2018, 2, 13, 16, 12, 6, 850532)& ...
- TCP/IP网络编程之套接字与标准I/O
标准I/O函数 标准标准I/O函数有两个优点: 标准I/O函数具有良好的移植性 标准I/O函数可以利用缓冲提高性能 关于移植性无需过多解释,不仅是I/O函数,所有标准函数都具有良好的移植性.因为,为了 ...
- 决策树python实现小样例
我们经常使用决策树处理分类问题,近年来的调查表明决策树也是经常使用的数据挖掘算法K-NN可以完成多分类任务,但是它最大的缺点是无法给出数据的内在含义,决策树的主要优势在于数据形式非常容易理解决策树的优 ...
- Java开发微信公众号(一)---初识微信公众号以及环境搭建
ps:1.开发语言使用Java springMvc+Mybaits+spring maven实现 2.使用微信接口测试账号进行本地测试 https://mp.weixin.qq.com/debug/c ...
- winform DataGridView添加合计行
使用方法 /* DataTable dt= DBUtility.DB.FromSql(sql).ToDataTable(); DataGridViewAddSumRow sumRow = new Da ...
- Java 第七次
- OgnlValueStack 源码
/* * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License, ...