| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package googlereader // import "miniflux.app/v2/internal/googlereader"
- import (
- "errors"
- "fmt"
- "net/http"
- "strconv"
- "strings"
- )
- const (
- ItemIDPrefix = "tag:google.com,2005:reader/item/"
- ItemIDFormat = "tag:google.com,2005:reader/item/%016x"
- )
- func convertEntryIDToLongFormItemID(entryID int64) string {
- // The entry ID is a 64-bit integer, so we need to format it as a 16-character hexadecimal string.
- return fmt.Sprintf(ItemIDFormat, entryID)
- }
- // Expected format: "tag:google.com,2005:reader/item/00000000148b9369" (hexadecimal string with prefix and padding)
- // NetNewsWire uses this format: "tag:google.com,2005:reader/item/2f2" (hexadecimal string with prefix and no padding)
- // Reeder uses this format: "000000000000048c" (hexadecimal string without prefix and padding)
- // Liferea uses this format: "12345" (decimal string)
- // A 16-character ID without prefix is read as hexadecimal, since decimal IDs only reach 16 digits at 10^15.
- // It returns the parsed ID as a int64 and an error if parsing fails.
- func parseItemID(itemIDValue string) (int64, error) {
- value, base, format := itemIDValue, 10, "decimal"
- if hexID, found := strings.CutPrefix(itemIDValue, ItemIDPrefix); found {
- value, base, format = hexID, 16, "hexadecimal"
- } else if len(itemIDValue) == 16 {
- base, format = 16, "hexadecimal"
- }
- // ParseUint rejects signs, spaces and trailing characters, and a bit size of 63 keeps the ID within int64.
- itemID, err := strconv.ParseUint(value, base, 63)
- if err != nil {
- return 0, fmt.Errorf("failed to parse %s item ID %s: %w", format, itemIDValue, err)
- }
- if itemID == 0 {
- return 0, fmt.Errorf("failed to parse %s item ID %s: item ID must be positive", format, itemIDValue)
- }
- return int64(itemID), nil
- }
- func parseItemIDsFromRequest(r *http.Request) ([]int64, error) {
- items := r.Form[paramItemIDs]
- if len(items) == 0 {
- return nil, errors.New("googlereader: no items requested")
- }
- itemIDs := make([]int64, len(items))
- for i, item := range items {
- itemID, err := parseItemID(item)
- if err != nil {
- return nil, fmt.Errorf("googlereader: failed to parse item ID %s: %w", item, err)
- }
- itemIDs[i] = itemID
- }
- return itemIDs, nil
- }
|