-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsiegfried.go
97 lines (79 loc) · 2.21 KB
/
siegfried.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package filetrove
import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"github.com/richardlehane/siegfried"
)
// SiegfriedType is a struct for all the strings siegfried returns
type SiegfriedType struct {
FileName string
SizeInByte int64
Registry string
FMT string
FormatName string
FormatVersion string
MIMEType string
IdentificationNote string
IdentificationProof string
SiegOutput string
}
const SiegfriedVersion = "1_11"
// GetSiegfriedDB downloads the signature db
func GetSiegfriedDB(installPath string) error {
sigurl := "https://www.itforarchivists.com/siegfried/latest/" + SiegfriedVersion + "/default"
// We download siegfried's database derived from DROID here, PRONOM based
// TODO: Check license note with Richard
resp, err := http.Get(sigurl)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New("Could not download siegfried signature. Server returned: " + resp.Status)
}
// Create the signature file in the db subdirectory
out, err := os.Create(filepath.Join(installPath, "db", "siegfried.sig"))
if err != nil {
return err
}
defer out.Close()
// Write the body to the signature file
_, err = io.Copy(out, resp.Body)
return err
}
// SiegfriedIdent gets PRONOM metadata and the size of a single file
func SiegfriedIdent(s *siegfried.Siegfried, inFile string) (SiegfriedType, error) {
var MetaOneFile SiegfriedType
var oneFile string
f, err := os.Open(inFile)
if err != nil {
return MetaOneFile, err
}
defer f.Close()
fi, _ := f.Stat()
MetaOneFile.SizeInByte = fi.Size()
if fi.Size() == 0 {
return MetaOneFile, err
}
ids, err := s.Identify(f, "", "")
if err != nil {
return MetaOneFile, err
}
for _, id := range ids {
values := id.Values()
MetaOneFile.Registry = values[0]
MetaOneFile.FMT = values[1]
MetaOneFile.FormatName = values[2]
MetaOneFile.FormatVersion = values[3]
MetaOneFile.MIMEType = values[4]
MetaOneFile.IdentificationNote = values[5]
MetaOneFile.IdentificationProof = values[6]
}
MetaOneFile.FileName = inFile
MetaOneFile.SizeInByte = fi.Size()
MetaOneFile.SiegOutput = oneFile
return MetaOneFile, nil
}