NAME
Apache2::ASP::MediaManager - Instant file management for Apache2::ASP applications
SYNOPSIS
First, add the following to your httpd.conf file:
# 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, $asp, $Upload) = @_;
my $user = my_user_finding_method();
if( ! ( $user && $user->can_upload_files() ) )
{
return 1;
}
else
{
$asp->session->{message} = "You are not authorized to upload files";
$asp->response->Redirect("/unauthorized.asp");
return 0;
}# end if()
}# end before_create()
#==============================================================================
# Redirect the user to a "thank you" page:
sub after_create
{
my ($s, $asp, $Upload) = @_;
# Do whatever we want to the new file on disk:
# recombobulate( $Upload->new_file );
# Store a friendly message:
$asp->session->{message} = "Your upload was successful";
# Redirect the user to your "Thank you" page:
$asp->response->Redirect( "/upload_completed.asp" );
}# 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 us what we're going to do -->
<!-- Possible values include "create", "update" and "delete" -->
<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).
PROTECTED METHODS
The following methods are intended for subclasses of Apache2::ASP::MediaManager
.
register_mode( %args )
Allows your MediaManager class to handle "mode=xxx" requests on files.
Example:
package MyMediaManager;
use base 'Apache2::ASP::MediaManager';
__PACKAGE__->register_mode(
name => 'mymode',
handler => \&do_mymode,
);
# Accessible via URLs such as /media/[filename]?mode=mymode
sub do_mymode
{
# ... do stuff ...
$Response->Write("mymode is successful!");
}# end do_mymode()
Any call to /media/file123.gif?mode=mymode
will execute your do_mymode()
method.
This is useful for generating image thumbnails - i.e. /media/file123.gif?mode=thumb&max_w=100&max_h=80
The rest is left as an exercise for the reader.
OVERRIDABLE METHODS
before_create($s, $asp, $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, $asp, $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, $asp, $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, $asp, $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, $asp)
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, $asp)
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, $asp)
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, $asp)
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, or archiving files instead of deleting them, for example).
compose_download_file_path($s, $asp)
Should return a string like "file_to_be_downloaded.gif"
compose_download_file_name($s, $asp)
Should return a string like "/absolute/local/path/to/file.gif"
delete_file($s, $asp, $filename)
Should delete $filename
from disk.
send_http_headers($s, $asp, $filename, $file, $ext)
Should cause any HTTP headers to be sent to the client.
compose_upload_file_name($s, $asp, $Upload)
Should return a string like "uploaded_file.gif"
open_file_for_reading($s, $asp, $filename)
Should return a filehandle opened for reading.
open_file_for_writing($s, $asp, $filename)
Should return a filehandle opened for writing.
open_file_for_appending($s, $asp, $filename)
Should return a filehandle opened for appending.
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.