item.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. )
  11. const (
  12. ItemIDPrefix = "tag:google.com,2005:reader/item/"
  13. ItemIDFormat = "tag:google.com,2005:reader/item/%016x"
  14. )
  15. func convertEntryIDToLongFormItemID(entryID int64) string {
  16. // The entry ID is a 64-bit integer, so we need to format it as a 16-character hexadecimal string.
  17. return fmt.Sprintf(ItemIDFormat, entryID)
  18. }
  19. // Expected format: "tag:google.com,2005:reader/item/00000000148b9369" (hexadecimal string with prefix and padding)
  20. // NetNewsWire uses this format: "tag:google.com,2005:reader/item/2f2" (hexadecimal string with prefix and no padding)
  21. // Reeder uses this format: "000000000000048c" (hexadecimal string without prefix and padding)
  22. // Liferea uses this format: "12345" (decimal string)
  23. // A 16-character ID without prefix is read as hexadecimal, since decimal IDs only reach 16 digits at 10^15.
  24. // It returns the parsed ID as a int64 and an error if parsing fails.
  25. func parseItemID(itemIDValue string) (int64, error) {
  26. value, base, format := itemIDValue, 10, "decimal"
  27. if hexID, found := strings.CutPrefix(itemIDValue, ItemIDPrefix); found {
  28. value, base, format = hexID, 16, "hexadecimal"
  29. } else if len(itemIDValue) == 16 {
  30. base, format = 16, "hexadecimal"
  31. }
  32. // ParseUint rejects signs, spaces and trailing characters, and a bit size of 63 keeps the ID within int64.
  33. itemID, err := strconv.ParseUint(value, base, 63)
  34. if err != nil {
  35. return 0, fmt.Errorf("failed to parse %s item ID %s: %w", format, itemIDValue, err)
  36. }
  37. if itemID == 0 {
  38. return 0, fmt.Errorf("failed to parse %s item ID %s: item ID must be positive", format, itemIDValue)
  39. }
  40. return int64(itemID), nil
  41. }
  42. func parseItemIDsFromRequest(r *http.Request) ([]int64, error) {
  43. items := r.Form[paramItemIDs]
  44. if len(items) == 0 {
  45. return nil, errors.New("googlereader: no items requested")
  46. }
  47. itemIDs := make([]int64, len(items))
  48. for i, item := range items {
  49. itemID, err := parseItemID(item)
  50. if err != nil {
  51. return nil, fmt.Errorf("googlereader: failed to parse item ID %s: %w", item, err)
  52. }
  53. itemIDs[i] = itemID
  54. }
  55. return itemIDs, nil
  56. }