feed icon indicating copy to clipboard operation
feed copied to clipboard

MIME Type Detection Bug

Open marcojakob opened this issue 8 months ago • 0 comments

Problem

The formatEnclosure function in atom1.ts ignores the explicitly provided type parameter in Enclosure objects and always tries to auto-detect MIME types from URLs, leading to incorrect types like image// for proxy URLs.

Current Code

const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
  if (typeof enclosure === "string") {
    const type = new URL(enclosure).pathname.split(".").slice(-1)[0];
    return { _attributes: { rel: "enclosure", href: enclosure, type: `${mimeCategory}/${type}` } };
  }

  const type = new URL(enclosure.url).pathname.split(".").slice(-1)[0]; // BUG: ignores enclosure.type!
  return {
    _attributes: {
      rel: "enclosure",
      href: enclosure.url,
      title: enclosure.title,
      type: `${mimeCategory}/${type}`, // Should use enclosure.type if provided
      length: enclosure.length,
    },
  };
};

Issue

  1. When an Enclosure object provides an explicit type, it's completely ignored
  2. Auto-detection fails for proxy URLs like https://wsrv.nl/?url=... resulting in image//
  3. Users have no way to override incorrect auto-detection

Proposed Fix

const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
  if (typeof enclosure === "string") {
    const detectedType = new URL(enclosure).pathname.split(".").slice(-1)[0];
    return { _attributes: { rel: "enclosure", href: enclosure, type: `${mimeCategory}/${detectedType}` } };
  }

  // Use explicitly provided type, otherwise auto-detect
  let type = enclosure.type;
  if (!type) {
    const detectedType = new URL(enclosure.url).pathname.split(".").slice(-1)[0];
    type = `${mimeCategory}/${detectedType}`;
  }

  return {
    _attributes: {
      rel: "enclosure",
      href: enclosure.url,
      title: enclosure.title,
      type: type,
      length: enclosure.length,
    },
  };
};

Benefits

  1. Respects explicit types: When users provide type, it's used as-is
  2. Backward compatible: Auto-detection still works for string URLs and Enclosure objects without type
  3. Fixes proxy URL issues: Users can specify correct MIME types for any URL
  4. Simple and focused: No complex URL parsing logic needed

Example Usage

// Before: Always resulted in image// for proxy URLs
feed.addItem({
  image: "https://wsrv.nl/?url=https%3A//example.com/image.jpg&w=1000"
});

// After: Users can specify the correct type
feed.addItem({
  image: {
    url: "https://wsrv.nl/?url=https%3A//example.com/image.jpg&w=1000",
    type: "image/jpeg"
  }
});

marcojakob avatar Aug 24 '25 16:08 marcojakob