Add enclosure element to RSS feed
authorJacob Casper <dev@jacobcasper.com>
Sun, 26 Jun 2022 20:31:06 +0000 (15:31 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sun, 26 Jun 2022 21:00:26 +0000 (16:00 -0500)
This should allow the client to download the full quality image when
fetching items if supported. The length attribute is _technically_ a lie
but it's B64 encoded so it should be greater than the actual bytes
transferred if anything, and considered a worst case.

rss.php

diff --git a/rss.php b/rss.php
index 0312255..c795637 100644 (file)
--- a/rss.php
+++ b/rss.php
@@ -4,7 +4,9 @@ require_once __DIR__ . '/db.php';
 
 function post_to_item(DOMDocument $dom, Array $post) {
       $mime_type = $post['MIME_TYPE'];
-      $post_id = $post['ID'];
+      $post_id = $post['POST_ID'];
+      $pic_id = $post['PIC_ID'];
+      $post_size = $post['SIZE'];
       $post_body = $post['BODY'];
       $timestamp = $post['TIMESTAMP'];
       $item = $dom->createElement('item');
@@ -12,13 +14,12 @@ function post_to_item(DOMDocument $dom, Array $post) {
       $link = $dom->createElement('link', "https://pics.jacobcasper.com/#post-{$post_id}");
       $guid = $dom->createElement('guid', "https://pics.jacobcasper.com/#post-{$post_id}");
       $pub_date = $dom->createElement('pubDate', date(DATE_RSS, $timestamp));
-      // TODO write the endpoint for this
-      //$enclosure = $dom->createElement('enclosure');
-      //$enclosure->setAttribute('url', "https://pics.jacobcasper.com/pic/{$post_id}");
-      //$enclosure->setAttribute('length', "9999");
-      //$enclosure->setAttribute('type', $mime_type);
+      $enclosure = $dom->createElement('enclosure');
+      $enclosure->setAttribute('url', "https://pics.jacobcasper.com/pic/{$pic_id}");
+      $enclosure->setAttribute('length', $post_size);
+      $enclosure->setAttribute('type', $mime_type);
       $item->appendChild($description);
-      //$item->appendChild($enclosure);
+      $item->appendChild($enclosure);
       $item->appendChild($link);
       $item->appendChild($guid);
       $item->appendChild($pub_date);
@@ -32,7 +33,12 @@ $rss_dom->loadXML($rss_template);
 $channel_node = $rss_dom->firstChild->childNodes[1];
 $db = get_db();
 $posts = $db->query('SELECT
-    p.ID, p.TIMESTAMP, pi.MIME_TYPE, c.BODY
+    p.ID as POST_ID,
+    pi.id as PIC_ID,
+    p.TIMESTAMP,
+    pi.MIME_TYPE,
+    length(pi.PIC_B64) as SIZE,
+    c.BODY
     FROM posts p
     JOIN pics pi on p.id = pi.post_id
     JOIN comments c on p.id = c.post_id