1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use super::rpc::Rpc;
use prost::Message;
use std::fmt;
mod proto {
include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.chatfile.rs");
}
pub struct ChatFile {}
impl ChatFile {
pub fn cli(command: &str) {
match command {
cmd if cmd.starts_with("send ") => {
let command_string = cmd.strip_prefix("send ").unwrap().to_string();
let mut iter = command_string.split_whitespace();
if let Some(group_id_str) = iter.next() {
let group_id;
match Self::id_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id.clone();
}
Err(_e) => match Self::uuid_string_to_bin(group_id_str.to_string()) {
Ok(id) => {
group_id = id.clone();
}
_ => {
log::error!("Invalid group id");
return;
}
},
}
if let Some(file_path_name) = iter.next() {
let descr = match iter.next() {
Some(description) => description.to_string(),
_ => "".to_string(),
};
log::trace!(
"send file to group={}, file path={}, description={}",
group_id_str,
file_path_name,
descr
);
Self::send_file(group_id, file_path_name.to_string(), descr);
} else {
log::error!("file pathname is not given");
}
} else {
log::error!("chat send command incorrectly formatted");
}
}
cmd if cmd.starts_with("history") => {
let mut offset: i32 = 0;
let mut limit: i32 = 10;
if cmd.starts_with("history ") {
let command_string = cmd.strip_prefix("history ").unwrap().to_string();
let mut iter = command_string.split_whitespace();
if let Some(offset_str) = iter.next() {
offset = offset_str.to_string().parse().unwrap();
if let Some(limit_str) = iter.next() {
limit = limit_str.to_string().parse().unwrap();
}
}
}
Self::send_file_history_command(offset as u32, limit as u32);
}
_ => log::error!("unknown file command"),
}
}
fn id_string_to_bin(id: String) -> Result<Vec<u8>, String> {
if id.len() < 52 {
return Err("Group ID not long enough".to_string());
}
match bs58::decode(id).into_vec() {
Ok(id_bin) => Ok(id_bin),
Err(e) => {
let err = fmt::format(format_args!("{}", e));
Err(err)
}
}
}
fn uuid_string_to_bin(id_str: String) -> Result<Vec<u8>, String> {
match uuid::Uuid::parse_str(id_str.as_str()) {
Ok(id) => Ok(id.as_bytes().to_vec()),
_ => Err("invalid group id".to_string()),
}
}
fn send_file(group_id: Vec<u8>, file_name: String, description: String) {
let proto_message = proto::ChatFile {
message: Some(proto::chat_file::Message::SendFileRequest(
proto::SendFileRequest {
path_name: file_name.clone(),
group_id: group_id.clone(),
description: description.clone(),
},
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
super::rpc::proto::Modules::Chatfile.into(),
"".to_string(),
);
}
fn send_file_history_command(offset: u32, limit: u32) {
let proto_message = proto::ChatFile {
message: Some(proto::chat_file::Message::FileHistory(
proto::FileHistoryRequest { offset, limit },
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
super::rpc::proto::Modules::Chatfile.into(),
"".to_string(),
);
}
pub fn rpc(data: Vec<u8>) {
match proto::ChatFile::decode(&data[..]) {
Ok(file_share) => {
match file_share.message {
Some(proto::chat_file::Message::FileHistoryResponse(proto_file_history)) => {
println!("====================================");
println!("File Sharing Histories");
println!("------------------------------------");
println!(
"offset={}, limit={}, total={}",
proto_file_history.offset,
proto_file_history.limit,
proto_file_history.total
);
for entry in proto_file_history.histories {
println!("[{}] - {}", entry.file_id, entry.file_name);
println!("\t Time: {}, SenderId: {}", entry.time, entry.sender_id);
println!("\t Group Id: {}", entry.group_id);
println!(
"\t FileSize: {}, Description: {}",
entry.file_size, entry.file_description
);
println!("");
}
}
_ => {
log::error!("unprocessable RPC file message");
}
}
}
Err(error) => {
log::error!("{:?}", error);
}
}
}
}