use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use super::ShareType;
use super::SharePermission;
use super::ShareStatus;
use super::AuditMetadata;
use super::*;
use crate::domain::state_machine::{FileShareStateMachine, FileShareState, StateMachineError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FileShareId(pub Uuid);
impl FileShareId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for FileShareId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for FileShareId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for FileShareId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<FileShareId> for Uuid {
fn from(id: FileShareId) -> Self { id.0 }
}
impl AsRef<Uuid> for FileShareId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for FileShareId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct FileShare {
pub id: Uuid,
pub file_id: Uuid,
pub owner_id: Uuid,
pub token: String,
pub share_type: ShareType,
pub permission: SharePermission,
pub shared_with: Vec<Uuid>,
pub password_hash: Option<String>,
pub max_downloads: Option<i32>,
pub download_count: i32,
pub expires_at: Option<DateTime<Utc>>,
pub(crate) status: ShareStatus,
pub revoked_at: Option<DateTime<Utc>>,
pub revoked_by: Option<Uuid>,
pub message: Option<String>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl FileShare {
pub fn builder() -> FileShareBuilder {
<FileShareBuilder as Default>::default()
}
pub fn new(file_id: Uuid, owner_id: Uuid, token: String, share_type: ShareType, permission: SharePermission, shared_with: Vec<Uuid>, download_count: i32, status: ShareStatus) -> Self {
Self {
id: Uuid::new_v4(),
file_id,
owner_id,
token,
share_type,
permission,
shared_with,
password_hash: None,
max_downloads: None,
download_count,
expires_at: None,
status,
revoked_at: None,
revoked_by: None,
message: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> FileShareId {
FileShareId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn status(&self) -> &ShareStatus {
&self.status
}
pub fn with_password_hash(mut self, value: String) -> Self {
self.password_hash = Some(value);
self
}
pub fn with_max_downloads(mut self, value: i32) -> Self {
self.max_downloads = Some(value);
self
}
pub fn with_expires_at(mut self, value: DateTime<Utc>) -> Self {
self.expires_at = Some(value);
self
}
pub fn with_revoked_at(mut self, value: DateTime<Utc>) -> Self {
self.revoked_at = Some(value);
self
}
pub fn with_revoked_by(mut self, value: Uuid) -> Self {
self.revoked_by = Some(value);
self
}
pub fn with_message(mut self, value: String) -> Self {
self.message = Some(value);
self
}
pub fn transition_to(&mut self, new_state: FileShareState) -> Result<(), StateMachineError> {
let current = self.status.to_string().parse::<FileShareState>()?;
let mut sm = FileShareStateMachine::from_state(current);
sm.transition_to_state(new_state)?;
self.status = new_state.to_string().parse::<ShareStatus>()
.map_err(|e| StateMachineError::InvalidState(e.to_string()))?;
Ok(())
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"file_id" => {
if let Ok(v) = serde_json::from_value(value) { self.file_id = v; }
}
"owner_id" => {
if let Ok(v) = serde_json::from_value(value) { self.owner_id = v; }
}
"token" => {
if let Ok(v) = serde_json::from_value(value) { self.token = v; }
}
"share_type" => {
if let Ok(v) = serde_json::from_value(value) { self.share_type = v; }
}
"permission" => {
if let Ok(v) = serde_json::from_value(value) { self.permission = v; }
}
"shared_with" => {
if let Ok(v) = serde_json::from_value(value) { self.shared_with = v; }
}
"password_hash" => {
if let Ok(v) = serde_json::from_value(value) { self.password_hash = v; }
}
"max_downloads" => {
if let Ok(v) = serde_json::from_value(value) { self.max_downloads = v; }
}
"download_count" => {
if let Ok(v) = serde_json::from_value(value) { self.download_count = v; }
}
"expires_at" => {
if let Ok(v) = serde_json::from_value(value) { self.expires_at = v; }
}
"revoked_at" => {
if let Ok(v) = serde_json::from_value(value) { self.revoked_at = v; }
}
"revoked_by" => {
if let Ok(v) = serde_json::from_value(value) { self.revoked_by = v; }
}
"message" => {
if let Ok(v) = serde_json::from_value(value) { self.message = v; }
}
_ => {} }
}
}
pub fn is_valid(&self) -> bool {
self.status == ShareStatus::Active
&& !self.is_expired()
&& self.has_downloads_remaining()
}
pub fn can_access(&self, user_id: Option<Uuid>, password: Option<&str>) -> bool {
if !self.is_valid() {
return false;
}
match self.share_type {
ShareType::Link => true,
ShareType::User => {
match user_id {
Some(uid) => self.shared_with.contains(&uid),
None => false,
}
}
ShareType::Password => {
match (password, &self.password_hash) {
(Some(pw), Some(hash)) => {
bcrypt::verify(pw, hash).unwrap_or(false)
}
_ => false,
}
}
}
}
pub fn record_download(&mut self) -> bool {
if !self.has_downloads_remaining() {
return false;
}
self.download_count += 1;
true
}
pub fn revoke(&mut self, by_user_id: Uuid) {
self.status = ShareStatus::Revoked;
self.revoked_at = Some(Utc::now());
self.revoked_by = Some(by_user_id);
}
pub fn is_expired(&self) -> bool {
match self.expires_at {
Some(expires) => expires <= Utc::now(),
None => false,
}
}
pub fn has_downloads_remaining(&self) -> bool {
match self.max_downloads {
Some(max) => self.download_count < max,
None => true,
}
}
pub fn check_invariants(&self) -> Result<(), Vec<&'static str>> {
let mut errors = Vec::new();
if self.token.is_empty() {
errors.push("token must not be empty");
}
if self.token.contains("..") {
errors.push("token must not contain '..'");
}
if self.download_count < 0 {
errors.push("download_count must be non-negative");
}
if let Some(max) = self.max_downloads {
if self.download_count > max {
errors.push("download_count must not exceed max_downloads");
}
}
if self.share_type == ShareType::Password && self.password_hash.is_none() {
errors.push("password share must have a password_hash");
}
if errors.is_empty() { Ok(()) } else { Err(errors) }
}
}
impl super::Entity for FileShare {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"FileShare"
}
}
impl backbone_core::PersistentEntity for FileShare {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for FileShare {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("file_id".to_string(), "uuid".to_string());
m.insert("owner_id".to_string(), "uuid".to_string());
m.insert("share_type".to_string(), "share_type".to_string());
m.insert("permission".to_string(), "share_permission".to_string());
m.insert("status".to_string(), "share_status".to_string());
m.insert("expires_at".to_string(), "timestamptz".to_string());
m.insert("revoked_at".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["token"]
}
fn relations() -> &'static [(&'static str, &'static str, &'static str)] {
&[("file", "stored_files", "fileId")]
}
}
#[derive(Debug, Clone, Default)]
pub struct FileShareBuilder {
file_id: Option<Uuid>,
owner_id: Option<Uuid>,
token: Option<String>,
share_type: Option<ShareType>,
permission: Option<SharePermission>,
shared_with: Option<Vec<Uuid>>,
password_hash: Option<String>,
max_downloads: Option<i32>,
download_count: Option<i32>,
expires_at: Option<DateTime<Utc>>,
status: Option<ShareStatus>,
revoked_at: Option<DateTime<Utc>>,
revoked_by: Option<Uuid>,
message: Option<String>,
}
impl FileShareBuilder {
pub fn file_id(mut self, value: Uuid) -> Self {
self.file_id = Some(value);
self
}
pub fn owner_id(mut self, value: Uuid) -> Self {
self.owner_id = Some(value);
self
}
pub fn token(mut self, value: String) -> Self {
self.token = Some(value);
self
}
pub fn share_type(mut self, value: ShareType) -> Self {
self.share_type = Some(value);
self
}
pub fn permission(mut self, value: SharePermission) -> Self {
self.permission = Some(value);
self
}
pub fn shared_with(mut self, value: Vec<Uuid>) -> Self {
self.shared_with = Some(value);
self
}
pub fn password_hash(mut self, value: String) -> Self {
self.password_hash = Some(value);
self
}
pub fn max_downloads(mut self, value: i32) -> Self {
self.max_downloads = Some(value);
self
}
pub fn download_count(mut self, value: i32) -> Self {
self.download_count = Some(value);
self
}
pub fn expires_at(mut self, value: DateTime<Utc>) -> Self {
self.expires_at = Some(value);
self
}
pub fn status(mut self, value: ShareStatus) -> Self {
self.status = Some(value);
self
}
pub fn revoked_at(mut self, value: DateTime<Utc>) -> Self {
self.revoked_at = Some(value);
self
}
pub fn revoked_by(mut self, value: Uuid) -> Self {
self.revoked_by = Some(value);
self
}
pub fn message(mut self, value: String) -> Self {
self.message = Some(value);
self
}
pub fn build(self) -> Result<FileShare, String> {
let file_id = self.file_id.ok_or_else(|| "file_id is required".to_string())?;
let owner_id = self.owner_id.ok_or_else(|| "owner_id is required".to_string())?;
let token = self.token.ok_or_else(|| "token is required".to_string())?;
let shared_with = self.shared_with.ok_or_else(|| "shared_with is required".to_string())?;
Ok(FileShare {
id: Uuid::new_v4(),
file_id,
owner_id,
token,
share_type: self.share_type.unwrap_or_default(),
permission: self.permission.unwrap_or_default(),
shared_with,
password_hash: self.password_hash,
max_downloads: self.max_downloads,
download_count: self.download_count.unwrap_or(0),
expires_at: self.expires_at,
status: self.status.unwrap_or_default(),
revoked_at: self.revoked_at,
revoked_by: self.revoked_by,
message: self.message,
metadata: AuditMetadata::default(),
})
}
}