From 9b48c6d57f9f1265f71b3b26d0d1ee9cdd6cc6b3 Mon Sep 17 00:00:00 2001 From: ShukeBta <272197458+ShukeBta@users.noreply.github.com> Date: Sat, 27 Jun 2026 12:28:01 +0800 Subject: [PATCH] split cloud text reader --- internal/service/storage_cloud_resolve.go | 38 ------------------- internal/service/storage_cloud_text.go | 45 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 internal/service/storage_cloud_text.go diff --git a/internal/service/storage_cloud_resolve.go b/internal/service/storage_cloud_resolve.go index 942e363..b816834 100644 --- a/internal/service/storage_cloud_resolve.go +++ b/internal/service/storage_cloud_resolve.go @@ -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) { diff --git a/internal/service/storage_cloud_text.go b/internal/service/storage_cloud_text.go new file mode 100644 index 0000000..60d1060 --- /dev/null +++ b/internal/service/storage_cloud_text.go @@ -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 +}