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
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

use md5::{Digest, Md5};

pub fn get_checksum(file: File) -> String {
    let mut hasher = Md5::new();
    let mut buf_reader = BufReader::new(file);
    loop {
        let length = {
            let buf = buf_reader.fill_buf().unwrap();
            hasher.update(buf);
            buf.len()
        };
        if length == 0 {
            break;
        }
        buf_reader.consume(length);
    }
    let a = hasher.finalize();
    format!("{:x}", a)
}

#[derive(Debug)]
pub struct MetaData {
    pub path: PathBuf,
    pub size: u64,
    pub is_file: bool,
    pub is_dir: bool,
    pub is_symlink: bool,
    pub checksum: Option<String>,
}

impl MetaData {
    pub fn new() -> MetaData {
        MetaData {
            path: PathBuf::new(),
            size: 0,
            is_file: false,
            is_dir: false,
            is_symlink: false,
            checksum: None,
        }
    }

    pub fn strip_prefix<T: AsRef<Path>>(&mut self, root: T) {
        self.path = self.path.strip_prefix(root).unwrap().to_path_buf()
    }

    fn name_ex_to_binary(&self) -> Vec<u8> {
        let mut binary: Vec<u8> = Vec::new();
        let mut name = self.path.to_str().unwrap().to_string();
        while name.len() > u16::MAX.into() {
            name.pop();
        }
        let length: u16 = name.len().try_into().unwrap();
        let length = length.to_be_bytes();
        binary.push(length[0]);
        binary.push(length[1]);

        for i in name.bytes() {
            binary.push(i);
        }

        binary
    }

    fn type_size_to_binary(&self) -> Vec<u8> {
        let mut binary: Vec<u8> = Vec::new();

        let mut flag_and_size: u8 = 0;
        if let true = self.is_file {
            flag_and_size += 0x80;
        }
        if let true = self.is_dir {
            flag_and_size += 0x40;
        }
        if let true = self.is_symlink {
            flag_and_size += 0x20;
        }

        let mut index = 0;
        for byte in self.size.to_be_bytes() {
            if byte == 0 {
                index += 1;
            } else {
                break;
            }
        }
        let size_bytes_count = (self.size.to_le_bytes().len() - index) as u8;
        flag_and_size += size_bytes_count;
        binary.push(flag_and_size);
        for i in &self.size.to_le_bytes()[..size_bytes_count as usize] {
            binary.push(*i);
        }

        binary
    }

    fn checksum_to_binary(&self) -> Vec<u8> {
        let mut binary: Vec<u8> = Vec::new();
        match &self.checksum {
            Some(c) => {
                for i in c.as_bytes() {
                    binary.push(*i);
                }
            }
            None => {
                for _ in 0..32 {
                    binary.push(0);
                }
            }
        }
        binary
    }

    pub fn serialize(&self) -> Vec<u8> {
        let mut binary: Vec<u8> = Vec::new();
        binary.append(&mut self.name_ex_to_binary());
        binary.append(&mut self.type_size_to_binary());
        binary.append(&mut self.checksum_to_binary());
        binary
    }

    pub fn deserialize_path(&mut self, name_binary: &[u8]) {
        self.path = match String::from_utf8(name_binary.to_vec()) {
            Ok(n) => PathBuf::from(n),
            Err(_) => PathBuf::from("untitled.bin"),
        };
    }
    pub fn deserialize_type(&mut self, type_flag: u8) {
        self.is_file = match type_flag & 0x80 {
            0 => false,
            _ => true,
        };
        self.is_dir = match type_flag & 0x40 {
            0 => false,
            _ => true,
        };
        self.is_symlink = match type_flag & 0x20 {
            0 => false,
            _ => true,
        };
    }

    pub fn deserialize_size(&mut self, size_binary: &[u8]) {
        self.size = super::binary_to_u64(size_binary);
    }

    pub fn deserialize_checksum(&mut self, checksum_binary: &[u8]) {
        self.checksum = match String::from_utf8(checksum_binary.to_vec()) {
            Ok(c) => Some(c),
            Err(_) => Some(String::from("00000000000000000000000000000000")),
        };
    }
}

