Hi,

I am looking into using Drupal for a podcast. I see that you can upload a file to the Drupal server and have the first attachment be an enclosure in an RSS feed. However, our audio files are hosted on a different server and so I cannot use the upload feature to get an enclosure tag to come up. Is there any way to specify an attachment manually (that is to say, can I specify some path like "http://audio.somewhere.com/shows/01012007.mp3")?

Does anyone have any suggestions or advice, aside from writing my own module to take an enclosure value when editing a node? (I suppose I could look at the upload module to try to figure out how it pins an enclosure tag to the RSS feed...right? Of course, I'd really really rather not have to do this :-D).

Thanks for your help!

Armando

Comments

armandoleon’s picture

Well, I just wrote my own module to solve this problem. I suppose it would conflict with the upload module (since the upload module automatically makes the first file uploaded the RSS enclosure), so I just commented the RSS part out of the upload.module file. This comes with no warranty, but is a pretty simple module so it can't imagine there could be too much wrong with it (I've tested it, but not *extensively*).

If you have any questions, feel free to email me at armando (dot) leon (at) gmail (dot) com .

Since the hook_install function seems to not be working, you'll need to manually apply the following SQL to your database:

CREATE TABLE enclosure (
     nid int(10) unsigned NOT NULL default '0',
     enc_url varchar(255) NOT NULL default '',
     enc_length int(10) unsigned NOT NULL default '0',
     enc_type varchar(30) NOT NULL default '',
     PRIMARY KEY (nid)
)

Here is the enclosure.info file:

// $Id: enclosure.module,v 1.0 2007/04/13 20:48:54 Exp $
name = Enclosure
description = Allows users specify a custom RSS enclosure tag for nodes.
package = War News Radio Custom
version = VERSION

And here is the enclosure.module file:

<?php
// $Id: enclosure.module,v 1.0 2007/04/13 20:48:35 Exp $

/**
 * @file
 * Enables users to attach an enclosure tag to the RSS feed of a node.
 */

/**
 * Implementation of hook_help().
 */
function enclosure_help($section) {
  switch ($section) {
    case 'admin/help#enclosure':
      $output = '<p>'. t('The enclosure module allows users to specify an RSS enclosure tag for any node.  The tag will appear any time RSS is generated from a particular node.') .'</p>';
      $output .= '<p>'. t('Users with the add RSS enclosure permission can upload attachments. You can choose which post types can take RSS enclosure tags on the content types settings page.') .'</p>';
      $output .= '<p>'. t('For more information please contact Armando Leon, the author of this module, at <a href="@upload">armando(dot)leon(at)gmail(dot)com</a>.', array('@upload' => 'mailto:armando(dot)leon(at)gmail(dot)com')) .'</p>';
      return $output;
  }
}


/**
 * Implementation of hook_install().
 */
function enclosure_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {enclosure} (
                  nid int(10) unsigned NOT NULL default '0',
                  enc_url varchar(255) NOT NULL default '',
                  enc_length int(10) unsigned NOT NULL default '0',
                  enc_type varchar(30) NOT NULL default '',
                  PRIMARY KEY (nid)
                )");
      break;

    case 'pgsql':
      db_query("CREATE TABLE {enclosure} (
                  nid int NOT NULL default '0',
                  enc_url varchar(255) NOT NULL default '0',
                  enc_length int NOT NULL default '0',
                  enc_type varchar(30) NOT NULL default '',
                  PRIMARY KEY (nid)
                )");
      break;
  }
}

/**
 * Implementation of hook_uninstall().
 */
function enclosure_uninstall() {
  db_query('DROP TABLE {enclosure}');
}



/**
 * Implementation of hook_perm().
 */
function enclosure_perm() {
  return array('modify enclosure');
}


/**
 * Implementation of hook_form_alter().
 */
function enclosure_form_alter($form_id, &$form) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $enc_url = $form['#node']->enc_url;
    $enc_length = $form['#node']->enc_length;
    $enc_type = $form['#node']->enc_type;
    
    $form['enclosure'] = array(
      '#type' => 'fieldset',
      '#title' => t('RSS Enclosure'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($enc_url),
      '#access' => user_access('modify enclosure'),
      '#weight' => 35,
    );
    $form['enclosure']['enc_url'] = array(
      '#type' => 'textfield',
      '#default_value' => $enc_url,
      '#maxlength' => 250,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Optionally specify an RSS enclosure URL for this node.  This will only appear in an RSS feed.'),
    );
    $form['enclosure']['enc_length'] = array(
      '#type' => 'textfield',
      '#default_value' => $enc_length,
      '#maxlength' => 8,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Specify a file size.  When in doubt, enter "0".'),
    );
    $form['enclosure']['enc_type'] = array(
      '#type' => 'textfield',
      '#default_value' => $enc_type,
      '#maxlength' => 30,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Specify a file type.  When in doubt, enter "audio/mpeg".'),
    );
  }
}


/**
 * Implementation of hook_nodeapi().
 */
function enclosure_nodeapi(&$node, $op, $teaser) {
  if (user_access('modify enclosure')) {
    switch ($op) {
      case 'validate':
        $node->enc_url = trim($node->enc_url);
        $node->enc_length = trim($node->enc_length);
        $node->enc_type = trim($node->enc_type);
        if ($enc_url) {
          if (!is_numeric($node->enc_length) || ($node->enc_length < 0)) {
            form_set_error('enc_length', t('The enclosure length must be a number and greater than or equal to zero.'));
          }
          if (!$enc_type) {
            form_set_error('enc_type', t('The enclosure type must be set.'));
          }
        }
        break;
      
      case 'load':
        $result = db_query("SELECT * FROM {enclosure} WHERE nid = '%d'", $node->nid);
        if (db_num_rows($result)) {
          $data = db_fetch_object($result);
          $node->enc_url = $data->enc_url;
          $node->enc_length = $data->enc_length;
          $node->enc_type = $data->enc_type;
        }
        break;
      
      case 'insert':
        if ($node->enc_url) {
          enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
        }
        break;
    
      case 'update':
        if ($node->enc_url) {
          enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
        } else {
          enclosure_unset($node->nid);
        }
        break;

      case 'delete':
        enclosure_unset($node->nid);
        break;

      case 'rss item':
        if ($node->enc_url) {
            return array(
              array(
                'key' => 'enclosure',
                'attributes' => array(
                'url' => $node->enc_url,
                'length' => $node->enc_length,
                'type' => $node->enc_type
                )
              )
            );
         } else {
         return array();
         }
    }
  }
}

function enclosure_set($nid, $enc_url, $enc_length, $enc_type) {
  if (db_result(db_query("SELECT COUNT(nid) FROM {enclosure} WHERE nid = %d", $nid))) {
    db_query("UPDATE {enclosure} SET enc_url = '%s', enc_length = '%d', enc_type = '%s' WHERE nid = %d", $enc_url, $enc_length, $enc_type, $nid);
  } else {
    db_query("INSERT INTO {enclosure} (nid, enc_url, enc_length, enc_type) VALUES (%d, '%s', %d, '%s')", $nid, $enc_url, $enc_length, $enc_type);
  }
}

function enclosure_unset($nid) {
  if (db_result(db_query("SELECT COUNT(nid) FROM {enclosure} WHERE nid = %d", $nid))) {
    db_query("DELETE FROM {enclosure} WHERE nid = %d", $nid);
  }
}