Index: audio_images.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/audio_images.module,v
retrieving revision 1.18
diff -u -r1.18 audio_images.module
--- audio_images.module	20 Jul 2007 21:45:43 -0000	1.18
+++ audio_images.module	20 Jul 2007 22:06:19 -0000
@@ -18,6 +18,26 @@
 }
 
 /**
+ * Implementation of hook_file_download().
+ */
+function audio_images_file_download($filepath) {
+  $filepath = file_create_path($filepath);
+  $result = db_query("SELECT f.* FROM audio_image ai INNER JOIN files f ON ai.fid = f.fid WHERE f.filepath = '%s'", $filepath);
+  if ($file = db_fetch_object($result)) {
+    $node = node_load($file->nid);
+    if (node_access('view', $node)) {
+      return array(
+        'Content-Type: '. mime_header_encode($file->filemime),
+        'Content-Length: '. $file->filesize,
+      );
+    }
+    else {
+      return -1;
+    }
+  }
+}
+
+/**
  * Implementation of hook_form_alter() so we can add our image fields to the 
  * audio node form.
  */
@@ -36,15 +56,15 @@
 
     if ($node->audio_images) {
       $form['audio_images']['#theme'] = 'audio_images_form';
-      foreach ((array)$node->audio_images as $pid => $image) {
-        $form['audio_images'][$pid]['pid'] = array('#type' => 'value', '#value' => $pid);
-        $form['audio_images'][$pid]['pictype'] = array('#type' => 'value', '#value' => $image['pictype']);
-        $form['audio_images'][$pid]['filepath'] = array('#type' => 'value', '#value' => $image['filepath']);
-        $form['audio_images'][$pid]['filemime'] = array('#type' => 'value', '#value' => $image['filemime']);
-        $form['audio_images'][$pid]['filesize'] = array('#type' => 'value', '#value' => $image['filesize']);
-        $form['audio_images'][$pid]['height'] = array('#type' => 'value', '#value' => $image['height']);
-        $form['audio_images'][$pid]['width'] = array('#type' => 'value', '#value' => $image['width']);
-        $form['audio_images'][$pid]['delete'] = array('#type' => 'checkbox', '#default_value' => $image['delete']);
+      foreach ((array)$node->audio_images as $fid => $image) {
+        $form['audio_images'][$fid]['fid'] = array('#type' => 'value', '#value' => $fid);
+        $form['audio_images'][$fid]['pictype'] = array('#type' => 'value', '#value' => $image['pictype']);
+        $form['audio_images'][$fid]['filepath'] = array('#type' => 'value', '#value' => $image['filepath']);
+        $form['audio_images'][$fid]['filemime'] = array('#type' => 'value', '#value' => $image['filemime']);
+        $form['audio_images'][$fid]['filesize'] = array('#type' => 'value', '#value' => $image['filesize']);
+        $form['audio_images'][$fid]['height'] = array('#type' => 'value', '#value' => $image['height']);
+        $form['audio_images'][$fid]['width'] = array('#type' => 'value', '#value' => $image['width']);
+        $form['audio_images'][$fid]['delete'] = array('#type' => 'checkbox', '#default_value' => $image['delete']);
       }
     }
 
@@ -74,14 +94,14 @@
 function theme_audio_images_form(&$form) {
   $pictypes = audio_image_type_dirty_array();
   $rows = array();
-  foreach (element_children($form) as $pid) {
-    if ($pid != 'new') {
+  foreach (element_children($form) as $fid) {
+    if ($fid != 'new') {
       $rows[] = array(
-        l($pictypes[$form[$pid]['pictype']['#value']], $form[$pid]['filepath']['#value']),
-        $form[$pid]['filemime']['#value'],
-        $form[$pid]['height']['#value'] .'x'. $form[$pid]['width']['#value'],
-        $form[$pid]['filesize']['#value'],
-        drupal_render($form[$pid]['delete']),
+        l($pictypes[$form[$fid]['pictype']['#value']], $form[$fid]['filepath']['#value']),
+        $form[$fid]['filemime']['#value'],
+        $form[$fid]['height']['#value'] .'x'. $form[$fid]['width']['#value'],
+        $form[$fid]['filesize']['#value'],
+        drupal_render($form[$fid]['delete']),
       );
     }
   }  
@@ -96,9 +116,9 @@
   switch ($op) {
     case 'load':
       $ret['audio_images'] = array();
-      $result = db_query("SELECT pid, pictype, filemime, width, height, filepath, filesize FROM {audio_image} WHERE vid=%d", $node->vid);
+      $result = db_query("SELECT ai.fid, ai.pictype, f.filemime, f.filepath, f.filesize, ai.width, ai.height FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE vid=%d", $node->vid);
       while ($img = db_fetch_array($result)) {
-        $ret['audio_images'][$img['pid']] = $img;
+        $ret['audio_images'][$img['fid']] = $img;
       }
       return $ret;
 
@@ -117,12 +137,12 @@
         $imagepath = _audio_image_filename(basename($node->audio_file->filename), $upload->filemime, $pictype, TRUE);
         if ($file = file_save_upload($upload, $imagepath, FILE_EXISTS_REPLACE)) {
           if ($image = audio_image_from_file($file->filepath, $pictype)) {
-            $pid = 'new_'. count($_SESSION['audio_images']);
-            $image['pid'] = $pid;
-            $node->audio_images[$pid] = $image;
+            $fid = 'new_'. count($_SESSION['audio_images']);
+            $image['fid'] = $fid;
+            $node->audio_images[$fid] = $image;
 
             // Store the image in the session.
-            $_SESSION['audio_images'][$pid] = $image;
+            $_SESSION['audio_images'][$fid] = $image;
           }
           else {
             form_set_error('audio_image_upload', t('There was a problem saving the image. It may not have been a valid image file.'));
@@ -137,10 +157,10 @@
 
       // Check for deletions of temporaries.
       foreach ((array)$node->audio_images as $image) {
-        $pid = $image['pid'];
-        if (_audio_images_istemp($pid) && $_POST['audio_images'][$pid]['delete']) {
+        $fid = $image['fid'];
+        if (_audio_images_istemp($fid) && $_POST['audio_images'][$fid]['delete']) {
           file_delete($image['filepath']);
-          unset($node->audio_images[$pid]);
+          unset($node->audio_images[$fid]);
         }
       }
 
@@ -150,21 +170,21 @@
 
     case 'insert':
       // Add new images.
-      foreach ((array)$node->audio_images as $pid => $image) {
-        if (_audio_images_istemp($image['pid'])) {
+      foreach ((array)$node->audio_images as $fid => $image) {
+        if (_audio_images_istemp($image['fid'])) {
           _audio_images_save_new($node, $image);
         }
       }
       break;
 
     case 'insert revision':
-      foreach ((array)$node->audio_images as $pid => $image) {
+      foreach ((array)$node->audio_images as $fid => $image) {
         // Deletions.
         if ($image['delete']) {
-          _audio_images_delete($pid, $image['filepath']);
+          _audio_images_delete($fid, $image['filepath']);
         }
         // Additions.
-        else if (_audio_images_istemp($image['pid'])) {
+        else if (_audio_images_istemp($image['fid'])) {
           _audio_images_save_new($node, $image);
         }
         // Make copies of unchanged images.
@@ -175,38 +195,39 @@
       break;
 
     case 'update':
-      foreach ((array)$node->audio_images as $pid => $image) {
+      foreach ((array)$node->audio_images as $fid => $image) {
         // Deletions.
         if ($image['delete']) {
-          _audio_images_delete($pid, $image['filepath']);
+          _audio_images_delete($fid, $image['filepath']);
         }
         // Additions.
-        else if (_audio_images_istemp($image['pid'])) {
+        else if (_audio_images_istemp($image['fid'])) {
           _audio_images_save_new($node, $image);
         }
       }
       break;
 
     case 'delete':
-      // Delete any associated previews.
       _audio_images_delete_previews($node);
 
-      // Delete the image files and remove them from the database.
-      $result = db_query('SELECT filepath FROM {audio_image} WHERE nid = %d', $node->nid);
+      $result = db_query('SELECT f.fid, f.filepath FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE ai.nid = %d', $node->nid);
       while ($file = db_fetch_object($result)) {
         file_delete($file->filepath);
+        db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
       }
       db_query('DELETE FROM {audio_image} WHERE nid = %d', $node->nid);
       break;
 
     case 'delete revision':
-      // Delete any associated previews.
       _audio_images_delete_previews($node);
 
-      // Delete the image files and remove them from the database.
-      $result = db_query('SELECT filepath FROM {audio_image} WHERE vid = %d', $node->vid);
+      $result = db_query('SELECT f.fid, f.filepath FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE ai.vid = %d', $node->vid);
       while ($file = db_fetch_object($result)) {
         file_delete($file->filepath);
+        // This is a little less efficient than simply doing a DELETE FROM 
+        // {files} WHERE nid = $node->nid but it's the correct way to do it so 
+        // we don't delete other modules' files. 
+        db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
       }
       db_query('DELETE FROM {audio_image} WHERE vid = %d', $node->vid);
       break;
@@ -216,8 +237,8 @@
 /**
  * Is an image a temporary preview?
  */
-function _audio_images_istemp($pid) {
-  return (strpos($pid, 'new') !== FALSE);
+function _audio_images_istemp($fid) {
+  return (strpos($fid, 'new') !== FALSE);
 }
 
 /**
@@ -238,23 +259,24 @@
 function _audio_images_save_copy(&$node, $image) {
   $newpath = _audio_image_filename($node->vid, $image['filemime'], $image['pictype'], FALSE);
   if (file_copy($image['filepath'], $newpath, FILE_EXISTS_REPLACE)) {
-    $pid = db_next_id('{audio_image}_pid');
-    db_query("INSERT INTO {audio_image} (pid, nid, vid, pictype, filemime, width, height, filepath, filesize)
-      VALUES (%d, %d, %d, %d, '%s', %d, %d, '%s', %d)",
-      $pid, $node->nid, $node->vid, $image['pictype'], $image['filemime'],
-      $image['width'], $image['height'], $newpath, filesize($newpath));
-
+    $image['fid'] = db_next_id('{files}_fid');
     $image['filepath'] = $newpath;
-    $image['pid'] = $pid;
-    $node->audio_images[$pid] = $image;
+
+    db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')",
+      $image['fid'], $node->nid, basename($image['filepath']), $image['filepath'], $image['filemime'], filesize($image['filepath']));
+    db_query("INSERT INTO {audio_image} (fid, nid, vid, pictype, width, height) VALUES (%d, %d, %d, %d, %d, %d)", 
+      $image['fid'], $node->nid, $node->vid, $image['pictype'], $image['width'], $image['height']);
+
+    $node->audio_images[$fid] = $image;
   }
 }
 
-function _audio_images_delete($pid, $filepath) {
+function _audio_images_delete($fid, $filepath) {
   file_delete($filepath);
   // Delete from the database if it's not a preview.
-  if (!_audio_images_istemp($pid)) {
-    db_query("DELETE FROM {audio_image} WHERE pid = %d", $pid);
+  if (!_audio_images_istemp($fid)) {
+    db_query("DELETE FROM {files} WHERE fid = %d", $fid);
+    db_query("DELETE FROM {audio_image} WHERE fid = %d", $fid);
   }
 }
 
@@ -262,10 +284,10 @@
  * Delete any preview images associated with the node.
  */ 
 function _audio_images_delete_previews(&$node) {
-  foreach ((array)$node->audio_images as $pid => $image) {
-    if (_audio_images_istemp($pid)) {
+  foreach ((array)$node->audio_images as $fid => $image) {
+    if (_audio_images_istemp($fid)) {
       file_delete($image['filepath']);
-      unset($node->audio_images[$pid]);
+      unset($node->audio_images[$fid]);
     }
   }
 }
Index: audio_images.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/audio_images.install,v
retrieving revision 1.6
diff -u -r1.6 audio_images.install
--- audio_images.install	7 Mar 2007 23:52:01 -0000	1.6
+++ audio_images.install	20 Jul 2007 21:51:21 -0000
@@ -86,4 +86,41 @@
       break;
   }
   return $ret;
-}
\ No newline at end of file
+}
+
+/**
+ * Refactor the way that files are stored in the database
+ *
+ * This is the first update for the 5.3 branch, using the update naming 
+ * convention described in: http://drupal.org/node/114774#update-n
+ */
+function audio_images_update_5300() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {audio_image}
+        ADD COLUMN `fid` INTEGER UNSIGNED NOT NULL DEFAULT 0 AFTER `pid`;");
+
+      // Move the files into the core {files} table.
+      $result = db_query("SELECT pid, nid, filepath, filemime, filesize FROM {audio_image};");
+      while ($file = db_fetch_object($result)) {
+        $fid = db_next_id('{files}_fid');
+        db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $file->nid, basename($file->filepath), $file->filepath, $file->filemime, $file->filesize);
+        db_query("UPDATE {audio_image} SET fid = %d WHERE pid = %d", $fid, $file->pid);
+      }
+
+      $ret[] = update_sql("ALTER TABLE {audio_image} 
+        DROP COLUMN `pid`,
+        DROP COLUMN `filemime`,
+        DROP COLUMN `filepath`,
+        DROP COLUMN `filesize`,
+        DROP INDEX `audio_image_vid_pictype`,
+        DROP PRIMARY KEY,
+        ADD PRIMARY KEY (`vid`, `pictype`), 
+        ADD INDEX audio_image_fid(`fid`);");      
+
+      break;
+  }
+  return $ret;
+}
