mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
94 lines
1.3 KiB
Go
94 lines
1.3 KiB
Go
package fs
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/datarhei/core/v16/cluster"
|
|
"github.com/datarhei/core/v16/io/fs"
|
|
)
|
|
|
|
type Filesystem interface {
|
|
fs.Filesystem
|
|
}
|
|
|
|
type filesystem struct {
|
|
fs.Filesystem
|
|
|
|
cluster cluster.Cluster
|
|
}
|
|
|
|
func NewClusterFS(what string, fs fs.Filesystem, cluster cluster.Cluster) Filesystem {
|
|
f := &filesystem{
|
|
Filesystem: fs,
|
|
cluster: cluster,
|
|
}
|
|
|
|
return f
|
|
}
|
|
|
|
func (fs *filesystem) Open(path string) fs.File {
|
|
// Check if the file is locally available
|
|
if file := fs.Filesystem.Open(path); file != nil {
|
|
return file
|
|
}
|
|
|
|
// Check if the file is available in the cluster
|
|
url, err := fs.cluster.GetFile(path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
client := &http.Client{
|
|
Timeout: 15 * time.Second,
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
file := &file{
|
|
ReadCloser: resp.Body,
|
|
name: path,
|
|
}
|
|
|
|
return file
|
|
}
|
|
|
|
type file struct {
|
|
io.ReadCloser
|
|
|
|
name string
|
|
}
|
|
|
|
func (f *file) Name() string {
|
|
return f.name
|
|
}
|
|
|
|
func (f *file) Stat() (fs.FileInfo, error) {
|
|
return f, nil
|
|
}
|
|
|
|
func (f *file) Size() int64 {
|
|
return 0
|
|
}
|
|
|
|
func (f *file) ModTime() time.Time {
|
|
return time.Now()
|
|
}
|
|
|
|
func (f *file) IsLink() (string, bool) {
|
|
return "", false
|
|
}
|
|
|
|
func (f *file) IsDir() bool {
|
|
return false
|
|
}
|