109 lines
1.8 KiB
Go
109 lines
1.8 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package transport
|
|
|
|
import (
|
|
_ "errors"
|
|
_ "fmt"
|
|
"net/url"
|
|
_ "net/http"
|
|
_ "strings"
|
|
_ "path/filepath"
|
|
"io"
|
|
_ "os"
|
|
"context"
|
|
)
|
|
|
|
type Handler interface {
|
|
URI() *url.URL
|
|
ContentType() string
|
|
Gzip() bool
|
|
Signature() string
|
|
Reader() io.ReadCloser
|
|
Writer() io.WriteCloser
|
|
}
|
|
|
|
type Reader struct {
|
|
uri *url.URL
|
|
handle Handler
|
|
stream io.ReadCloser
|
|
}
|
|
|
|
func NewReader(u *url.URL) (reader *Reader, e error) {
|
|
ctx := context.Background()
|
|
reader = &Reader{ uri: u }
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
reader.handle, e = NewHTTP(u, ctx)
|
|
case "file":
|
|
fallthrough
|
|
default:
|
|
reader.handle, e = NewFile(u)
|
|
}
|
|
reader.stream = reader.handle.Reader()
|
|
return
|
|
}
|
|
|
|
type Writer struct {
|
|
uri *url.URL
|
|
handle Handler
|
|
stream io.WriteCloser
|
|
}
|
|
|
|
func NewWriter(u *url.URL) (writer *Writer, e error) {
|
|
ctx := context.Background()
|
|
writer = &Writer{ uri: u }
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
writer.handle, e = NewHTTP(u, ctx)
|
|
case "file":
|
|
fallthrough
|
|
default:
|
|
writer.handle, e = NewFile(u)
|
|
}
|
|
writer.stream = writer.handle.Writer()
|
|
return writer, e
|
|
}
|
|
|
|
func (r *Reader) Read(b []byte) (int, error) {
|
|
return r.stream.Read(b)
|
|
}
|
|
|
|
func (r *Reader) Close() error {
|
|
return r.stream.Close()
|
|
}
|
|
|
|
func (r *Reader) ContentType() string {
|
|
return r.handle.ContentType()
|
|
}
|
|
|
|
func (r *Reader) Gzip() bool {
|
|
return r.handle.Gzip()
|
|
}
|
|
|
|
func (r *Reader) Signature() string {
|
|
return r.handle.Signature()
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) Write(b []byte) (int, error) {
|
|
return w.stream.Write(b)
|
|
}
|
|
|
|
func (w *Writer) Close() error {
|
|
return w.stream.Close()
|
|
}
|
|
|
|
func (w *Writer) ContentType() string {
|
|
return w.handle.ContentType()
|
|
}
|
|
|
|
func (w *Writer) Gzip() bool {
|
|
return w.handle.Gzip()
|
|
}
|
|
|
|
func (w *Writer) Signature() string {
|
|
return w.handle.Signature()
|
|
}
|