~vpzom/lotide

ref: a180781e02e14910f367bf2c704b84103e84b8c2 lotide/src/routes/api/media.rs -rw-r--r-- 2.3 KiB
a180781e — Colin Reeder Include score and optionally your_vote in every post list (#109) 4 months ago
                                                                                
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
63
64
65
66
67
68
use std::sync::Arc;

async fn route_unstable_media_create(
    _: (),
    ctx: Arc<crate::RouteContext>,
    req: hyper::Request<hyper::Body>,
) -> Result<hyper::Response<hyper::Body>, crate::Error> {
    let lang = crate::get_lang_for_req(&req);

    let content_type = req
        .headers()
        .get(hyper::header::CONTENT_TYPE)
        .ok_or_else(|| {
            crate::Error::UserError(crate::simple_response(
                hyper::StatusCode::BAD_REQUEST,
                lang.tr("missing_content_type", None).into_owned(),
            ))
        })?;
    let content_type = std::str::from_utf8(content_type.as_ref())?;
    let content_type: mime::Mime = content_type.parse()?;

    if content_type.type_() != mime::IMAGE {
        return Err(crate::Error::UserError(crate::simple_response(
            hyper::StatusCode::BAD_REQUEST,
            lang.tr("media_upload_not_image", None).into_owned(),
        )));
    }

    let db = ctx.db_pool.get().await?;

    let user = crate::require_login(&req, &db).await?;

    if let Some(media_location) = &ctx.media_location {
        let filename = uuid::Uuid::new_v4().to_string();
        let path = media_location.join(&filename);

        {
            use futures::TryStreamExt;
            use tokio::io::AsyncWriteExt;
            let file = tokio::fs::File::create(path).await?;
            req.into_body()
                .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
                .try_fold(file, |mut file, chunk| async move {
                    file.write_all(chunk.as_ref()).await.map(|_| file)
                })
                .await?;
        }

        let id = crate::Pineapple::generate();

        db.execute(
            "INSERT INTO media (id, path, person, mime) VALUES ($1, $2, $3, $4)",
            &[&id.as_int(), &filename, &user, &content_type.as_ref()],
        )
        .await?;

        crate::json_response(&serde_json::json!({"id": id.to_string()}))
    } else {
        Err(crate::Error::UserError(crate::simple_response(
            hyper::StatusCode::INTERNAL_SERVER_ERROR,
            lang.tr("media_upload_not_configured", None).into_owned(),
        )))
    }
}

pub fn route_media() -> crate::RouteNode<()> {
    crate::RouteNode::new().with_handler_async("POST", route_unstable_media_create)
}