-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv2json.rs
62 lines (49 loc) · 1.95 KB
/
csv2json.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Csv file to Json
//!
//! CsvReader + JsonWriter
use std::collections::HashMap;
use std::io::{Cursor, Write};
use actix_multipart::Multipart;
use actix_web::{HttpResponse, Result};
use fabrix::{CsvReader, JsonWriter};
use futures::{StreamExt, TryStreamExt};
use serde_json::Value;
use crate::{AppError, FILE_TYPE_CSV, MULTIPART_KEY_FILE};
pub async fn csv_to_json(mut payload: Multipart) -> Result<HttpResponse> {
let mut result = Vec::<HashMap<String, Value>>::new();
while let Ok(Some(mut field)) = payload.try_next().await {
// skip non-csv files
if *field.content_type() != FILE_TYPE_CSV {
continue;
}
let cd = field.content_disposition();
if let Some(MULTIPART_KEY_FILE) = cd.get_name() {
let filename = cd
.get_filename()
.ok_or_else(|| AppError::Uncategorized("Filename not found".to_string()))?;
let name = sanitize_filename::sanitize(filename);
// turn buffer into fabrix struct
let mut buff = Cursor::new(Vec::new());
// write all bytes from multipart to buffer
while let Some(Ok(chunk)) = field.next().await {
buff.get_mut().write_all(&chunk)?;
}
// turn buffer into fabrix
let mut reader = CsvReader::new(buff);
let fx = reader.finish(None).map_err(AppError::Fabrix)?;
// turn fabrix into json
let mut json = Cursor::new(Vec::new());
let mut writer = JsonWriter::new(json.by_ref());
writer
.with_json_format(true)
.finish(fx)
.map_err(AppError::Fabrix)?;
let json_str: Value =
serde_json::from_slice(json.get_ref()).map_err(AppError::Serde)?;
let mut res = HashMap::new();
res.insert(name, json_str);
result.push(res);
}
}
Ok(HttpResponse::Ok().json(result))
}