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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
mod sender;
use std::env;
use std::ffi::OsStr;
use std::fs::{create_dir, File, OpenOptions, read_dir};
use std::io::{Error, ErrorKind, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{PathBuf};
/// **This is a file manager**.
pub struct FileManager;
impl FileManager {
/// Creates a new File.
/// # Arguments
/// * `path` - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
/// # Example
/// ```
/// use filelib::FileManager;
/// FileManager::create_file("file.txt").unwrap();
/// FileManager::create_file("C:\\user\\documents\\file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be created, an error is returned.
pub fn create_file(path: &str) -> Result<File, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
Ok(File::create(root)?)
}
/// Writes a string to a file.
/// # Arguments
/// * `path` - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
/// * `content` - The content to write to the file.
/// # Example
/// ```
/// use filelib::FileManager;
/// FileManager::write_to_file("file.txt", "Hello World!").unwrap();
/// FileManager::write_to_file("C:\\user\\documents\\file.txt", "Hello World!").unwrap();
/// ```
/// # Errors
/// If the file cannot be written, an error is returned.
pub fn write_to_file(path: &str, data: &str) -> Result<(), Error> {
if !FileManager::file_exists(path) {
let mut file = FileManager::create_file(path)?;
file.write_all(data.as_bytes())?;
file.write_all("\n".as_ref())?;
Ok(())
} else {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
let mut file = OpenOptions::new().append(true).open(root)?;
file.write_all(data.as_bytes())?;
file.write_all("\n".as_ref())?;
Ok(())
}
}
/// Cleans a file.
/// # Arguments
/// * `path` - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
/// # Example
/// ```
/// use filelib::FileManager;
/// FileManager::clean_file("file.txt").unwrap();
/// FileManager::clean_file("C:\\user\\documents\\file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be cleaned, an error is returned.
pub fn clean_file(path: &str) -> Result<(), Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
FileManager::delete_file(root.to_str().unwrap())?;
FileManager::create_file(path)?;
Ok(())
}
/// Reads a file and returns its content.
/// # Arguments
/// * `path` - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
/// # Example
/// ```
/// use filelib::FileManager;
/// let cont1 = FileManager::read_file("file.txt").unwrap();
/// let cont2 = FileManager::read_file("C:\\user\\documents\\file.txt").unwrap();
/// ```
pub fn read_file(path: &str) -> std::io::Result<String> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
let mut file = File::open(root)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
/// Checks if a file exists.
/// # Arguments
/// * `path` - The path of the file.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::file_exists("file.txt").unwrap();
/// FileManager::file_exists("C:\\user\\documents\\file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be found, an error is returned.
pub fn file_exists(path: &str) -> bool {
let mut root = PathBuf::from(path);
root = Utils::get_file_name(root, path);
root.exists()
}
/// Deletes a file.
/// # Arguments
/// * `path` - The path of the file.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::delete_file("file.txt").unwrap();
/// FileManager::delete_file("C:\\user\\documents\\file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be deleted, an error is returned.
pub fn delete_file(path: &str) -> Result<bool, Error> {
let root = PathBuf::from(path);
let root = Utils::get_file_name(root, path);//.to_str().unwrap().to_string()
std::fs::remove_file(root)?;
Ok(true)
}
/// Renames a file
/// # Arguments
/// * `path` - The path of the file.
/// * `new_name` - The new name of the file.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::rename_file("file.txt", "new_file.txt").unwrap();
/// FileManager::rename_file("C:\\user\\documents\\file.txt", "new_file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be renamed, an error is returned.
pub fn rename_file(path: &str, new_name: &str) -> Result<(), Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path);
std::fs::rename(root, new_name)?;
} else {
std::fs::rename(root, new_name)?;
}
Ok(())
}
/// Changes a file extension
/// # Arguments
/// * `path` - The path of the file.
/// * `new_extension` - The new extension of the file.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::change_extension("file.txt", "rs").unwrap();
/// FileManager::change_extension("C:\\user\\documents\\file.txt", "rs").unwrap();
/// ```
/// # Errors
/// If the file cannot be renamed, an error is returned.
pub fn change_extension(path: &str, new_extension: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
let mut has_changed = false;
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path);
has_changed = root.set_extension(new_extension)
} else {
has_changed = root.set_extension(new_extension)
}
if has_changed {
let content = FileManager::read_file(path)?;
FileManager::delete_file(path)?;
FileManager::write_to_file(root.to_str().unwrap(), &content)?;
}
Ok(true)
}
/// Copies a file
/// # Arguments
/// * `path` - The path of the file.
/// * `another_path` - The path of the new file.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::copy_file("file.txt", "C:\\user\\documents\\file.txt").unwrap();
/// FileManager::copy_file("C:\\user\\documents\\file.txt", "C:\\user\\documents\\file.txt").unwrap();
/// ```
/// # Errors
/// If the file cannot be found, an error is returned.
///
/// If the paths have the same value , an error is returned.
pub fn copy_file(path: &str, another_path: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
let mut another_root = PathBuf::from(another_path);
match root == another_root {
true => {
return Err(Error::new(ErrorKind::Other, "The paths have the same value."));
}
false => {
if !Utils::is_absolute(another_root.to_str().unwrap()) {
another_root = Utils::user_route();
}
another_root.push(another_path);
std::fs::copy(root, another_root)?;
Ok(true)
}
}
}
/// Creates a directory
/// # Arguments
/// * `path` - The path of the directory.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::create_dir("C:\\user\\documents\\some_dir").unwrap();
/// FileManager::create_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
/// FileManager::create_dir("some_dir3").unwrap();
/// ```
/// # Errors
/// If the directory cannot be created, an error is returned.
pub fn create_dir(path: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path);
}
create_dir(root)?;
Ok(true)
}
/// Deletes a directory
/// # Arguments
/// * `path` - The path of the directory.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::delete_dir("C:\\user\\documents\\some_dir").unwrap();
/// FileManager::delete_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
/// FileManager::delete_dir("some_dir3").unwrap();
/// ```
/// # Errors
/// If the directory cannot be deleted, an error is returned.
pub fn delete_dir(path: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path);
}
std::fs::remove_dir(root)?;
Ok(true)
}
/// Renames a directory
/// # Arguments
/// * `path` - The path of the directory.
/// * `new_name` - The new name of the directory.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::rename_dir("C:\\user\\documents\\some_dir", "new_dir").unwrap();
/// FileManager::rename_dir("C:\\user\\documents\\some_dir\\some_dir2", "new_dir2").unwrap();
/// FileManager::rename_dir("some_dir3", "new_dir3").unwrap();
/// ```
/// # Errors
/// If the directory cannot be renamed, an error is returned.
pub fn rename_dir(path: &str, new_name: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
let mut new_name = PathBuf::from(new_name);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path);
}
std::fs::rename(root, new_name)?;
Ok(true)
}
/// Copies a directory
/// # Arguments
/// * `path` - The path of the directory.
/// * `another_path` - The new path of the directory.
/// # Example
/// ```
/// use filelib::{FileManager};
/// FileManager::copy_dir("C:\\user\\documents\\some_dir", "C:\\user\\documents\\some_dir2").unwrap();
/// FileManager::copy_dir("C:\\user\\documents\\some_dir\\some_dir2", "C:\\user\\documents\\some_dir2\\some_dir3").unwrap();
/// FileManager::copy_dir("some_dir3", "some_dir4").unwrap();
/// ```
/// # Errors
/// If the directory is not found, an error is returned.
///
/// If the paths have the same value , an error is returned.
pub fn copy_dir(path: &str, another_path: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
let mut another_root = PathBuf::from(another_path);
let mut new_path: PathBuf = Default::default();
match root == another_root {
true => {
return Err(Error::new(ErrorKind::Other, "The paths have the same value."));
}
false => {
if !Utils::is_absolute(another_root.to_str().unwrap()) {
another_root = Utils::user_route();
}
another_root.push(another_path);
new_path = PathBuf::from(another_root.to_str().unwrap());
create_dir(new_path.clone())?;
for file in read_dir(root).unwrap() {
let file = file.unwrap();
let file_name = file.file_name().into_string().unwrap();
let file_path = file.path();
let another_path = new_path.join(file_name);
if file_path.is_dir() {
FileManager::copy_dir(file_path.to_str().unwrap(), another_path.to_str().unwrap())?;
} else {
FileManager::copy_file(file_path.to_str().unwrap(), another_path.to_str().unwrap())?;
}
}
Ok(true)
}
}
}
}
/// **These are some utilities**.
pub struct Utils;
impl Utils {
/// Checks if the specified path is an absolute path.
/// # Arguments
/// * `path` - The path to check.
/// # Example
/// ```
/// use filelib::Utils;
/// let is_absolute = Utils::is_absolute("C:\\Users\\user\\Desktop\\file.txt").unwrap();
/// assert_eq!(is_absolute, true)
pub fn is_absolute(path: &str) -> bool {
PathBuf::from(path).is_absolute()
}
/// Returns the root directory of the project.
/// # Example
/// ```
/// use filelib::Utils;
/// let root = Utils::get_root();
/// ```
pub fn get_route() -> String {
Utils::user_route().to_str().unwrap().to_string()
}
fn user_route() -> PathBuf {
let bin = env::current_exe().expect("exe path");
let mut target_dir = PathBuf::from(bin.parent().expect("bin parent"));
while target_dir.file_name() != Some(OsStr::new("target")) {
target_dir.pop();
}
target_dir.pop();
let mut new_path = String::new();
let mut first: char = ' ';
for c in target_dir.display().to_string().chars() {
if c.is_alphabetic() {
first = c;
break;
}
}
new_path.push(first);
new_path.push_str(target_dir.display().to_string().split(first).collect::<Vec<&str>>()[1].to_string().as_str());
PathBuf::from(new_path)
}
/// Checks if the specified path is a file.
/// # Arguments
/// * `path` - The path to check.
/// # Example
/// ```
/// use filelib::Utils;
/// let is_file = Utils::is_file("file.txt").unwrap();
/// assert_eq!(is_file, true)
pub fn is_file(path: &str) -> bool {
PathBuf::from(path).is_file()
}
fn get_file_name(mut root: PathBuf, path: &str) -> PathBuf {
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
} else {
root.push(path)
}
root
}
/// Returns the size of a file in bytes.
/// # Arguments
/// * `path` - The path of the file.
/// # Example
/// ```
/// use filelib::Utils;
/// let size = Utils::get_file_size("file.txt").unwrap();
/// assert_eq!(size, 1405)
/// ```
/// # Errors
/// If the file cannot be found, an error is returned.
pub fn get_file_size(path: &str) -> Result<u64, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
let metadata = std::fs::metadata(root)?;
Ok(metadata.len())
}
/// Returns the size of a directory in bytes.
/// # Arguments
/// * `path` - The path of the directory.
/// # Example
/// ```
/// use filelib::Utils;
/// let size = Utils::get_dir_size("C:\\user\\documents\\some_dir").unwrap();
/// assert_eq!(size, 1454)
/// ```
/// # Errors
/// If the directory cannot be found, an error is returned.
pub fn get_dir_size(path: &str) -> Result<u64, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
let mut size = 0;
for entry in read_dir(root)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
size += Utils::get_dir_size(path.to_str().unwrap())?;
} else {
size += Utils::get_file_size(path.to_str().unwrap())?;
}
}
Ok(size)
}
}
/// **This is a simple file sender**.
pub struct FileSender;
impl FileSender {
/// Sends a file to the specified ip address.
/// # Arguments
/// * `path` - The path of the file.
/// * `address` - The address to send the file to.
/// # Example
/// ```
/// use filelib::FileSender;
/// FileSender::send_file("file.txt", "192.168.1.1:8080").unwrap();
/// ```
/// # Errors
/// If the file cannot be found, an error is returned.
/// If the address is invalid, an error is returned.
pub fn send_file(path: &str, ip: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
let mut file = File::open(root)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let mut stream = TcpStream::connect(ip)?;
stream.write(&buffer)?;
Ok(true)
}
/// Waits for a file on the specified ip address.
/// # Arguments
/// * `path` - The path of the directory.
/// * `address` - The address to send the directory to.
/// # Example
/// ```
/// use filelib::FileSender;
/// FileSender::receive_file("C:\\user\\documents\\some_dir", "192.168.1.1:8080").unwrap();
/// ```
/// # Errors
/// If the directory cannot be found, an error is returned.
/// If the address is invalid, an error is returned.
pub fn receive_file(path: &str, ip: &str) -> Result<bool, Error> {
let mut root = PathBuf::from(path);
if !Utils::is_absolute(root.to_str().unwrap()) {
root = Utils::user_route();
root.push(path)
}
match TcpListener::bind(ip)?.accept()?
{
(mut stream, _) => {
let mut buffer = Vec::new();
stream.read_to_end(&mut buffer)?;
let mut file = File::create(root)?;
file.write(&buffer)?;
Ok(true)
}
}
}
}