Skip to main content

web/
files.rs

1use std::sync::Arc;
2
3use crate::AppState;
4use axum::{
5    extract::{Path, State},
6    http::{HeaderMap, StatusCode, header},
7    response::IntoResponse,
8};
9
10#[derive(Debug)]
11pub struct S3File {
12    pub name: String,
13    pub link: String,
14}
15
16pub async fn download_file(
17    State(_data): State<Arc<AppState>>,
18    Path(_file_name): Path<String>,
19) -> Result<impl IntoResponse, StatusCode> {
20    /*    let client = &data.s3.clone();
21
22    let get_req = GetObjectRequest {
23        bucket: data.conf.s3_bucket.clone(),
24        key: format!("files/{}", file_name),
25        ..Default::default()
26    };
27
28    let result = client
29        .get_object(get_req)
30        .await
31        .map_err(|_| StatusCode::NOT_FOUND)?;
32
33    let content_type = result
34        .content_type
35        .unwrap_or("application/octet-stream".to_string());
36    let body = result.body.ok_or(StatusCode::INTERNAL_SERVER_ERROR);
37
38    let stream = FramedRead::new(
39        body.expect("No result").into_async_read(),
40        BytesCodec::new(),
41    );
42    let bytes = Body::from_stream(stream);
43     */
44    let content_type = "application/octet-stream";
45    let bytes = vec![1, 2, 3];
46    let mut headers = HeaderMap::new();
47    headers.insert(header::CONTENT_TYPE, content_type.parse().unwrap());
48    headers.insert(header::CONTENT_DISPOSITION, "attachment".parse().unwrap());
49
50    Ok((headers, bytes).into_response())
51}
52
53pub async fn list_s3(State(_data): State<Arc<AppState>>) -> Result<Vec<S3File>, StatusCode> {
54    /*
55    let client = &data.s3.clone();
56
57    let list_req = ListObjectsRequest {
58        bucket: data.conf.s3_bucket.clone(),
59        prefix: Some("files/".to_string()),
60        ..Default::default()
61    };
62
63    client
64        .list_objects(list_req)
65        .await
66        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
67        .contents
68        .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
69        .iter()
70        .map(|obj| -> Result<S3File, StatusCode> {
71            let name = obj
72                .key
73                .as_ref()
74                .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
75                .strip_prefix("files/")
76                .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
77                .to_owned();
78            let link = format!("/api/files/download/{}", name);
79
80            Ok(S3File { name, link })
81        })
82    .collect()
83     */
84    Ok(vec![])
85}