2018-04-28 16:51:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type storage interface {
|
|
|
|
// WriteFile takes the content of a file and writes it into the underlying
|
|
|
|
// storage system.
|
|
|
|
WriteFile(filepath string, content io.Reader, contentType string) error
|
|
|
|
// ReadFile retrieves a file from the underlying storage, needs to return
|
|
|
|
// errFileNotFound when file is not present.
|
|
|
|
ReadFile(filepath string) (io.ReadCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errFileNotFound = errors.New("File not found")
|
|
|
|
|
|
|
|
func getStorageByURI(uri string) (storage, error) {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Scheme {
|
|
|
|
case "file":
|
|
|
|
return fileStorage{path: strings.TrimPrefix(uri, "file://")}, nil
|
|
|
|
|
|
|
|
case "log":
|
|
|
|
// NoOp storage: Only logs requests
|
|
|
|
return logStorage{}, nil
|
|
|
|
|
2018-04-28 18:22:51 +00:00
|
|
|
case "s3":
|
|
|
|
return s3StorageFromURI(uri), nil
|
|
|
|
|
2018-04-28 16:51:55 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Storage scheme %q not defined", u.Scheme)
|
|
|
|
}
|
|
|
|
}
|