Skip to content

Commit e20e191

Browse files
feat: add sc commands for files
1 parent 6b67489 commit e20e191

1 file changed

Lines changed: 37 additions & 11 deletions

File tree

src/types.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use std::{collections::HashMap, str::FromStr};
22
use std::fmt::Display;
3-
4-
53
use anyhow::anyhow;
64
use serde::{Deserialize, Serialize};
75
use wg_internal::{network::NodeId, packet::Packet};
86
use crossbeam_channel::Sender;
97
use uuid::Uuid;
10-
11-
128
pub type Bytes = Vec<u8>;
139

1410
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
@@ -56,9 +52,9 @@ impl FromStr for MediaReference {
5652
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
5753
pub struct TextFile{
5854
pub id: Uuid,
59-
title: String,
60-
content: String,
61-
media_refs: Vec<MediaReference>
55+
pub title: String,
56+
pub content: String,
57+
pub media_refs: Vec<MediaReference>
6258
}
6359

6460

@@ -89,10 +85,31 @@ impl TextFile {
8985
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
9086
pub struct MediaFile {
9187
pub id: Uuid,
92-
title: String,
88+
pub title: String,
9389
content: Vec<Bytes>,
9490
}
9591

92+
impl MediaFile {
93+
pub fn new(title: String, content: Vec<Bytes>) -> Self {
94+
Self {
95+
id: Uuid::new_v4(),
96+
title,
97+
content,
98+
}
99+
}
100+
101+
pub fn get_title(&self) -> &str {
102+
&self.title
103+
}
104+
105+
pub fn get_content(&self) -> &Vec<Bytes> {
106+
&self.content
107+
}
108+
109+
pub fn get_size(&self) -> usize {
110+
self.content.iter().map(|chunk| chunk.len()).sum()
111+
}
112+
}
96113

97114
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
98115
pub struct File {
@@ -214,7 +231,6 @@ pub enum ChatEvent {
214231
MessageReceived(Message)
215232
}
216233

217-
218234
#[derive(Debug, Clone)]
219235
pub enum WebCommand {
220236
GetCachedFiles,
@@ -223,6 +239,12 @@ pub enum WebCommand {
223239
GetTextFile(Uuid),
224240
GetMediaFiles,
225241
GetMediaFile(Uuid),
242+
AddTextFile(TextFile),
243+
AddTextFileFromPath(String),
244+
AddMediaFile(MediaFile),
245+
AddMediaFileFromPath(String),
246+
RemoveTextFile(Uuid),
247+
RemoveMediaFile(Uuid),
226248
}
227249

228250
#[derive(Debug, Clone)]
@@ -234,6 +256,11 @@ pub enum WebEvent {
234256
MediaFiles(Vec<MediaFile>),
235257
MediaFile(MediaFile),
236258
FileNotFound(Uuid),
259+
TextFileAdded(Uuid),
260+
MediaFileAdded(Uuid),
261+
TextFileRemoved(Uuid),
262+
MediaFileRemoved(Uuid),
263+
FileOperationError(String),
237264
}
238265

239266

@@ -244,7 +271,6 @@ pub enum NodeEvent {
244271
NodeRemoved(NodeId)
245272
}
246273

247-
248274
#[derive(Debug, Clone)]
249275
pub enum NodeCommand {
250276
AddSender(NodeId, Sender<Packet>),
@@ -278,4 +304,4 @@ pub enum ServerType {
278304
ChatServer,
279305
TextServer,
280306
MediaServer,
281-
}
307+
}

0 commit comments

Comments
 (0)