Skip to content

MIME Type Detection Bug #224

@marcojakob

Description

@marcojakob

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"
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions