use enigma_packet::{
AttachmentChunkMeta, AttachmentKind, AttachmentMeta, Message, MessageMeta, MessageType,
};
use uuid::Uuid;
pub fn build_attachment_init(
sender: &str,
receiver: &str,
attachment_id: Uuid,
kind: AttachmentKind,
filename: Option<&str>,
content_type: Option<&str>,
total_size: u64,
chunk_size: u32,
chunk_count: u32,
timestamp_ms: u64,
) -> Message {
let meta = AttachmentMeta {
attachment_id,
kind,
filename: filename.map(|s| s.to_string()),
content_type: content_type.map(|s| s.to_string()),
total_size,
chunk_size,
chunk_count,
sha256: None,
created_at_ms: Some(timestamp_ms),
};
Message {
id: Uuid::new_v4(),
sender: sender.to_string(),
receiver: receiver.to_string(),
timestamp_ms,
msg_type: MessageType::AttachmentInit,
payload: Vec::new(),
meta: MessageMeta::AttachmentInit(meta),
}
}
pub fn build_attachment_chunks(
sender: &str,
receiver: &str,
attachment_id: Uuid,
bytes: &[u8],
chunk_size: usize,
base_timestamp_ms: u64,
) -> Vec<Message> {
if chunk_size == 0 {
return Vec::new();
}
let mut out = Vec::new();
let mut index: u32 = 0;
let mut offset: u64 = 0;
let mut ts = base_timestamp_ms;
for chunk in bytes.chunks(chunk_size) {
let meta = AttachmentChunkMeta {
attachment_id,
index,
offset,
chunk_size: chunk.len() as u32,
total_size: Some(bytes.len() as u64),
};
out.push(Message {
id: Uuid::new_v4(),
sender: sender.to_string(),
receiver: receiver.to_string(),
timestamp_ms: ts,
msg_type: MessageType::AttachmentChunk,
payload: chunk.to_vec(),
meta: MessageMeta::AttachmentChunk(meta),
});
index = index.saturating_add(1);
offset = offset.saturating_add(chunk.len() as u64);
ts = ts.saturating_add(1);
}
out
}
pub fn build_attachment_end(
sender: &str,
receiver: &str,
attachment_id: Uuid,
total_size: u64,
chunk_count: u32,
timestamp_ms: u64,
) -> Message {
Message {
id: Uuid::new_v4(),
sender: sender.to_string(),
receiver: receiver.to_string(),
timestamp_ms,
msg_type: MessageType::AttachmentEnd,
payload: Vec::new(),
meta: MessageMeta::AttachmentEnd {
attachment_id,
sha256: None,
total_size,
chunk_count,
},
}
}