Skip to content

Commit 5bcf5d1

Browse files
committed
feat: Added file upload
1 parent fb19be3 commit 5bcf5d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+842
-225
lines changed

.rustfmt.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# unstable_features = true
1+
unstable_features = true
22

3-
# format_strings = true
4-
# imports_granularity = "Crate"
3+
format_strings = true
4+
imports_granularity = "Crate"
55
use_field_init_shorthand = true
6-
# wrap_comments = true
6+
wrap_comments = true
77
tab_spaces = 4

Cargo.lock

+66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/entity/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ path = "src/lib.rs"
1313
serde = { version = "1.0.147", features = ["derive"] }
1414
serde_json = "1.0.87"
1515
chrono = { version = "0.4.22", features = ["serde"] }
16-
sea-orm = { version = "0.10.1" }
17-
sea-query = { version = "0.27.1" }
16+
sea-orm = { version = "0.10.1", features = ["postgres-array"] }
17+
sea-query = { version = "0.27.1", features = ["postgres-array"] }
1818
getset = "0.1.2"

crates/entity/src/file.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use getset::Getters;
2+
use sea_orm::prelude::*;
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Clone, Debug, Eq, PartialEq, DeriveEntityModel, Deserialize, Serialize, Getters)]
6+
#[sea_orm(table_name = "files")]
7+
#[getset(get = "pub")]
8+
pub struct Model {
9+
#[sea_orm(primary_key)]
10+
#[serde(skip_deserializing)]
11+
pub id: i32,
12+
#[sea_orm(column_type = "Text")]
13+
pub namespace: String,
14+
#[sea_orm(column_type = "Text")]
15+
pub storage_key: String,
16+
pub tags: Vec<String>,
17+
pub metadata: Json,
18+
pub created_at: DateTimeUtc,
19+
pub updated_at: DateTimeUtc,
20+
}
21+
22+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
23+
pub enum Relation {
24+
#[sea_orm(
25+
belongs_to = "crate::namespace::Entity",
26+
from = "Column::Namespace",
27+
to = "crate::namespace::Column::Name"
28+
)]
29+
Namespace,
30+
}
31+
32+
impl Related<crate::namespace::Entity> for Entity {
33+
fn to() -> RelationDef {
34+
Relation::Namespace.def()
35+
}
36+
}
37+
38+
impl ActiveModelBehavior for ActiveModel {}

crates/entity/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod api_key;
22
pub mod blok;
3+
pub mod file;
34
pub mod git_auth;
45
pub mod image;
56
pub mod locale;

crates/migration/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod m20220917_000011_fix_blok_priority_trigger;
1515
mod m20220917_000012_fix_blok_priority_trigger;
1616
mod m20221013_000013_create_locale_table;
1717
mod m20221023_000014_add_timestamps_to_locales_data;
18+
mod m20221103_000015_create_files_table;
1819
pub(crate) mod utils;
1920

2021
pub struct Migrator;
@@ -37,6 +38,7 @@ impl MigratorTrait for Migrator {
3738
Box::new(m20220917_000012_fix_blok_priority_trigger::Migration),
3839
Box::new(m20221013_000013_create_locale_table::Migration),
3940
Box::new(m20221023_000014_add_timestamps_to_locales_data::Migration),
41+
Box::new(m20221103_000015_create_files_table::Migration),
4042
]
4143
}
4244
}

crates/migration/src/m20221013_000013_create_locale_table.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils::macros::{create_table_from_entity, exec_stmt};
2-
use entity::locale::Entity;
3-
use entity::locale_data::Entity as DataEntity;
2+
use entity::{locale::Entity, locale_data::Entity as DataEntity};
43
use sea_orm_migration::{prelude::*, MigrationName};
54

