NAME

Apache2::ASP::MediaManager - Instant file management for Apache2::ASP applications

SYNOPSIS

First, add the following to your httpd.conf file:

PerlSetEnv APACHE2_MEDIA_MANAGER_UPLOAD_ROOT /usr/local/dstack/MEDIA

# Configuration for MediaManager:
RedirectMatch ^/media/(.*) /handlers/MyMediaManager?file=$1

Then, in your /handlers directory for your Apache2::ASP website, make a new file named "MyMediaManager.pm":

package MyMediaManager;

use strict;
use base 'Apache2::ASP::MediaManager';

#==============================================================================
# Make sure the user is authorized to upload files to this server:
sub before_create
{
  my ($s, $Session, $Request, $Response, $Server, $Application, $Upload) = @_;
  
  my $user = my_user_finding_method();
  if( ! ( $user && $user->can_upload_files() ) )
  {
    return 1;
  }
  else
  {
    $Session->{message} = "You are not authorized to upload files";
    $Response->Redirect("/unauthorized.asp");
    return 0;
  }# end if()
}# end before_create()

#==============================================================================
# Redirect the user to a "thank you" page:
sub after_create
{
  my ($s, $Session, $Request, $Response, $Server, $Application, $Upload) = @_;
  
  # Do whatever we want to the new file on disk:
  # recombobulate( $Upload->{new_file} );
  
  # Store a friendly message:
  $Session->{message} = "Your upload was successful";
  
  # Redirect the user to your "Thank you" page:
  $Response->Redirect( "/upload_completed.asp?file=$Upload->{link_to_file}" );
}# end after_create()

1;# return true:

Now, when you want to upload files, just point the upload form to /handlers/MyMediaManager, like so:

<html>
...
<form method="POST" enctype="multipart/form-data" action="/handlers/MyMediaManager">
  <!-- This "mode" parameter tells the 
  <input type="hidden" name="mode" value="create">
  <input type="file" name="filename">
  <input type="submit" value="Click Here to Upload">
</form>
...
</html>

DESCRIPTION

Almost any web application will eventually require some kind of file-upload functionality.

Apache2::ASP aims to deliver this right out of the box. Since all the file-upload work is already done, all you need to do is subclass Apache2::ASP::MediaManager and go from there (as shown in the synopsis above).

OVERRIDABLE METHODS

before_create($s, $Session, $Request, $Response, $Server, $Application, $Upload)

Called just before we begin reading data from the client. This would be a good time to verify that the user is allowed to upload files.

after_create($s, $Session, $Request, $Response, $Server, $Application, $Upload)

Called just after we have finished writing data to the new file. This would be a good time to do any post-processing on the file (i.e. store metadata about the upload in a database).

before_update($s, $Session, $Request, $Response, $Server, $Application, $Upload)

Called just before we begin reading data from the client. This would be a good time to verify that the user is allowed to update this file.

after_update($s, $Session, $Request, $Response, $Server, $Application, $Upload)

Called just after we have finished writing data to the existing file. This would be a good time to do any post-processing on the file (i.e. store metadata about the upload in a database, delete any cached thumbnails, etc.).

before_delete($s, $Session, $Request, $Response, $Server, $Application)

Called just before we delete the file from disk. This would be a good time to verify that the user is allowed to delete this file.

after_delete($s, $Session, $Request, $Response, $Server, $Application)

Called just after we delete the file from disk. This would be a good time to do any post-processing (i.e. delete any metadata about the file in a database, delete any cached thumbnails, etc.)

before_download($s, $Session, $Request, $Response, $Server, $Application)

Called just after we have verified that the file exists and that we can open it for reading, but before we have printed any data to the client. This would be a good time to verify that the user is allowed to download this file.

after_download($s, $Session, $Request, $Response, $Server, $Application)

Called just after we have finished transferring the file to the client. This would be a good time to make a note that the download occurred (who downloaded what and when).

ADVANCED METHODS

The following are overridable methods that - if necessary - can be overridden to achieve specialized functionality (such as composing file paths differently, for example).

compose_download_file_path($s, $Session, $Request, $Response, $Server, $Application)

compose_download_file_name($s, $Session, $Request, $Response, $Server, $Application)

compose_upload_file_name($s, $Session, $Request, $Response, $Server, $Application, $Upload)

delete_file($s, $Session, $Request, $Response, $Server, $Application, $filename)

open_file_for_writing($s, $Session, $Request, $Response, $Server, $Application, $filename)

send_http_headers($s, $Session, $Request, $Response, $Server, $Application, $filename, $file, $ext)

compose_upload_file_name($s, $Session, $Request, $Response, $Server, $Application, $Upload)

open_file_for_appending($s, $Session, $Request, $Response, $Server, $Application, $filename)

SEE ALSO

See Apache2::ASP::UploadHandler for details about the $Upload parameter passed to the *_create and *_update methods.

AUTHOR

John Drago mailto:jdrago_999@yahoo.com

COPYRIGHT AND LICENSE

Copyright 2007 John Drago, All rights reserved.

This software is free software. It may be used and distributed under the same terms as Perl itself.