jx/internal/transport/transport.go

125 lines
2.1 KiB
Go
Raw Normal View History

2024-05-12 08:20:51 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package transport
import (
_ "errors"
2024-07-01 07:16:55 +00:00
_ "fmt"
2024-05-12 08:20:51 +00:00
"net/url"
2024-07-01 07:16:55 +00:00
_ "net/http"
2024-05-12 08:20:51 +00:00
_ "strings"
2024-07-01 07:16:55 +00:00
_ "path/filepath"
2024-05-12 08:20:51 +00:00
"io"
2024-07-01 07:16:55 +00:00
_ "os"
"context"
2024-05-12 08:20:51 +00:00
)
2024-07-01 07:16:55 +00:00
type Handler interface {
URI() *url.URL
ContentType() string
Gzip() bool
Signature() string
Reader() io.ReadCloser
Writer() io.WriteCloser
}
2024-05-12 08:20:51 +00:00
type Reader struct {
uri *url.URL
2024-07-01 07:16:55 +00:00
handle Handler
2024-05-12 08:20:51 +00:00
stream io.ReadCloser
}
2024-07-01 07:16:55 +00:00
func NewReader(u *url.URL) (reader *Reader, e error) {
ctx := context.Background()
reader = &Reader{ uri: u }
2024-05-12 08:20:51 +00:00
switch u.Scheme {
case "http", "https":
2024-07-01 07:16:55 +00:00
reader.handle, e = NewHTTP(u, ctx)
2024-05-12 08:20:51 +00:00
case "file":
fallthrough
default:
2024-07-01 07:16:55 +00:00
reader.handle, e = NewFile(u)
2024-05-12 08:20:51 +00:00
}
2024-07-01 07:16:55 +00:00
reader.stream = reader.handle.Reader()
return
2024-05-12 08:20:51 +00:00
}
func NewReaderURI(uri string) (reader *Reader, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewReader(u)
}
return
}
2024-05-12 08:20:51 +00:00
type Writer struct {
2024-07-01 07:16:55 +00:00
uri *url.URL
handle Handler
stream io.WriteCloser
2024-05-12 08:20:51 +00:00
}
2024-07-01 07:16:55 +00:00
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
2024-05-12 08:20:51 +00:00
}
func NewWriterURI(uri string) (writer *Writer, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewWriter(u)
}
return
}
2024-05-12 08:20:51 +00:00
func (r *Reader) Read(b []byte) (int, error) {
return r.stream.Read(b)
}
func (r *Reader) Close() error {
return r.stream.Close()
}
2024-07-01 07:16:55 +00:00
func (r *Reader) ContentType() string {
return r.handle.ContentType()
}
func (r *Reader) Gzip() bool {
return r.handle.Gzip()
}
2024-05-12 08:20:51 +00:00
func (r *Reader) Signature() string {
2024-07-01 07:16:55 +00:00
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()
2024-05-12 08:20:51 +00:00
}