-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
51 lines (42 loc) · 1.22 KB
/
history.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
package request
import (
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/kyleu/npn/app/lib/filesystem"
"github.com/kyleu/npn/app/util"
)
const shouldSaveHistory = true
func (s *Service) saveHistory(userID *uuid.UUID, coll string, req *Request, p string, msg []byte) error {
if shouldSaveHistory {
hp := s.historyPath(userID, coll, req.Key)
now := time.Now()
hfn := path.Join(hp, util.TimeToFull(&now)+".json")
hd := filepath.Dir(hfn)
err := s.files.CreateDirectory(hd)
if err != nil {
return errors.Wrap(err, "unable to create request history directory ["+hd+"]")
}
x, _ := os.Stat(p)
if x == nil {
err = s.files.WriteFile(hfn, msg, filesystem.DefaultMode, true)
} else {
err = s.files.CopyFile(p, hfn)
}
if err != nil {
return errors.Wrap(err, "unable to create request history ["+hp+"]")
}
}
err := s.files.WriteFile(p, msg, filesystem.DefaultMode, true)
if err != nil {
return errors.Wrap(err, "unable to write file")
}
return nil
}
func (s *Service) historyPath(userID *uuid.UUID, coll string, key string) string {
return path.Join(strings.TrimSuffix(s.dirFor(userID, coll), "requests"), "history", "requests", key)
}