views:

170

answers:

2

I have many mp3 files stored on my server already from a static website, and we're now moving to drupal. I'm going to create a node for each audio file, but I don't want to have to upload each file again. I'd rather copy the files into the drupal files directory where I want them, and then associate the nodes with the appropriate file.

Any ideas on how to accomplish that?

Thanks!

A: 

The file_import module doesn't do exactly what you want (it creates node attachments instead of nodes), but it would be relatively simple to use that module as guidance along with the Drapal API to do what you want.

Mike Crittenden
+3  A: 

I am not sure if I am going to propose a different approach or if I am about to tell with different words what you already meant with your original question, but as you want the nodes to be the files, I would rather generate the nodes starting from the files, rather than associating existing nodes with existing files.

In generic terms I would do it programmatically: for each existing files in your import directory I would build the $node object and then invoke node_save($node) to store it in Drupal.

Of course in building the $node object you will need to invoke the API function of the module you are using to manage the files. Here's some sample code I wrote to do a similar task. In this scenario I was attaching a product sheet to a product (a node with additional fields), so...

  • field_sheet was the CCK field for the product sheet in the product node
  • product was the node type
  • $sheet_file was the complete reference (path + filename) to the product sheet file.

So the example:

// Load the CCK field
$field = content_fields('field_sheet', 'product');
// Load the appropriate validators
$validators = array_merge(filefield_widget_upload_validators($field));
// Where do we store the files?
$files_path = filefield_widget_file_path($field);
// Create the file object
$file = field_file_save_file($sheet_file, $validators, $files_path);
// Apply the file to the field, this sets the first file only, could be looped
// if there were more files
$node->field_scheda = array(0 => $file);
// The file has been copied in the appropriate directory, so it can be
// removed from the import directory
unlink($sheet_file);

BTW: if you use a library to read MP3 metadata, you could set $node->title and other attributes in a sensible way.

Hope this helps!

mac
What is $sheet_file in the above example?
googletorp
Browsed the api, looks like $sheet_file is a string of path to the file source. Anyways it's a sound idea to create a node per file in the process. This, however, might be the actual pain point, if the nodes are going to have several fields etc.
googletorp
I get the gist of what you're saying, I think: iterate through each audio file in my directory and create a node for each file, using the filename (or id3 data, to your point) to fill in the information about the node. I assume, since I'd need to use the drupal node api, that I'd write a custom module to accomplish this - is that correct?Thanks for your help!
ldweeks
@googletorp - You've been faster than me... but I updated the example providing some more reference to the context it was developed for. Thanks for pointing that out.
mac
@Idweeks - Yes, you have to write a simple module for achieving that. You should of course set up your node type (e.g. a node called "song") with CCK first (you can define a nodetype programmatically too, but it's more complicated). Also note the call that uses proper validators for the filefield: this implies than when you set up your node, you have to explicity allow as file formats the ones you are going to use (e.g.: ogg, oga, mp3) or the import will fail.
mac

related questions