Somehow, trivial problems tend to be the hardest/most time consuming ones to solve, it seems. I'm currently working on a small community website with a focus on collaboration. Among other things, this project calls for a file section, where users can exchange files. So far, thats easy. Just add a new content type, that allows for a single file to be attached and hook it up with taxonomy for navigation.
It could have ended there, but I decided to go for gold...
I figured, that my file area needs checksums on files, so users can validate, that the file they have on disk is actually the same as the one offered for download. Big problem. Drupal does not offer something like this in a ready to use way and so my odyssey began. In essence I was looking for something, that would trigger on a node being saved and then run a small piece of custom PHP code to populate a CCK field with either a MD5 or SHA1 checksum of the uploaded file. First stop: the rules module. Pretty powerful little bastard, but hard to handle. Took me a couple of minutes to figure out how to create the trigger and write something into the prepared field. Afterwards, it took me the entire evening to not figure out how to actually reference the file in question from my custom code. I'm pretty sure, there is a way for doing it, I looked hard for it, but I couldn't find it. I did however find the computed_field module while searching. That one pretty much does the trick.
So, instructions for building a Download node: First of all, install CCK, filefield and the computed field modules. Create a new content type "Upload" and add the two fields "file" (using the "File" type) and "checksum" (using the "Computed" type). Make sure, that only one file can be attached to the node. To actually compute the MD5 checksum, use the following PHP code:
$node_field[0]['value'] = md5_file($node->field_file[0][filepath]);In case, you want to store the checksum in the database instead of recomputing it on the fly, use a varchar of 32 bytes. A SHA1 checksum takes up 40 bytes and the PHP function to calculate it is called sha1_file.
In retrospective, that was just to easy and I bet, my users won't even care about that little bit of extra security.
