From a8c4114d6d2d65d142b4e2b24c22fae7f2934732 Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Sun, 13 Apr 2025 17:40:38 +0200 Subject: [PATCH] feat(lists): read Plaintext from file on disk (#2031) * feat(lists): read Plaintext from file on disk * feat(lists): add tooltip to plaintext url field --- internal/list/process_list_plaintext.go | 90 +++++++++++++++++-------- web/src/forms/settings/ListForms.tsx | 11 +++ 2 files changed, 73 insertions(+), 28 deletions(-) diff --git a/internal/list/process_list_plaintext.go b/internal/list/process_list_plaintext.go index 032f656..70b9446 100644 --- a/internal/list/process_list_plaintext.go +++ b/internal/list/process_list_plaintext.go @@ -7,6 +7,9 @@ import ( "context" "io" "net/http" + "net/url" + "os" + "runtime" "strings" "github.com/autobrr/autobrr/internal/domain" @@ -23,33 +26,69 @@ func (s *service) plaintext(ctx context.Context, list *domain.List) error { l.Debug().Msgf("fetching titles from %s", list.URL) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, list.URL, nil) + // Parse the URL to determine if it's a file or HTTP scheme + parsedURL, err := url.Parse(list.URL) if err != nil { - return errors.Wrapf(err, "could not make new request for URL: %s", list.URL) + return errors.Wrapf(err, "failed to parse URL: %s", list.URL) } - list.SetRequestHeaders(req) + var body []byte - //setUserAgent(req) + // Handle different URL schemes + switch parsedURL.Scheme { + case "file": + // Read from filesystem for file:// URLs + filePath := parsedURL.Path - resp, err := s.httpClient.Do(req) - if err != nil { - return errors.Wrapf(err, "failed to fetch titles from URL: %s", list.URL) - } - defer resp.Body.Close() + if runtime.GOOS == "windows" { + // On Windows, remove leading slash from path if needed + if len(filePath) > 0 && filePath[0] == '/' && len(parsedURL.Host) > 0 { + filePath = parsedURL.Host + filePath + } else if len(filePath) > 0 && filePath[0] == '/' { + filePath = filePath[1:] + } + } - if resp.StatusCode != http.StatusOK { - return errors.Wrapf(err, "failed to fetch titles from URL: %s", list.URL) - } + l.Debug().Msgf("reading from file: %s", filePath) - contentType := resp.Header.Get("Content-Type") - if !strings.HasPrefix(contentType, "text/plain") { - return errors.Wrapf(err, "unexpected content type for URL: %s expected text/plain got %s", list.URL, contentType) - } + body, err = os.ReadFile(filePath) + if err != nil { + return errors.Wrapf(err, "failed to read file: %s", filePath) + } - body, err := io.ReadAll(resp.Body) - if err != nil { - return errors.Wrapf(err, "failed to read response body from URL: %s", list.URL) + case "http", "https": + // Use HTTP client for http:// or https:// URLs + req, err := http.NewRequestWithContext(ctx, http.MethodGet, list.URL, nil) + if err != nil { + return errors.Wrapf(err, "could not make new request for URL: %s", list.URL) + } + + list.SetRequestHeaders(req) + + //setUserAgent(req) + + resp, err := s.httpClient.Do(req) + if err != nil { + return errors.Wrapf(err, "failed to fetch titles from URL: %s", list.URL) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return errors.Wrapf(err, "failed to fetch titles from URL: %s with status code: %d", list.URL, resp.StatusCode) + } + + contentType := resp.Header.Get("Content-Type") + if !strings.HasPrefix(contentType, "text/plain") { + return errors.Errorf("unexpected content type for URL: %s expected text/plain got %s", list.URL, contentType) + } + + body, err = io.ReadAll(resp.Body) + if err != nil { + return errors.Wrapf(err, "failed to read response body from URL: %s", list.URL) + } + + default: + return errors.Errorf("unsupported URL scheme: %s", parsedURL.Scheme) } var titles []string @@ -59,22 +98,17 @@ func (s *service) plaintext(ctx context.Context, list *domain.List) error { if title == "" { continue } - titles = append(titles, title) + titles = append(titles, processTitle(title, list.MatchRelease)...) } - filterTitles := []string{} - for _, title := range titles { - filterTitles = append(filterTitles, processTitle(title, list.MatchRelease)...) - } - - if len(filterTitles) == 0 { + if len(titles) == 0 { l.Debug().Msgf("no titles found to update for list: %v", list.Name) return nil } - joinedTitles := strings.Join(filterTitles, ",") + joinedTitles := strings.Join(titles, ",") - l.Trace().Str("titles", joinedTitles).Msgf("found %d titles", len(joinedTitles)) + l.Trace().Str("titles", joinedTitles).Msgf("found %d titles", len(titles)) filterUpdate := domain.FilterUpdate{Shows: &joinedTitles} diff --git a/web/src/forms/settings/ListForms.tsx b/web/src/forms/settings/ListForms.tsx index ab02563..eca3cf0 100644 --- a/web/src/forms/settings/ListForms.tsx +++ b/web/src/forms/settings/ListForms.tsx @@ -63,6 +63,7 @@ import { DocsTooltip } from "@components/tooltips/DocsTooltip"; import { MultiSelect as RMSC } from "react-multi-select-component"; import { useToggle } from "@hooks/hooks.ts"; import { DeleteModal } from "@components/modals"; +import {DocsLink} from "@components/ExternalLink.tsx"; interface ListAddFormValues { name: string; @@ -729,6 +730,16 @@ function ListTypePlainText() { label="List URL" help="URL to a plain text file with one item per line" placeholder="https://example.com/list.txt" + tooltip={ +
+

Plaintext list can read from both http urls and local files on disk.

+
+

Remote: https://service.com/file.txt

+
+

Local: file:///home/username/file.txt

+ +
+ } />