-
Notifications
You must be signed in to change notification settings - Fork 29
/
ActivityFormat.go
75 lines (58 loc) · 1.75 KB
/
ActivityFormat.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
package connect
import (
"path/filepath"
"strings"
)
// ActivityFormat is a file format for importing and exporting activities.
type ActivityFormat int
const (
// ActivityFormatFIT is the "original" Garmin format.
ActivityFormatFIT ActivityFormat = iota
// ActivityFormatTCX is Training Center XML (TCX) format.
ActivityFormatTCX
// ActivityFormatGPX will export as GPX - the GPS Exchange Format.
ActivityFormatGPX
// ActivityFormatKML will export KML files compatible with Google Earth.
ActivityFormatKML
// ActivityFormatCSV will export splits as CSV.
ActivityFormatCSV
activityFormatMax
activityFormatInvalid
)
const (
// ErrUnknownFormat will be returned if the activity file format is unknown.
ErrUnknownFormat = Error("Unknown format")
)
var (
activityFormatTable = map[string]ActivityFormat{
"fit": ActivityFormatFIT,
"tcx": ActivityFormatTCX,
"gpx": ActivityFormatGPX,
"kml": ActivityFormatKML,
"csv": ActivityFormatCSV,
}
)
// Extension returns an appropriate filename extension for format.
func (f ActivityFormat) Extension() string {
for extension, format := range activityFormatTable {
if format == f {
return extension
}
}
return ""
}
// FormatFromExtension tries to guess the format from a file extension.
func FormatFromExtension(extension string) (ActivityFormat, error) {
extension = strings.ToLower(extension)
format, found := activityFormatTable[extension]
if !found {
return activityFormatInvalid, ErrUnknownFormat
}
return format, nil
}
// FormatFromFilename tries to guess the format based on a filename (or path).
func FormatFromFilename(filename string) (ActivityFormat, error) {
extension := filepath.Ext(filename)
extension = strings.TrimPrefix(extension, ".")
return FormatFromExtension(extension)
}