Browse Source

feat: asset upstream package

feature/assets
Dnomd343 10 months ago
parent
commit
904405119c
  1. 7
      assets/common.go
  2. 16
      assets/update.go
  3. 36
      assets/upstream/local.go
  4. 19
      assets/upstream/remote.go
  5. 2
      assets/upstream/remote_legacy.go
  6. 29
      assets/upstream/upstream.go

7
assets/common.go

@ -3,15 +3,8 @@ package assets
import (
"XProxy/logger"
"io"
"time"
)
type upstream interface {
getTag() string
doRequest() io.Reader
lastModify() time.Time
}
type asset struct {
tag string
stream io.Reader

16
assets/update.go

@ -44,17 +44,17 @@ func saveAsset(file string, content []byte, date *time.Time) error {
// save them locally. Local files will be overwritten if they exist.
func updateRemoteAsset(file string, url string, proxy *urlpkg.URL) error {
logger.Debugf("Start downloading remote asset `%s` to `%s`", url, file)
asset, date, err := downloadAsset(url, proxy)
if err != nil {
logger.Errorf("Failed to download remote asset `%s`", url)
return err
}
//asset, date, err := downloadAsset(url, proxy)
//if err != nil {
// logger.Errorf("Failed to download remote asset `%s`", url)
// return err
//}
//if asset, err = tryExtract(asset); err != nil {
// return err
//}
if err := saveAsset(file, asset, date); err != nil {
return err
}
//if err := saveAsset(file, asset, date); err != nil {
// return err
//}
logger.Infof("Successfully obtained remote asset `%s`", file)
return nil
}

36
assets/upstream/local.go

@ -0,0 +1,36 @@
package upstream
import (
"XProxy/logger"
"io"
"os"
"time"
)
type localAsset struct {
tag string
path string
}
func (l *localAsset) Tag() string {
return l.tag
}
func (l *localAsset) lastModify() time.Time {
stat, err := os.Stat(l.path)
if err != nil {
logger.Warnf("Failed to get local file stat -> %v", err)
return time.Now() // unknown modify time
}
return stat.ModTime() // using last modify time of src file
}
func (l *localAsset) Request() (io.ReadCloser, time.Time, error) {
logger.Debugf("Start extracting local asset `%s`", l.path)
stream, err := os.Open(l.path)
if err != nil {
logger.Errorf("Failed to read local asset -> %v", err)
return nil, l.lastModify(), err
}
return stream, l.lastModify(), nil
}

19
assets/upstream/remote.go

@ -0,0 +1,19 @@
package upstream
import (
"io"
"net/http"
"time"
)
type remoteAsset struct {
client http.Client
}
func (r *remoteAsset) GetTag() string {
return ""
}
func (r *remoteAsset) Request() (io.ReadCloser, time.Time, error) {
return nil, time.Now(), nil
}

2
assets/remote.go → assets/upstream/remote_legacy.go

@ -1,4 +1,4 @@
package assets
package upstream
import (
"XProxy/logger"

29
assets/upstream/upstream.go

@ -0,0 +1,29 @@
package upstream
import (
"io"
urlpkg "net/url"
"time"
)
// Upstream interface is an abstraction that supports initiating request and
// returning information such as data streams.
type Upstream interface {
// Tag function get the description of current upstream.
Tag() (tag string)
// Request function initiates a request, returns the data stream and last
// modification time, or an error information.
Request() (stream io.ReadCloser, lastModify time.Time, err error)
}
func NewLocalAsset(tag string, path string) Upstream {
return &localAsset{
tag: tag,
path: path,
}
}
func NewRemoteAsset(url string, proxy *urlpkg.URL) Upstream {
return &remoteAsset{}
}
Loading…
Cancel
Save