1
0
mirror of https://github.com/actix/examples synced 2025-06-29 10:14:58 +02:00
This commit is contained in:
cheolgyu
2020-02-05 12:53:33 +09:00
parent d59129c212
commit 5fb3d3270e
8 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,2 @@
pub mod s3;
pub mod upload;

View File

@ -0,0 +1,66 @@
use crate::rusoto_s3::S3;
use rusoto_core::{ProvideAwsCredentials, Region, RusotoError};
use rusoto_s3::{DeleteObjectRequest, PutObjectRequest, S3Client};
use std::io::Read;
use std::io::Write;
pub struct Client {
region: Region,
s3: S3Client,
bucket_name: String,
}
impl Client {
// construct S3 testing client
pub fn new() -> Client {
let region = Region::ApNortheast2;
Client {
region: region.to_owned(),
s3: S3Client::new(region),
bucket_name: std::env::var("AWS_S3_BUCKET_NAME").unwrap(),
}
}
pub fn url(&self, key: &str) -> String {
format!(
"https://{}.s3.{}.amazonaws.com/{}",
std::env::var("AWS_S3_BUCKET_NAME").unwrap(),
"ap-northeast-2",
key
)
}
pub fn put_object(&self, localfilepath: &str, key: &str) -> String {
let mut file = std::fs::File::open(localfilepath).unwrap();
let mut contents: Vec<u8> = Vec::new();
file.read_to_end(&mut contents);
let put_request = PutObjectRequest {
bucket: self.bucket_name.to_owned(),
key: key.to_owned(),
body: Some(contents.into()),
..Default::default()
};
let res = self
.s3
.put_object(put_request)
.sync()
.expect("Failed to put test object");
self.url(key)
}
pub fn delete_object(&self, key: String) {
let delete_object_req = DeleteObjectRequest {
bucket: self.bucket_name.to_owned(),
key: key.to_owned(),
..Default::default()
};
let res = self
.s3
.delete_object(delete_object_req)
.sync()
.expect("Couldn't delete object");
}
}

View File

@ -0,0 +1,120 @@
use crate::utils::s3::Client;
use actix_multipart::{Field, Multipart};
use actix_web::{web, Error};
use bytes::Bytes;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json::{Map as serdeMap, Value};
use std::convert::From;
use std::io::Write;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct UplodFile {
pub filename: String,
pub key: String,
pub url: String,
}
impl From<Tmpfile> for UplodFile {
fn from(tmp_file: Tmpfile) -> Self {
UplodFile {
filename: tmp_file.name,
key: tmp_file.s3_key,
url: tmp_file.s3_url,
}
}
}
/*
1. savefile
2. s3 upload -> upload_data
3. deletefile
*/
#[derive(Debug, Clone)]
pub struct Tmpfile {
pub name: String,
pub tmp_path: String,
pub s3_key: String,
pub s3_url: String,
}
impl Tmpfile {
fn new(filename: &str) -> Tmpfile {
Tmpfile {
name: filename.to_string(),
tmp_path: format!("./tmp/{}", filename),
s3_key: "".to_string(),
s3_url: "".to_string(),
}
}
fn s3_upload_and_tmp_remove(&mut self, s3_upload_key: String) {
self.s3_upload(s3_upload_key);
self.tmp_remove();
}
fn s3_upload(&mut self, s3_upload_key: String) {
let key = format!("{}{}", &s3_upload_key, &self.name);
self.s3_key = key.clone();
let url: String = Client::new().put_object(&self.tmp_path, &key.clone());
self.s3_url = url;
}
fn tmp_remove(&self) {
std::fs::remove_file(&self.tmp_path).unwrap();
}
}
pub async fn split_payload(payload: &mut Multipart) -> (bytes::Bytes, Vec<Tmpfile>) {
let mut tmp_files: Vec<Tmpfile> = Vec::new();
let mut data = Bytes::new();
while let Some(item) = payload.next().await {
let mut field: Field = item.expect(" split_payload err");
let content_type = field.content_disposition().unwrap();
let name = content_type.get_name().unwrap();
if name == "data" {
while let Some(chunk) = field.next().await {
data = chunk.expect(" split_payload err chunk");
}
} else {
if content_type.get_filename() != None {
let filename = content_type.get_filename().unwrap();
let tmp_file = Tmpfile::new(filename);
let tmp_path = tmp_file.tmp_path.clone();
let mut f = web::block(move || std::fs::File::create(&tmp_path))
.await
.unwrap();
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
f = web::block(move || f.write_all(&data).map(|_| f))
.await
.unwrap();
}
tmp_files.push(tmp_file.clone());
}
}
}
(data, tmp_files)
}
pub async fn save_file(
tmp_files: Vec<Tmpfile>,
s3_upload_key: String,
) -> Result<Vec<UplodFile>, Error> {
let mut arr: Vec<UplodFile> = Vec::new();
let mut iter = tmp_files.iter();
let mut index = 0;
// iterate over multipart stream
while let Some(item) = iter.next() {
let mut tmp_file: Tmpfile = item.clone();
tmp_file.s3_upload_and_tmp_remove(s3_upload_key.clone());
arr.push(UplodFile::from(tmp_file));
}
Ok(arr)
}
pub async fn delete_object(mut list: Vec<String>) {
for key in list {
Client::new().delete_object(key);
}
}