split cloud text reader

This commit is contained in:
ShukeBta
2026-06-27 12:28:01 +08:00
parent 6d5a141096
commit 9b48c6d57f
2 changed files with 45 additions and 38 deletions
-38
View File
@@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
@@ -249,42 +247,6 @@ func (s *StorageConfigService) CloudResolveUncached(ctx context.Context, typ, fi
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
// non-empty clientUA is supplied.
func (s *StorageConfigService) cloudProviderWithUA(ctx context.Context, typ, clientUA string) (cloud.Provider, error) {
+45
View File
@@ -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
}