jx/internal/transport/transport.go
Matthew Rich 2c9e178554
Some checks failed
Lint / golangci-lint (push) Successful in 9m40s
Declarative Tests / test (push) Failing after 15s
Declarative Tests / build-fedora (push) Successful in 1m58s
Declarative Tests / build-ubuntu-focal (push) Successful in 1m15s
add FileExists
2024-07-17 01:19:55 -07:00

157 lines
2.7 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
exists func() bool
}
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.exists = func() bool { return FileExists(u) }
}
reader.SetStream(reader.handle.Reader())
return
}
func NewReaderURI(uri string) (reader *Reader, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewReader(u)
}
return
}
type Writer struct {
uri *url.URL
handle Handler
stream io.WriteCloser
exists func() bool
}
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.exists = func() bool { return FileExists(u) }
}
writer.SetStream(writer.handle.Writer())
return writer, e
}
func NewWriterURI(uri string) (writer *Writer, e error) {
var u *url.URL
if u, e = url.Parse(uri); e == nil {
return NewWriter(u)
}
return
}
func ExistsURI(uri string) bool {
var u *url.URL
u, _ = url.Parse(uri)
return Exists(u)
}
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() }
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 (r *Reader) SetStream(s io.ReadCloser) {
r.stream = s
}
func (w *Writer) Exists() bool { return w.exists() }
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()
}
func (w *Writer) SetStream(s io.WriteCloser) {
w.stream = s
}