-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeembbededfile.go
83 lines (68 loc) · 2.04 KB
/
writeembbededfile.go
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
package raft
import (
"io"
"strconv"
"sourcecode.social/reiver/go-erorr"
)
// writeEmbeddedFile embeds a(nother) file into a RAFT file using the provided file-name, file-size, and file-content.
//
// Note that this adds an additional "\n" after the file-content.
// Technically it isn't necessary.
// But it is added to make the resulting raft file easier to read (if a person was to try to read it).
func writeEmbeddedFile(writer io.Writer, filename string, filesize int64, filecontent io.Reader) error {
if nil == writer {
return errNilWriter
}
if nil == filecontent {
return errNilReader
}
// file name
{
var value string = filename
{
err := writeString(writer, value)
if nil != err {
return erorr.Errorf("raft: problem writing embedded-file's file-name (i.e., %q): %w", value, err)
}
}
{
err := writeString(writer, eol)
if nil != err {
return erorr.Errorf("raft: problem writing end-of-line (i.e., %q) after writing embedded-file's file-name (i.e., %q): %w", eol, value, err)
}
}
}
// file size
{
var value string = strconv.FormatInt(filesize, 10)
{
err := writeString(writer, value)
if nil != err {
return erorr.Errorf("raft: problem writing embedded-file's file-size — %q: %w", value, err)
}
}
{
err := writeString(writer, eol)
if nil != err {
return erorr.Errorf("raft: problem writing end-of-line (i.e., %q) after writing embedded-file's file-size (i.e., %q): %w", eol, value, err)
}
}
}
// file content
{
{
err := copyN(writer, filecontent, filesize)
if nil != err {
return erorr.Errorf("raft: problem writing embedded-file's file-content: %w", err)
}
}
{
var value string = eoleol // <---- Note this is a 'eoleol' rather than just a single 'eol'. This is because the 2nd 'eol' add a blank line that makes it easier for a programmer to read the file.
err := writeString(writer, value)
if nil != err {
return erorr.Errorf("raft: problem writing 2 end-of-lines (i.e., %q) after writing file-content: %w", value, err)
}
}
}
return nil
}