Skip to content

Commit

Permalink
image upload alpha 0.0.0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed Apr 3, 2024
1 parent c1eec88 commit 8e8d99a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ jsonwebtoken = "=7.2"
time = "0.3"
tracing = "0.1"
tracing-subscriber = "0.3"
uuid = { version = "0.8", features = ["v4"] }
futures = { version = "0.3", default-features = false }
bytes = "1.0"
2 changes: 2 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

mkdir -p ./media/profile-pictures
mkdir -p ./media/images
touch SECRET
./setup-db.sh
./build.sh
Expand Down
9 changes: 5 additions & 4 deletions setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS posts_tags;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS likes ;
DROP TABLE IF EXISTS likes;
DROP TABLE IF EXISTS images;
DROP TABLE IF EXISTS posts_images;

CREATE TABLE posts(
post_id INTEGER PRIMARY KEY NOT NULL,
Expand Down Expand Up @@ -38,12 +40,11 @@ CREATE TABLE likes(

CREATE TABLE posts_images(
post_id INTEGER NOT NULL,
image_id INTEGER NOT NULL
image_uuid VARCHAR(34) NOT NULL
);

CREATE TABLE images(
image_id INTEGER NOT NULL,
image_file TEXT NOT NULL
image_uuid VARCHAR(34) NOT NULL
);

-- ---------------- ----------- ---------------- ____________
Expand Down
50 changes: 48 additions & 2 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::collections::HashMap;

use jsonwebtoken::TokenData;
use tokio_rusqlite::params;
use tracing::info;
use tracing::{error, info};
use warp::filters::multipart::FormData;
use warp::Filter;

use bytes::BufMut;
use futures::{StreamExt, TryStreamExt};
use crate::get_token::get_token;
use crate::verify_token::{self, Claims};
use crate::types::*;
Expand Down Expand Up @@ -655,6 +657,50 @@ pub async fn change_display_name(request: DisplayNameChangeRequest) -> Result<im
}
}

pub async fn upload_image(form: FormData) -> Result<impl warp::Reply, warp::Rejection> {
let mut parts = form.into_stream();
while let Some(Ok(p)) = parts.next().await {
if p.name() == "file" {
let content_type = p.content_type();
let file_ending;
match content_type {
Some(file_type) => match file_type {
"image/png" => {
file_ending = "png";
}
v => {
error!("invalid file type found: {}", v);
return Err(warp::reject::reject());
}
},
None => {
error!("file type could not be determined");
return Err(warp::reject::reject());
}
}
let value = p
.stream()
.try_fold(Vec::new(), |mut vec, data| {
vec.put(data);
async move { Ok(vec) }
})
.await
.map_err(|e| {
error!("reading file error: {}", e);
warp::reject::reject()
})?;
let image_uuid = uuid::Uuid::new_v4().to_string();
let file_name = format!("./media/images/{}.{}", image_uuid, file_ending);
tokio::fs::write(&file_name, value).await.map_err(|e| {
error!("error writing file: {}", e);
warp::reject::reject()
})?;
info!("created file: {}", file_name);
}
}
Ok("success")
}

pub fn post_json() -> impl Filter<Extract = (PostCreateRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(display_name_change_json())
.and_then(change_display_name);

let upload_image = warp::post()
.and(warp::path("api"))
.and(warp::path("post"))
.and(warp::path("upload"))
.and(warp::path("image"))
.and(warp::multipart::form().max_length(25000000))
.and_then(upload_image);

get_posts_by_user
.or(post)
.or(get_posts)
Expand All @@ -198,6 +206,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(get_profile_by_id)
.or(get_profile_picture)
.or(change_display_name)
.or(upload_image)
// .or(get_posts_from_search)
// .or(get_users_from_search)
}
Expand All @@ -208,5 +217,4 @@ async fn main() {
let cors = warp::cors().allow_any_origin();
let routes = routes().with(cors);
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;

}
6 changes: 3 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct TagList {
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ReactionCountMap {
pub reaction_count_map: HashMap<i64, i64>
pub struct LikeCountMap {
pub like_count_map: HashMap<i64, i64>
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand All @@ -60,7 +60,7 @@ pub struct PostCreateRequest {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ReactRequest {
pub post_id: i64,
pub reaction_type: i64,
pub like_type: i64,
pub token: String
}

Expand Down
7 changes: 7 additions & 0 deletions upload-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

path="$1/api/post/upload/image"

curl --location --request POST "$path" \
--header 'Content-Type: multipart/form-data' \
--form "file=@$2"

0 comments on commit 8e8d99a

Please sign in to comment.