Skip to content

Commit

Permalink
fix file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
GKaszewski committed Oct 19, 2024
1 parent 9dee0ee commit dfa2f69
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 128 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
dockerfile
.dockerignore
.git
.gitignore
3 changes: 3 additions & 0 deletions .env.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_DB=podcast
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ axum = {version = "0.7.5", features = ["multipart"]}
include_dir = "0.7"
uuid = { version = "1.6.0", features = ["v4"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }
bytes = "1.7.2"

[[bin]]
name = "podcast_loco-cli"
Expand Down
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
web:
build:
context: .
dockerfile: dockerfile
volumes:
- media_volume:/usr/app/podcasts
depends_on:
- db
expose:
- 5150
db:
image: postgres:16.4
volumes:
- postgres_data:/var/lib/postgresql/data
env_file:
- .env.db
nginx:
build: ./nginx
volumes:
- media_volume:/usr/app/podcasts
ports:
- '3969:80'
depends_on:
- web
volumes:
postgres_data:
media_volume:
18 changes: 18 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM rust:1.74-slim as builder

WORKDIR /usr/src/

COPY . .

RUN cargo build --release

FROM debian:bookworm-slim

WORKDIR /usr/app

COPY --from=builder /usr/src/config /usr/app/config
COPY --from=builder /usr/src/target/release/podcast_loco-cli /usr/app/podcast_loco-cli

RUN mkdir /usr/app/podcasts

ENTRYPOINT ["/usr/app/podcast_loco-cli", "-e", "production", "start"]
4 changes: 4 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.27

RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d
46 changes: 46 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# server {
# listen 80;
# server_name ~^(?<subdomain>\w*)\.localhost$;

# location / {
# if ($http_x_subdomain = "") {
# set $http_x_subdomain $subdomain;
# }
# proxy_set_header X-Subdomain $http_x_subdomain;
# proxy_pass http://localhost:5150/;
# }
# }

upstream web {
server web:5150;
}


server {
listen 80;
client_max_body_size 250M;

location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /audio {
alias /usr/app/podcasts;

autoindex on;
types {
mp3 audio/mpeg;
m4a audio/mp4;
ogg audio/ogg;
opus audio/ogg;
flac audio/flac;
wav audio/wav;
}

add_header Accept-Ranges bytes;
}
}
Binary file not shown.
Binary file not shown.
14 changes: 4 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ use loco_rs::{
use migration::Migrator;
use sea_orm::DatabaseConnection;

use crate::{
controllers,
models::_entities::{notes, users},
tasks,
workers::downloader::DownloadWorker,
};
use crate::{controllers, models::_entities::users, tasks, workers::downloader::DownloadWorker};

pub struct App;
#[async_trait]
Expand All @@ -47,7 +42,6 @@ impl Hooks for App {
AppRoutes::with_default_routes()
.add_route(controllers::podcast::routes())
.prefix("/api")
.add_route(controllers::notes::routes())
.add_route(controllers::auth::routes())
.add_route(controllers::user::routes())
}
Expand All @@ -58,8 +52,10 @@ impl Hooks for App {
}

async fn after_context(ctx: AppContext) -> Result<AppContext> {
let store = storage::drivers::local::new_with_prefix("podcasts").map_err(Box::from)?;

Ok(AppContext {
storage: Storage::single(storage::drivers::local::new()).into(),
storage: Storage::single(store).into(),
..ctx
})
}
Expand All @@ -70,13 +66,11 @@ impl Hooks for App {

async fn truncate(db: &DatabaseConnection) -> Result<()> {
truncate_table(db, users::Entity).await?;
truncate_table(db, notes::Entity).await?;
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<notes::ActiveModel>(db, &base.join("notes.yaml").display().to_string()).await?;
Ok(())
}
}
2 changes: 0 additions & 2 deletions src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod auth;
pub mod notes;
pub mod user;

pub mod podcast;
75 changes: 0 additions & 75 deletions src/controllers/notes.rs

This file was deleted.

34 changes: 21 additions & 13 deletions src/controllers/podcast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::unnecessary_struct_initialization)]
#![allow(clippy::unused_async)]
use bytes::Bytes;
use std::path::PathBuf;

use axum::extract::Multipart;
Expand Down Expand Up @@ -46,13 +47,14 @@ pub async fn add(

let mut file = None;

while let Some(field) = payload.next_field().await.map_err(|err| {
tracing::error!(error = ?err,"could not readd multipart");
Error::BadRequest("could not readd multipart".into())
})? {
let name = field.name().unwrap();
while let Some(field) = payload
.next_field()
.await
.map_err(|_| Error::BadRequest("could not readd multipart".into()))?
{
let name = field.name().unwrap().to_string();

match name {
match name.as_str() {
"title" => {
title = Some(field.text().await.unwrap());
}
Expand All @@ -62,10 +64,15 @@ pub async fn add(
_ => return Err(Error::BadRequest("content type not found".into())),
};

let content = field.bytes().await.map_err(|err| {
tracing::error!(error = ?err, "could not read field bytes");
Error::BadRequest("could not read field bytes".into())
})?;
let file_name = match field.file_name() {
Some(file_name) => String::from(file_name),
_ => return Err(Error::BadRequest("file name not found".into())),
};

let content = field
.bytes()
.await
.map_err(|err| Error::BadRequest(format!("failed to read bytes: {}", err)))?;

let audio_content_types = vec![
"audio/mpeg",
Expand All @@ -82,13 +89,14 @@ pub async fn add(
let file_name = format!(
"{}.{}",
Uuid::new_v4(),
content_type.split("/").last().unwrap()
file_name.split('.').last().unwrap_or("mp3")
);

let path = PathBuf::from("podcasts").join(&file_name);
let path = PathBuf::from("podcasts/").join(&file_name);

ctx.storage
.as_ref()
.upload(path.as_path(), &content)
.upload(path.as_path(), &Bytes::from(content))
.await?;

url = Some(format!("/audio/{}", file_name));
Expand Down
1 change: 0 additions & 1 deletion src/models/_entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
pub mod prelude;

pub mod notes;
pub mod podcasts;
pub mod users;
18 changes: 0 additions & 18 deletions src/models/_entities/notes.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/models/_entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
pub use super::notes::Entity as Notes;
pub use super::podcasts::Entity as Podcasts;
pub use super::users::Entity as Users;
1 change: 0 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod _entities;
pub mod notes;
pub mod users;
pub mod podcasts;
7 changes: 0 additions & 7 deletions src/models/notes.rs

This file was deleted.

0 comments on commit dfa2f69

Please sign in to comment.