jx/internal/transport/transport.go

158 lines
2.7 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-17 08:19:55 +00:00
exists func() bool
2024-05-12 08:20:51 +00:00
}
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-07-17 08:19:55 +00:00
reader.exists = func() bool { return FileExists(u) }
2024-05-12 08:20:51 +00:00
}
2024-07-17 08:19:55 +00:00
reader.SetStream(reader.handle.Reader())
2024-07-01 07:16:55 +00:00
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-07-17 08:19:55 +00:00
exists func() bool
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)
2024-07-17 08:19:55 +00:00
writer.exists = func() bool { return FileExists(u) }
2024-07-01 07:16:55 +00:00
}
2024-07-17 08:19:55 +00:00
writer.SetStream(writer.handle.Writer())
2024-07-01 07:16:55 +00:00
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-07-17 08:19:55 +00:00
func ExistsURI(uri string) bool {
if u, e := url.Parse(uri); e == nil {
return Exists(u)
}
return false
2024-07-17 08:19:55 +00:00
}
func Exists(u *url.URL) bool {
switch u.Scheme {
case "http", "https":
return HTTPExists(u)
case "file":
fallthrough
default:
return FileExists(u)
}
}
func (r *Reader) Exists() bool { return r.exists() }
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()
}
2024-07-17 08:19:55 +00:00
func (r *Reader) SetStream(s io.ReadCloser) {
r.stream = s
}
2024-07-01 07:16:55 +00:00
2024-07-17 08:19:55 +00:00
func (w *Writer) Exists() bool { return w.exists() }
2024-07-01 07:16:55 +00:00
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
}
2024-07-17 08:19:55 +00:00
func (w *Writer) SetStream(s io.WriteCloser) {
w.stream = s
}