65
pub struct Migration;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use sea_orm_migration::{prelude::*, MigrationName};
2+
3+
use entity::file::Entity;
4+
5+
use crate::utils::macros::{create_table_from_entity, exec_stmt};
6+
7+
pub struct Migration;
8+
9+
impl MigrationName for Migration {
10+
fn name(&self) -> &str {
11+
"m20221103_000015_create_files_table"
12+
}
13+
}
14+
15+
#[async_trait::async_trait]
16+
impl MigrationTrait for Migration {
17+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
18+
exec_stmt!(manager, r#"drop table if exists files"#)?;
19+
create_table_from_entity!(manager, Entity)?;
20+
21+
// Set default value for created_at / updated_at columns and adds constraint
22+
exec_stmt!(
23+
manager,
24+
r#"alter table files
25+
alter column created_at set default now(),
26+
alter column updated_at set default now(),
27+
add constraint files_storage_key_unique unique (storage_key),
28+
alter column tags type text[],
29+
alter column tags set default '{{}}'::text[],
30+
alter column metadata type jsonb,
31+
alter column metadata set default '{{}}'::jsonb,
32+
drop constraint if exists "fk-files-namespace",
33+
add constraint "fk-files-namespace"
34+
foreign key (namespace)
35+
references namespaces (name)
36+
on update cascade
37+
on delete cascade
38+
"#
39+
)?;
40+
exec_stmt!(manager, r#"create index tags_idx on files (tags)"#)?;
41+
exec_stmt!(manager, r#"create index metadata_idx on files (metadata)"#)?;
42+
43+
// Trigger for timestamps
44+
exec_stmt!(
45+
manager,
46+
r#"create trigger _100_timestamps
47+
before insert or update on files
48+
for each row execute procedure tg__timestamps();
49+
"#
50+
)?;
51+
exec_stmt!(
52+
manager,
53+
r#"create trigger _500_create_missing_namespace
54+
before insert or update on files
55+
for each row execute procedure public.tg__create_missing_namespace();
56+
"#
57+
)?;
58+
59+
Ok(())
60+
}
61+
62+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
63+
manager
64+
.drop_table(Table::drop().table(Entity).to_owned())
65+
.await?;
66+
Ok(())
67+
}
68+
}

crates/server/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tracing-opentelemetry = { version = "0.18.0" }
2323
tracing-subscriber = { version = "0.3.16", features = ["registry", "env-filter"] }
2424
tracing-bunyan-formatter = "0.3.4"
2525
tracing-log = "0.1.3"
26-
sea-orm = { version = "0.10.1", features = ["debug-print", "runtime-tokio-native-tls", "sqlx-postgres"] }
26+
sea-orm = { version = "0.10.1", features = ["debug-print", "runtime-tokio-native-tls", "sqlx-postgres", "postgres-array"] }
2727
config = { version = "0.13.2" }
2828
serde_json = "1.0.87"
2929
chrono = "0.4.22"
@@ -47,7 +47,9 @@ lazy_static = { version = "1.4.0" }
4747
base64 = "0.20.0-alpha.1"
4848
reqwest = { version = "0.11.12", features = ["json", "cookies", "multipart", "stream"] }
4949
typed-builder = "0.11.0"
50+
humansize = { version = "2.1.0", features = ["impl_style"] }
5051

5152
[dev-dependencies]
53+
insta = { version = "1.21.0", features = ["json"] }
5254
portpicker = "0.1.1"
5355
test-context = "0.1.4"

crates/server/src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use aws_smithy_async::rt::sleep::default_async_sleep;
22
use aws_smithy_http::endpoint::Endpoint;
3-
use aws_types::app_name::AppName;
4-
use aws_types::region::Region;
5-
use aws_types::Credentials;
3+
use aws_types::{app_name::AppName, region::Region, Credentials};
64
use config::Environment;
75
pub use config::{Config, ConfigError};
86
use derive_more::Constructor;
@@ -92,6 +90,7 @@ impl S3Credentials {
9290
#[getset(get = "pub")]
9391
pub struct S3Buckets {
9492
image: String,
93+
file: String,
9594
}
9695

9796
impl Settings {
@@ -102,6 +101,7 @@ impl Settings {
102101
.set_default("host", "0.0.0.0")?
103102
.set_default("telemetry", false)?
104103
.set_default("s3.buckets.image", "lyonkit-images")?
104+
.set_default("s3.buckets.file", "lyonkit-files")?
105105
.set_default("cors", Vec::<String>::new())?
106106
.set_default("log_format", "json")?
107107
.add_source(

0 commit comments

Comments
 (0)