impl<T: AsRef<Path>> From<&T> for MetaData {
    fn from(file_path: &T) -> Self {
        match File::open(&file_path) {
            Ok(file) => {
                return MetaData {
                    path: file_path.as_ref().to_path_buf(),
                    size: match file.metadata() {
                        Ok(m) => m.len(),
                        Err(_) => 0,
                    },
                    is_file: match file.metadata() {
                        Ok(m) => m.is_file(),
                        Err(_) => false,
                    },
                    is_dir: match file.metadata() {
                        Ok(m) => m.is_dir(),
                        Err(_) => false,
                    },
                    is_symlink: match file.metadata() {
                        Ok(m) => m.is_symlink(),
                        Err(_) => false,
                    },
                    checksum: { Some(get_checksum(file)) },
                }
            }
            Err(_) => return MetaData::new(),
        };
    }
}

impl PartialEq for MetaData {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
            && self.size == other.size
            && self.is_file == other.is_file
            && self.is_dir == other.is_dir
            && self.is_symlink == other.is_symlink
            && self.checksum == other.checksum
    }
}

#[cfg(test)]
mod tests {

    use std::{collections::VecDeque, path::PathBuf};

    use crate::serialize::get_file_list;

    use super::MetaData;

    const ORIGINAL_FILE: &str = "tests/original_images/dir1/board-g43968feec_1920.jpg";

