-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
76 lines (68 loc) · 1.73 KB
/
storage.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
/*
* Copyright 2014–2018 Kullo GmbH
*
* This source code is licensed under the 3-clause BSD license. See LICENSE.txt
* in the root directory of this source tree for details.
*/
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"io/ioutil"
"mime/multipart"
)
type Metadata struct {
Prod string
Ver string
Guid string
Ptime string
Ctime string
Email string
Comments string
}
func writeToDisk(dumpDirectory *string, fileHeader []*multipart.FileHeader, attribs map[string][]string) (string, error) {
var meta Metadata
meta.Prod = getFirstOrEmptyString(attribs["prod"])
meta.Ver = getFirstOrEmptyString(attribs["ver"])
meta.Guid = getFirstOrEmptyString(attribs["guid"])
meta.Ptime = getFirstOrEmptyString(attribs["ptime"])
meta.Ctime = getFirstOrEmptyString(attribs["ctime"])
meta.Email = getFirstOrEmptyString(attribs["email"])
meta.Comments = getFirstOrEmptyString(attribs["comments"])
jsonMeta, err := json.Marshal(meta)
if err != nil {
return "", err
}
const NEWLINE = byte(0x0A)
jsonMeta = append(jsonMeta, NEWLINE)
basename := getRandomFilename()
err = ioutil.WriteFile(*dumpDirectory+"/"+basename+".json", jsonMeta, 0600)
if err != nil {
return "", err
}
dumpFile, err := fileHeader[0].Open()
if err != nil {
return "", err
}
dump, err := ioutil.ReadAll(dumpFile)
if err != nil {
return "", err
}
err = ioutil.WriteFile(*dumpDirectory+"/"+basename+".dmp", dump, 0600)
if err != nil {
return "", err
}
return basename, nil
}
func getFirstOrEmptyString(strings []string) string {
if len(strings) == 0 {
return ""
}
return strings[0]
}
func getRandomFilename() string {
rndBytes := make([]byte, 16)
rand.Read(rndBytes)
return hex.EncodeToString(rndBytes)
}