use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::neo_fs::types::{Attributes, ContainerId, ObjectId, ObjectType, OwnerId};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Object {
pub id: Option<ObjectId>,
pub container_id: ContainerId,
pub owner_id: OwnerId,
pub object_type: ObjectType,
#[serde(skip_serializing, skip_deserializing)]
pub payload: Vec<u8>,
pub attributes: Attributes,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(with = "chrono::serde::ts_seconds_option")]
pub created_at: Option<DateTime<Utc>>,
}
impl Object {
pub fn new(container_id: ContainerId, owner_id: OwnerId) -> Self {
Self {
id: None,
container_id,
owner_id,
object_type: ObjectType::Regular,
payload: Vec::new(),
attributes: Attributes::new(),
created_at: None,
}
}
pub fn with_payload(mut self, payload: Vec<u8>) -> Self {
self.payload = payload;
self
}
pub fn with_type(mut self, object_type: ObjectType) -> Self {
self.object_type = object_type;
self
}
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.add(key, value);
self
}
pub fn with_filename(self, filename: impl Into<String>) -> Self {
self.with_attribute("FileName", filename)
}
pub fn with_content_type(self, content_type: impl Into<String>) -> Self {
self.with_attribute("Content-Type", content_type)
}
pub fn size(&self) -> usize {
self.payload.len()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultipartUpload {
pub id: Option<ObjectId>,
pub container_id: ContainerId,
pub owner_id: OwnerId,
pub upload_id: String,
pub attributes: Attributes,
pub part_size: u64,
pub max_parts: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Part {
pub part_number: u32,
#[serde(skip_serializing, skip_deserializing)]
pub payload: Vec<u8>,
}
impl Part {
pub fn new(part_number: u32, payload: Vec<u8>) -> Self {
Self { part_number, payload }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultipartUploadResult {
pub object_id: ObjectId,
pub container_id: ContainerId,
}