mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-08 22:36:37 +08:00
split cloud text reader
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -249,42 +247,6 @@ func (s *StorageConfigService) CloudResolveUncached(ctx context.Context, typ, fi
|
|||||||
return p.Resolve(ctx, fileRef)
|
return p.Resolve(ctx, fileRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloudReadText resolves a small cloud file and returns its text payload. It is
|
|
||||||
// used for cloud-hosted .strm files: the scanner reads the STRM target once and
|
|
||||||
// stores the real playback URL, while the media bytes still stay in the cloud.
|
|
||||||
func (s *StorageConfigService) CloudReadText(ctx context.Context, typ, fileRef string, limit int64) (string, error) {
|
|
||||||
if limit <= 0 {
|
|
||||||
limit = 64 << 10
|
|
||||||
}
|
|
||||||
link, err := s.CloudResolve(ctx, typ, fileRef, "")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, link.URL, nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for k, v := range link.Headers {
|
|
||||||
req.Header.Set(k, v)
|
|
||||||
}
|
|
||||||
resp, err := s.client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
||||||
return "", fmt.Errorf("%s: read strm returned http %d", typ, resp.StatusCode)
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(io.LimitReader(resp.Body, limit+1))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if int64(len(body)) > limit {
|
|
||||||
return "", fmt.Errorf("%s: strm file is too large", typ)
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(strings.TrimPrefix(string(body), "\ufeff")), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// cloudProviderWithUA builds a provider, overriding the request UA when a
|
// cloudProviderWithUA builds a provider, overriding the request UA when a
|
||||||
// non-empty clientUA is supplied.
|
// non-empty clientUA is supplied.
|
||||||
func (s *StorageConfigService) cloudProviderWithUA(ctx context.Context, typ, clientUA string) (cloud.Provider, error) {
|
func (s *StorageConfigService) cloudProviderWithUA(ctx context.Context, typ, clientUA string) (cloud.Provider, error) {
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CloudReadText resolves a small cloud file and returns its text payload. It is
|
||||||
|
// used for cloud-hosted .strm files: the scanner reads the STRM target once and
|
||||||
|
// stores the real playback URL, while the media bytes still stay in the cloud.
|
||||||
|
func (s *StorageConfigService) CloudReadText(ctx context.Context, typ, fileRef string, limit int64) (string, error) {
|
||||||
|
if limit <= 0 {
|
||||||
|
limit = 64 << 10
|
||||||
|
}
|
||||||
|
link, err := s.CloudResolve(ctx, typ, fileRef, "")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, link.URL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for k, v := range link.Headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
resp, err := s.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return "", fmt.Errorf("%s: read strm returned http %d", typ, resp.StatusCode)
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(io.LimitReader(resp.Body, limit+1))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if int64(len(body)) > limit {
|
||||||
|
return "", fmt.Errorf("%s: strm file is too large", typ)
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(strings.TrimPrefix(string(body), "\ufeff")), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user