/
url_to_stream.go
49 lines (43 loc) · 1.44 KB
/
url_to_stream.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
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package stream provides support to convert a URL into an io.ReadSeeker.
//
// Streamers should be registered via RegisterStreamer() in order
// to use them with URLToStream()
package stream
import (
"fmt"
"io"
)
var streamers []Streamer
// Streamer is the interface implemented by an object that can create an io.ReadSeeker from a remote or local URL
type Streamer interface {
// Returns true if this streamer can create a stream from the url
CanStream(url string) bool
// Opens a stream to the contents of url
// Returns:
// io.ReadSeeker - data at url
// Length of the stream in bytes
// checksum of the file pointed to by path
// error if any
CreateStream(url string) (io.ReadSeeker, int64, string, error)
}
// URLToStream converts a URL into an io.ReadSeeker
// returns:
// the io.ReadSeeker
// the length of the stream in bytes
// checksum of the file pointed to by path
// error if any
func URLToStream(url string) (io.ReadSeeker, int64, string, error) {
for i := 0; i < len(streamers); i++ {
if streamers[i].CanStream(url) {
return streamers[i].CreateStream(url)
}
}
return nil, 0, "", fmt.Errorf("no supported Streamer was found for %s", url)
}
// RegisterStreamer adds the Streamer to the
// list of Streamer handlers used by URLToStream method
func RegisterStreamer(s Streamer) {
streamers = append(streamers, s)
}