This simple patch by Robert Douglas introduces a fileapi hook. Nothing fancy, nothing difficult and most of all harldy any performance overhead.

It simply allows modules to hook into the data of the file on:
insert
delete
update
load

The reason why this should be here, and not in nodeapi, is because, for example media.module wants to "do stuff" with the file, like get id3 data and put that into a table on upload of a file.
Another valid use case would be to be able to track and count downloads. Yet another case would be some file renaming, or organising in directories.

Bèr

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

walkah’s picture

+1 for this idea (even though I don't actually see a patch) .

My big concern here is trying to manage the order of operations. particularly when you get into resizing / re-encoding things. i.e. how could we handle multiple modules that want to do generate thumbnails? or multiple bitrates for a media file?

overall though, i like the idea alot!

(was there actually a patch somewhere?)

Bèr Kessels’s picture

FileSize
1.92 KB

forgot the attachement. *blush*

robertDouglass’s picture

James,

how hard would it be to react to these lifecycle events in your image.module so that any image file that gets uploaded via upload.module gets handled as an image automatically?

As to the issue of "competing" interests from various modules on the same events (what you described above), don't we already have that danger in nodeapi? Have we run into any problems there? The things you want to do with files are rather discreet, I can't imagine two modules wanting to read the bitrate of a movie separately.

Anyway, thanks Bèr for submitting this patch, I'm looking forward to having people evaluate the idea and help think of possible uses and problems.

javanaut’s picture

I would suggest adding a "validate" hook in there as well. This would allow for file cleaning modules to be developed (such as virus scanners). This could be placed along with the other validation checks in upload_nodeapi().

robertDouglass’s picture

FileSize
2.46 KB

Added validate hook (2 lines). Please verify if this looks like the best choice of location.

robertDouglass’s picture

Changed version to cvs

robertDouglass’s picture

Assigned: Unassigned » robertDouglass
Amazon’s picture

+1 We need this for musicforamerica. We need to allow artists to enter meta data about themselves and their files. Also, we want to be able to allow for encoding based on the metadata as selected by artists.

So on upload we encode a low bitstream for radio. We will also encode at a bitrate the artist allows us to distribute their music.

Kieran

JonBob’s picture

Could we call this hook_file() instead? IIRC, I agree with Dries on this matter; the similar hook_user() and hook_taxonomy() constructs are more nicely named than hook_nodeapi() is.

robertDouglass’s picture

FileSize
2.44 KB

Sure! It is now hook_file.

joshk’s picture

Title: introduce fileapi hook » introduce hook_file +1

This will be useful for a whole lot of things going forward. Another example this would help drive is bittorrent integration.

JonBob’s picture

Title: introduce hook_file +1 » Introduce file API hook
moshe weitzman’s picture

i'll add to the chorus that this is a useful, minimally invasive patch. very drupalish. would be nice to run media.module without needing to patch core.

chx’s picture

another +1

I think, in general, the more hooks, the better. Most time is consumed by SQL queries and file loading after all, so a few php function calls are not big deal.

killes@www.drop.org’s picture

I also like this patch. It will make the revisioning of files easier. Currently, I have to keep the nid column in some tables. I can remove that after the patch has landed and rely on vid only.

moshe weitzman’s picture

still applies cleanly ... seems like a a handy enhancement to me as well.

Bèr Kessels’s picture

Status: Needs review » Reviewed & tested by the community

Patch still applies. with a little fuzzyness (1 or 2 lines).

Steven’s picture

Status: Reviewed & tested by the community » Needs review

Shouldn't the validate hook allow the file to be actually validated? Now its return value seems to be unused.

It also seems weird to me to tie this hook into upload.module... shouldn't it go into file.inc somewhere, so it can apply to all uploaded files?

telex4’s picture

+1 from me. Drupal is really lacking in the file-handling department at the moment, which is quite a pain for us at Remix Reading since our web site is supposed to support a file-sharing remix community! From what I can see this at least starts to point Drupal in the right direction...

walkah’s picture

ok, let me qualify my previous +1 , I agree with Steven, this should *absolutely* be part of file.inc *not* upload.module - as there are some other important modules that utilise the file api besides upload.module.

it will also likely require refactoring file.inc a bit - since you need to accomodate for several "file saving" senarios - file_save_upload, file_save_data (and perhaps file_move and file_copy?)

moshe weitzman’s picture

Like nodeapi('validate'), this validate hook has no return value. Modules are encouraged to form_set_error() if they want to. This will stop the node validation automatically. I think this is a non issue.

As for hooking into the file API directly, I think I agree. I asked walkah and robert to comment here on this since they are more familiar with file.inc.

-moshe

robertDouglass’s picture

Assigned: robertDouglass » Unassigned
Status: Needs review » Needs work

I would be happy to see file.inc refactored to support this hook, but I remember that at the time I created it, it wasn't possible to add it there (details long ago forgotten). File.inc is the obvious appropriate place, and everyone seems to like the functionality. I can't volunteer to look into this at the moment, however. Unassigning myself.

moshe weitzman’s picture

anyone up for this file.inc job. if i could assign it to walkah, i would :)

kwalsh-1’s picture

I am somewhat new to Drupal. I am wanting to use the media module and am told I need to patch upload.module with this patch. How do you install the patch?

magico’s picture

Version: » 6.x-dev

Big and high priority feature!
See also http://drupal.org/node/25685

Owen Barton’s picture

Status: Needs work » Closed (duplicate)

Dupe of #142995