-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
58 lines (51 loc) · 1.63 KB
/
file.go
File metadata and controls
58 lines (51 loc) · 1.63 KB
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
package nzb
import (
"strings"
"github.com/smquartz/errors"
)
// File describes the file elements within an NZB. It has appropriate tags and
// methods to enable deserialisation from XML.
type File struct {
// string describing who posted the NZB
Poster string `xml:"poster,attr"`
// unix formatted date that the NZB was posted
Date int `xml:"date,attr"`
// describes what the contents of the NZB file are
Subject string `xml:"subject,attr"`
// usenet groups associated with the NZB
Groups []string `xml:"groups>group,internalxml"`
// actual file segments to be downloaded
Segments []Segment `xml:"segments>segment"`
}
// Segment describes a piece of an NZB file, to be downloaded separately. It
// has appropriate tags and methods to enable deserialisation from XML.
type Segment struct {
// number of the segment relative to the file
Number int `xml:"number,attr"`
// size of the segment in bytes
Size int `xml:"bytes,attr"`
// identifier of the segment
ID string `xml:",innerxml"`
}
// ApproximatedName returns the approximated name of the file that an NZB file
// entry represents.
func (f *File) ApproximatedName() (string, error) {
parts := strings.Split(f.Subject, `"`)
n := ""
if len(parts) > 1 {
n = strings.Replace(parts[1], "/", "-", -1)
} else {
return "", errors.Errorf("could not parse subject")
}
return n, nil
}
// Size returns the size in bytes of the file. It returns the sum of the sizes
// of its segments.
func (f *File) Size() (size int64) {
// iterate over all file segments
for _, segment := range f.Segments {
// add the segment's size to the file's size
size += int64(segment.Size)
}
return size
}