Lines
21.43 %
Functions
8.33 %
Branches
100 %
use std::sync::Arc;
use crate::AppState;
use axum::{
extract::{Path, State},
http::{HeaderMap, StatusCode, header},
response::IntoResponse,
};
#[derive(Debug)]
pub struct S3File {
pub name: String,
pub link: String,
}
pub async fn download_file(
State(_data): State<Arc<AppState>>,
Path(_file_name): Path<String>,
) -> Result<impl IntoResponse, StatusCode> {
/* let client = &data.s3.clone();
let get_req = GetObjectRequest {
bucket: data.conf.s3_bucket.clone(),
key: format!("files/{}", file_name),
..Default::default()
let result = client
.get_object(get_req)
.await
.map_err(|_| StatusCode::NOT_FOUND)?;
let content_type = result
.content_type
.unwrap_or("application/octet-stream".to_string());
let body = result.body.ok_or(StatusCode::INTERNAL_SERVER_ERROR);
let stream = FramedRead::new(
body.expect("No result").into_async_read(),
BytesCodec::new(),
);
let bytes = Body::from_stream(stream);
*/
let content_type = "application/octet-stream";
let bytes = vec![1, 2, 3];
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, content_type.parse().unwrap());
headers.insert(header::CONTENT_DISPOSITION, "attachment".parse().unwrap());
Ok((headers, bytes).into_response())
pub async fn list_s3(State(_data): State<Arc<AppState>>) -> Result<Vec<S3File>, StatusCode> {
/*
let client = &data.s3.clone();
let list_req = ListObjectsRequest {
prefix: Some("files/".to_string()),
client
.list_objects(list_req)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.contents
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
.iter()
.map(|obj| -> Result<S3File, StatusCode> {
let name = obj
.key
.as_ref()
.strip_prefix("files/")
.to_owned();
let link = format!("/api/files/download/{}", name);
Ok(S3File { name, link })
})
.collect()
Ok(vec![])