-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilehandler.go
92 lines (83 loc) · 2.36 KB
/
filehandler.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
package main
import (
"fmt"
"go-file-processing-daemon/decode"
"os"
"time"
"github.com/bikedataproject/go-bike-data-lib/dbmodel"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
// HandleFitFile : Process a single fit file
func HandleFitFile(file string) error {
// Convert FIT to contribution
contribution, err := decode.FitToContribution(file)
if err != nil {
return fmt.Errorf("Could not convert .FIT to contribution: %v", err)
}
// Get userID from FIT
userID, err := decode.GetProviderID(file)
if err != nil {
return fmt.Errorf("Could not convert .FIT to user: %v", err)
}
// Fetch user data
user, err := db.GetUserData(userID)
// Check if user exists; if not create a new object
if user.ID == "" {
user = dbmodel.User{
Provider: "web/Fileupload",
ProviderUser: userID,
IsHistoryFetched: true,
ExpiresAt: -1,
ExpiresIn: -1,
TokenCreationDate: time.Now(),
UserIdentifier: uuid.New().String(),
}
usr, err := db.AddUser(&user)
if err != nil {
return fmt.Errorf("Could not create new user: %v", err)
}
user = usr
}
// Add contribution
if err := db.AddContribution(&contribution, &user); err != nil {
return fmt.Errorf("Could not create contribution: %v", err)
}
log.Infof("Added contribution for user %v", userID)
os.Remove(file)
return nil
}
// HandleGpxFile : Process a single GPX file
func HandleGpxFile(file string) error {
contribution, err := decode.GpxToContribution(file)
if err != nil {
return fmt.Errorf("Could not convert .GPX to contribution: %v", err)
}
// Use anonymous user since GPX files
// Fetch user data
user, err := db.GetUserData("AnonymousFileUpload")
// Check if user exists; if not create a new object
if user.ID == "" {
user = dbmodel.User{
Provider: "web/Fileupload",
ProviderUser: "AnonymousFileUpload",
IsHistoryFetched: true,
ExpiresAt: -1,
ExpiresIn: -1,
TokenCreationDate: time.Now(),
UserIdentifier: uuid.New().String(),
}
usr, err := db.AddUser(&user)
if err != nil {
return fmt.Errorf("Could not create new user: %v", err)
}
user = usr
}
// Add contribution
if err := db.AddContribution(&contribution, &user); err != nil {
return fmt.Errorf("Could not create contribution: %v", err)
}
log.Infof("Added contribution for user %v", user.ID)
os.Remove(file)
return nil
}