    #[test]
    fn metadata_compare_test() {
        let original = PathBuf::from("tests");

        let original_file_vec = get_file_list(&original).unwrap();
        let mut original_metadata_vec = Vec::new();
        for f in original_file_vec {
            let meta = MetaData::from(&f);
            original_metadata_vec.push(meta);
        }
        // path clearance
        let mut original_metadata_vec: Vec<MetaData> = original_metadata_vec
            .iter()
            .map(|m| MetaData {
                path: PathBuf::from(m.path.file_name().unwrap()),
                size: m.size,
                is_file: m.is_file,
                is_dir: m.is_dir,
                is_symlink: m.is_symlink,
                checksum: Some(m.checksum.clone().unwrap()),
            })
            .collect();
        let mut result_metadata_vec = Vec::from([
            MetaData {
                path: PathBuf::from("colorful-2174045.png"),
                size: 464447,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("4e42993bfd2756df48b646d68433db1e")),
            },
            MetaData {
                path: PathBuf::from("capsules-g869437822_1920.jpg"),
                size: 371728,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("60e191a914756ff7ae259e33f40f20da")),
            },
            MetaData {
                path: PathBuf::from("board-g43968feec_1920.jpg"),
                size: 914433,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("37ca14866812327e1776d8cbb250501c")),
            },
            MetaData {
                path: PathBuf::from("laboratory-g8f9267f5f_1920.jpg"),
                size: 418648,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("0bc9b40f01fd8d4c0deb5a76f430a778")),
            },
            MetaData {
                path: PathBuf::from("폭발.jpg"),
                size: 562560,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("4753aff9b06a34832ad1de0a69d5dcd3")),
            },
            MetaData {
                path: PathBuf::from("digitization-1755812_1920.jpg"),
                size: 468460,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("4b6cab47e9193a4aebe4c8c6b7c88c1b")),
            },
            MetaData {
                path: PathBuf::from("syringe-ge5e95bfe6_1920.jpg"),
                size: 253304,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("a7385d8a719c3036a857e21225c5bd6b")),
            },
            MetaData {
                path: PathBuf::from("books-g6617d4d97_1920.jpg"),
                size: 564004,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("65aee1442129f56a0a6157c6b55f80c9")),
            },
            MetaData {
                path: PathBuf::from("test-pattern-152459.png"),
                size: 55262,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("a09d4eab0326ba5403369035531f9308")),
            },
            MetaData {
                path: PathBuf::from("tv-g87676cdfb_1280.png"),
                size: 1280855,
                is_file: true,
                is_dir: false,
                is_symlink: false,
                checksum: Some(String::from("91517821bc6851b0d9abec5d5adea961")),
            },
        ]);
        original_metadata_vec.sort_by_key(|m| m.path.clone());
        result_metadata_vec.sort_by_key(|m| m.path.clone());
        assert_eq!(original_metadata_vec, result_metadata_vec);
    }

    #[test]
    fn name_serialize_test() {
        let meta = MetaData::from(&PathBuf::from(ORIGINAL_FILE));
        assert_eq!(meta.serialize()[0], 0);
        assert_eq!(meta.serialize()[1], 52);

        let expected_meta1_bi: [u8; 52] = [
            116, 101, 115, 116, 115, 47, 111, 114, 105, 103, 105, 110, 97, 108, 95, 105, 109, 97,
            103, 101, 115, 47, 100, 105, 114, 49, 47, 98, 111, 97, 114, 100, 45, 103, 52, 51, 57,
            54, 56, 102, 101, 101, 99, 95, 49, 57, 50, 48, 46, 106, 112, 103,
        ];
        let meta1_binary = meta.serialize();
        let type_size_index = meta1_binary[0] as usize * 0x100 + meta1_binary[1] as usize;
        assert_eq!(&meta.serialize()[2..type_size_index + 2], expected_meta1_bi);
    }

    #[test]
    fn flag_size_serialize_test() {
        let meta1 = MetaData::from(&PathBuf::from(ORIGINAL_FILE));
        let binary = meta1.serialize();
        let name_end_index = binary[0] as usize * 0x100 + binary[1] as usize;
        let type_size = binary[name_end_index + 2];

        assert_eq!(type_size & 0x80, 0x80);
        assert_eq!(type_size & 0x40, 0);
        assert_eq!(type_size & 0x20, 0);

        let type_size_index = (type_size & 0xF) as usize;
        assert_eq!(type_size_index, 3);
        // 131 means it is a file, and the size takes 3 bytes.
        // And size bytes are little endian.
        assert_eq!(
            &binary[name_end_index + 3..name_end_index + type_size_index + 3],
            [1, 244, 13]
        );
    }

    #[test]
    fn checksum_serialize_test() {
        let meta1 = MetaData::from(&PathBuf::from(ORIGINAL_FILE));

        let binary = meta1.serialize();
        let name_end_index = binary[0] as usize * 0x100 + binary[1] as usize;
        let type_size = binary[name_end_index + 2];
        let type_size_index = (type_size & 0xF) as usize;

        let expected_checksum: [u8; 32] = [
            51, 55, 99, 97, 49, 52, 56, 54, 54, 56, 49, 50, 51, 50, 55, 101, 49, 55, 55, 54, 100,
            56, 99, 98, 98, 50, 53, 48, 53, 48, 49, 99,
        ];

        assert_eq!(
            &binary
                [name_end_index + type_size_index + 3..name_end_index + type_size_index + 3 + 32],
            expected_checksum
        );
    }

    #[test]
    fn metadata_serialize_test() {
        let meta1 = MetaData::from(&PathBuf::from(ORIGINAL_FILE));
        let mut binary = VecDeque::from_iter(meta1.serialize());

        println!("{:?}", meta1);

        let mut meta2 = MetaData::new();

        // Restore file path
        let path_size = binary[0] as usize * 0x100 + binary[1] as usize;
        binary.drain(..2);
        meta2.deserialize_path(&binary.drain(..path_size).collect::<Vec<u8>>());

        // Restore file type
        let flag_and_byte_count = binary.pop_front().unwrap();
        meta2.deserialize_type(flag_and_byte_count);

        // Restore file size
        let size_count = (flag_and_byte_count & 0xF) as usize;
        meta2.deserialize_size(&binary.drain(..size_count).collect::<Vec<u8>>());

        // Restore checksum
        meta2.deserialize_checksum(&binary.drain(..32).collect::<Vec<u8>>());

        assert_eq!(meta1, meta2);
    }
}