Rust-HayStack

   2023-02-09 学习力0
核心提示:安装rustcurl https://sh.rustup.rs -sSf | sh建立项目cargo new upfile编写图片服务器 src/main.rs extern crate multipart;extern crate iron;extern crate time;//image converterextern crate image;extern crate crypto;extern crate rustc_serialize

安装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

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • bloom-server 基于 rust 编写的 rest api cache 中间件
    bloom-server 基于 rust 编写的 rest api cache
    bloom-server 基于 rust 编写的 rest api cache 中间件,他位于lb 与api worker 之间,使用redis 作为缓存内容存储, 我们需要做的就是配置proxy,同时他使用基于share 的概念,进行cache 的分布存储,包含了请求端口(proxy,访问数据) 以及cache 控制端口(
    03-08
  • #新闻拍一拍# Oracle 调研如何避免让 Java 开发者投奔 Rust 和 Kotlin | Linux 中国
    #新闻拍一拍# Oracle 调研如何避免让 Java 开发
     导读:• 英特尔对迟迟不被 Linux 主线接受的 SGX Enclave 进行了第 38 次修订 • ARM 支持开源的 Panfrost Gallium3D 驱动本文字数:977,阅读时长大约:1分钟作者:硬核老王Oracle 调研如何避免让 Java 开发者投奔 Rust 和 KotlinOracle 委托分析公司 Omd
    03-08
  • Linux系统下Rust快速安装:国内镜像加速
    Linux系统下Rust快速安装:国内镜像加速
    官方网址和方法Install Rust - Rust Programming Language然而速度慢得让人难以置信。利用国内镜像进行windows的Linux子系统的Rust安装。rust 使用国内镜像,快速安装方法参考:RUST安装慢怎么办,使用镜像方式安装_网络_为中华之崛起而编程-CSDN博客我的操作
    03-08
  • Rust到底值不值得学--Rust对比、特色和理念
    前言其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了。当然这只是一个玩笑,可别把这个问题当真。然而对于IT从业者来说,这可不是一个玩笑。几乎每一次的技术发展,都让这个生态变得更为复杂。“英年早秃”已经成
    03-08
  • 超33000行新代码,为Linux内核添加Rust支持的补丁已准备就绪
    超33000行新代码,为Linux内核添加Rust支持的补
    https://mp.weixin.qq.com/s/oKw9aBJSdmRoO6-rbLAkNw7 月 4 日,一套修订后的补丁被提交至 Linux 内核的邮件列表中,该补丁为在 Linux 内核中以 Rust 作为辅助编程语言提供了支持,借助 Rust 可以提高 Linux 内核和内存的安全。整套补丁包含 17 个子项,不光
    03-08
  • 【译】Rust 的 Result 类型入门
    【译】Rust 的 Result 类型入门
    A Primer on Rust’s Result Type 译文原文链接:https://medium.com/@JoeKreydt/a-primer-on-rusts-result-type-66363cf18e6a原文作者:Joe Kreydt译文出处:https://github.com/suhanyujie/article-transfer-rs译者:suhanyujietips:水平有限,翻译不当之
    03-08
  • Rust实战系列-基本语法
    Rust实战系列-基本语法
    主要介绍 Rust 的语法、基本类型和数据结构,通过实现一个简单版 grep 命令行工具,来理解 Rust 独有的特性。本文是《Rust in action》学习总结系列的第二部分,更多内容请看已发布文章:一、Rust实战系列-Rust介绍“主要介绍 Rust 的语法、基本类型和数据结
    03-08
  • 全栈程序员的新玩具Rust(三)板条箱
    上次用到了stdout,这次我们来写一个更复杂一点的游戏rust的标准库叫做std,默认就会引入。这次我们要用到一个随机数函数,而随机数比较尴尬的一点是这玩意不在标准库中,我们要额外依赖一个库。很多编程方案都有自己的模块化库系统,rust也不例外,不过rust
    02-10
  • 全栈程序员的新玩具Rust(六)第一个WASM程序
    全栈程序员的新玩具Rust(六)第一个WASM程序
    先上代码https://gitee.com/lightsever/rust_study/tree/master/wasm_hello01webassembly就不用再赘述了,耳朵里面快磨出茧子来了。rustwasm是火狐自家的玩具,让我们来继续做实验,让rust飞起来吧。环境安装安装好rust环境之后仍然需要 一个 wasm 工具包carg
    02-10
  • 【Rust】标准库-Result rust数据库
    环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/result.html示例main.rsmod checked {#[derive(Debug)]pub enum MathError {DivisionByZero,NonPositiveLogarithm,NegativeSquareRoot,}pub type MathResult =
    02-09
点击排行