generated from t/malachite
add: full initial
This commit is contained in:
parent
f5c663495d
commit
d06bc5e726
29 changed files with 592 additions and 1928 deletions
19
crates/buckets-core/Cargo.toml
Normal file
19
crates/buckets-core/Cargo.toml
Normal file
|
@ -0,0 +1,19 @@
|
|||
[package]
|
||||
name = "buckets-core"
|
||||
description = "Buckets media upload types"
|
||||
version = "1.0.1"
|
||||
edition = "2024"
|
||||
readme = "../../README.md"
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
homepage.workspace = true
|
||||
|
||||
[dependencies]
|
||||
tetratto-core = "15.0.2"
|
||||
tetratto-shared = "12.0.6"
|
||||
pathbufd = "0.1.4"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.142"
|
||||
toml = "0.9.4"
|
||||
oiseau = { version = "0.1.2", default-features = false, features = ["postgres", "redis",] }
|
59
crates/buckets-core/src/config.rs
Normal file
59
crates/buckets-core/src/config.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use oiseau::config::{Configuration, DatabaseConfig};
|
||||
use pathbufd::PathBufD;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// The directory files are stored in (relative to cwd).
|
||||
#[serde(default = "default_directory")]
|
||||
pub directory: String,
|
||||
/// Database configuration.
|
||||
#[serde(default = "default_database")]
|
||||
pub database: DatabaseConfig,
|
||||
}
|
||||
|
||||
fn default_directory() -> String {
|
||||
"buckets".to_string()
|
||||
}
|
||||
|
||||
fn default_database() -> DatabaseConfig {
|
||||
DatabaseConfig::default()
|
||||
}
|
||||
|
||||
impl Configuration for Config {
|
||||
fn db_config(&self) -> DatabaseConfig {
|
||||
self.database.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
directory: default_directory(),
|
||||
database: default_database(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Read the configuration file.
|
||||
pub fn read() -> Self {
|
||||
toml::from_str(
|
||||
&match std::fs::read_to_string(PathBufD::current().join("app.toml")) {
|
||||
Ok(x) => x,
|
||||
Err(_) => {
|
||||
let x = Config::default();
|
||||
|
||||
std::fs::write(
|
||||
PathBufD::current().join("app.toml"),
|
||||
&toml::to_string_pretty(&x).expect("failed to serialize config"),
|
||||
)
|
||||
.expect("failed to write config");
|
||||
|
||||
return x;
|
||||
}
|
||||
},
|
||||
)
|
||||
.expect("failed to deserialize config")
|
||||
}
|
||||
}
|
28
crates/buckets-core/src/database/mod.rs
Normal file
28
crates/buckets-core/src/database/mod.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
mod sql;
|
||||
mod uploads;
|
||||
|
||||
use crate::config::Config;
|
||||
use oiseau::{execute, postgres::DataManager as OiseauManager, postgres::Result as PgResult};
|
||||
use tetratto_core::model::{Error, Result};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DataManager(pub OiseauManager<Config>);
|
||||
|
||||
impl DataManager {
|
||||
/// Create a new [`DataManager`].
|
||||
pub async fn new(config: Config) -> PgResult<Self> {
|
||||
Ok(Self(OiseauManager::new(config).await?))
|
||||
}
|
||||
|
||||
/// Initialize tables.
|
||||
pub async fn init(&self) -> Result<()> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
execute!(&conn, sql::CREATE_TABLE_UPLOADS).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
7
crates/buckets-core/src/database/sql/create_uploads.sql
Normal file
7
crates/buckets-core/src/database/sql/create_uploads.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE IF NOT EXISTS uploads (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
bucket TEXT NOT NULL,
|
||||
metadata TEXT NOT NULL
|
||||
)
|
1
crates/buckets-core/src/database/sql/mod.rs
Normal file
1
crates/buckets-core/src/database/sql/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub const CREATE_TABLE_UPLOADS: &str = include_str!("./create_uploads.sql");
|
167
crates/buckets-core/src/database/uploads.rs
Normal file
167
crates/buckets-core/src/database/uploads.rs
Normal file
|
@ -0,0 +1,167 @@
|
|||
use crate::{
|
||||
DataManager,
|
||||
model::{MediaUpload, UploadMetadata},
|
||||
};
|
||||
use oiseau::{PostgresRow, cache::Cache, execute, get, params, query_rows};
|
||||
use tetratto_core::auto_method;
|
||||
use tetratto_core::model::{Error, Result};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`MediaUpload`] from an SQL row.
|
||||
pub(crate) fn get_upload_from_row(x: &PostgresRow) -> MediaUpload {
|
||||
MediaUpload {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
bucket: get!(x->3(String)),
|
||||
metadata: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_upload_by_id(usize as i64)@get_upload_from_row -> "SELECT * FROM uploads WHERE id = $1" --name="upload" --returns=MediaUpload --cache-key-tmpl="atto.upload:{}");
|
||||
|
||||
/// Get all uploads (paginated).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `batch` - the limit of items in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_uploads(&self, batch: usize, page: usize) -> Result<Vec<MediaUpload>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM uploads ORDER BY created DESC LIMIT $1 OFFSET $2",
|
||||
&[&(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_upload_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("upload".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all uploads by their owner (paginated).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `owner` - the ID of the owner of the upload
|
||||
/// * `batch` - the limit of items in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_uploads_by_owner(
|
||||
&self,
|
||||
owner: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<MediaUpload>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_upload_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("upload".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all uploads by their owner.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `owner` - the ID of the owner of the upload
|
||||
pub async fn get_uploads_by_owner_all(&self, owner: usize) -> Result<Vec<MediaUpload>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(owner as i64)],
|
||||
|x| { Self::get_upload_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("upload".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new upload in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`MediaUpload`] object to insert
|
||||
pub async fn create_upload(&self, data: MediaUpload) -> Result<MediaUpload> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
data.metadata.validate_kv()?;
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO uploads VALUES ($1, $2, $3, $4, $5)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.bucket,
|
||||
&serde_json::to_string(&data.metadata).unwrap().as_str(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// return
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_upload(&self, id: usize) -> Result<()> {
|
||||
// if !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
|
||||
// return Err(Error::NotAllowed);
|
||||
// }
|
||||
|
||||
// delete file
|
||||
// it's most important that the file gets off the file system first, even
|
||||
// if there's an issue in the database
|
||||
//
|
||||
// the actual file takes up much more space than the database entry.
|
||||
let upload = self.get_upload_by_id(id).await?;
|
||||
upload.remove(&self.0.0.directory)?;
|
||||
|
||||
// delete from database
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.0.1.remove(format!("atto.upload:{}", id)).await;
|
||||
|
||||
// return
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_upload_metadata(UploadMetadata) -> "UPDATE uploads SET metadata = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.upload:{}");
|
||||
}
|
7
crates/buckets-core/src/lib.rs
Normal file
7
crates/buckets-core/src/lib.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
pub mod model;
|
||||
|
||||
mod database;
|
||||
pub use database::DataManager;
|
||||
|
||||
mod config;
|
||||
pub use config::Config;
|
122
crates/buckets-core/src/model.rs
Normal file
122
crates/buckets-core/src/model.rs
Normal file
|
@ -0,0 +1,122 @@
|
|||
use pathbufd::PathBufD;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{exists, remove_file, write},
|
||||
};
|
||||
use tetratto_core::model::{Error, Result};
|
||||
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum MediaType {
|
||||
#[serde(alias = "image/webp")]
|
||||
Webp,
|
||||
#[serde(alias = "image/avif")]
|
||||
Avif,
|
||||
#[serde(alias = "image/png")]
|
||||
Png,
|
||||
#[serde(alias = "image/jpg")]
|
||||
Jpg,
|
||||
#[serde(alias = "image/gif")]
|
||||
Gif,
|
||||
#[serde(alias = "image/carpgraph")]
|
||||
Carpgraph,
|
||||
}
|
||||
|
||||
impl MediaType {
|
||||
pub fn extension(&self) -> &str {
|
||||
match self {
|
||||
Self::Webp => "webp",
|
||||
Self::Avif => "avif",
|
||||
Self::Png => "png",
|
||||
Self::Jpg => "jpg",
|
||||
Self::Gif => "gif",
|
||||
Self::Carpgraph => "carpgraph",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mime(&self) -> String {
|
||||
format!("image/{}", self.extension())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UploadMetadata {
|
||||
pub what: MediaType,
|
||||
#[serde(default)]
|
||||
pub alt: String,
|
||||
#[serde(default)]
|
||||
pub kv: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl UploadMetadata {
|
||||
pub fn validate_kv(&self) -> Result<()> {
|
||||
for x in &self.kv {
|
||||
if x.0.len() > 32 {
|
||||
return Err(Error::DataTooLong("key".to_string()));
|
||||
}
|
||||
|
||||
if x.1.len() > 128 {
|
||||
return Err(Error::DataTooLong("value".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MediaUpload {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub owner: usize,
|
||||
pub bucket: String,
|
||||
pub metadata: UploadMetadata,
|
||||
}
|
||||
|
||||
impl MediaUpload {
|
||||
/// Create a new [`MediaUpload`].
|
||||
pub fn new(what: MediaType, owner: usize, bucket: String) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
owner,
|
||||
bucket,
|
||||
metadata: UploadMetadata {
|
||||
alt: String::new(),
|
||||
what,
|
||||
kv: HashMap::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the path to the fs file for this upload.
|
||||
pub fn path(&self, directory: &str) -> PathBufD {
|
||||
PathBufD::current().extend(&[
|
||||
directory,
|
||||
&format!("{}.{}", self.id, self.metadata.what.extension()),
|
||||
])
|
||||
}
|
||||
|
||||
/// Write to this upload in the file system.
|
||||
pub fn write(&self, directory: &str, bytes: &[u8]) -> Result<()> {
|
||||
match write(self.path(directory), bytes) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(Error::MiscError(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete this upload in the file system.
|
||||
pub fn remove(&self, directory: &str) -> Result<()> {
|
||||
let path = self.path(directory);
|
||||
|
||||
if !exists(&path).unwrap() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match remove_file(path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(Error::MiscError(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue