Validating uploaded files...

Tonight I have been focusing on getting the torrent node type to validate that a file is attached to the node and if not, throw an error alerting the user and do not let them submit until they have attached a torrent. I then started to look at more cases where the user may have attached a torrent, but now they are trying to remove it. While writing the validation functions for these cases I realized that I was being dense and that when the node is inserted I remove the attached torrent once I parse the data. So the only time there will be a torrent attached is never! Also I ran into some issues writing clean code that would extract the info section from the torrent metadata, this is a clear as I could get:

preg_match('/4:info/', $metadata, $matches, PREG_OFFSET_CAPTURE);
$offset = $matches[0][1] + 6;
$info = substr($metadata, $offset, (strlen($metadata)-$offset-1));
print(strtoupper(sha1($info)));

I really just wanted to use preg_match to extract the info, but it stopping running the regex at the beginning of the pieces section :(. This code will work for now, until I can up with something better.

I got to dip my feet into module_invoke() tonight. I was orginally looking for a way to verify a file extension for an uploaded file (this didn't relaly pan out) so I looked into upload.module. I thought I found what I was looking for with upload_munge_filename, but I actually ended up calling it wrong with module_invoke() so nothing happened. then I got to thinking how I would be deleting the .torrent file without all of the hassle of working with the database tables that are not mine. I went diving into upload.module again and their I found upload_delete($node) which deletes all files associated with a node. So I figured I would give module_invoke() another shot. It did not work again! Now I realize that I had screwed up and it was not Drupal :) After puttering around with the function call I finally figured out how it works.

module_invoke('upload', 'delete', $node);

This now gets called after the torrent is processed, so currently the info_hash is calculated, displayed and then the torrent is destroyed.

Also I have spent some more time going through the 4.7 module from digital bicycle and noticed that the php file for bencoding data also includes some handy decode functions as well. I think I will perform some tesitng on these tomorrow, but for now I am going to sleep.