-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskchunkstore.go
111 lines (98 loc) · 2.75 KB
/
diskchunkstore.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
// diskChunkStore manages persisting and serving chunks from disk
type diskChunkStore struct {
basePath string
urlPrefix string
cr *recorder
}
func newDiskChunkStore(cr *recorder, basePath, urlPrefix string) *diskChunkStore {
return &diskChunkStore{
basePath: basePath,
urlPrefix: urlPrefix,
cr: cr,
}
}
// FetcherStore returns an individual storage for a given fetcher
func (d *diskChunkStore) FetcherStore(streamID string) (*stationChunkStore, error) {
od := filepath.Join(d.basePath, streamID)
if err := os.MkdirAll(od, 0755); err != nil {
return nil, fmt.Errorf("creating %s: %v", od, err)
}
return &stationChunkStore{
streamID: streamID,
path: od,
cr: d.cr,
}, nil
}
func (d *diskChunkStore) URLFor(streamID, chunkID string) string {
return d.urlPrefix + "/" + streamID + "/" + chunkID
}
func (d *diskChunkStore) ReaderFor(streamID, chunkID string) (io.ReadCloser, error) {
cfn := filepath.Join(d.basePath, streamID, chunkID)
f, err := os.Open(cfn)
if err != nil {
return nil, fmt.Errorf("opening %s: %v", cfn, err)
}
return f, nil
}
func (d *diskChunkStore) DeleteChunk(_ context.Context, streamID, chunkID string) error {
cfn := filepath.Join(d.basePath, streamID, chunkID)
if err := os.Remove(cfn); err != nil {
if os.IsNotExist(err) {
// already gone
return nil
}
return fmt.Errorf("deleting %s: %v", cfn, err)
}
return nil
}
func (d *diskChunkStore) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sp := strings.Split(r.URL.Path, "/")
if len(sp) != 2 {
http.Error(w, "Not Found", http.StatusNotFound)
}
// TODO - think about exploit opportunities here.
http.ServeFile(w, r, filepath.Join(d.basePath, sp[0], sp[1]))
}
type stationChunkStore struct {
path string
streamID string
cr *recorder
}
func (s *stationChunkStore) WriteChunk(ctx context.Context, chunkName string, chunkDuration float64, r io.Reader) error {
cfn := s.filename(chunkName)
f, err := os.Create(cfn)
if err != nil {
return fmt.Errorf("creating %s: %v", cfn, err)
}
if _, err := io.Copy(f, r); err != nil {
return fmt.Errorf("copying data to %s: %v", cfn, err)
}
if err := s.cr.RecordChunk(ctx, s.streamID, chunkName, chunkDuration, time.Now()); err != nil {
return fmt.Errorf("recording chunk: %v", err)
}
return nil
}
func (s *stationChunkStore) ChunkExists(_ context.Context, chunkName string) (bool, error) {
cfn := s.filename(chunkName)
if _, err := os.Stat(cfn); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("stat on %s: %v", cfn, err)
}
return true, nil
}
func (s *stationChunkStore) filename(chunkName string) string {
return filepath.Join(s.path, chunkName)
}