The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

Create a file in your /handlers folder named MM.pm and add the following:

  package MM;
  
  use strict;
  use warnings 'all';
  use base 'Apache2::ASP::MediaManager';
  use vars __PACKAGE__->VARS;
  
  sub before_download {
    my ($s, $context, $filename) = @_;
    
    warn "About to download '$filename'";
    return 1;
  }
  
  sub after_download {
    my ($s, $context, $filename) = @_;
    
    warn "Finished download of '$filename'";
    return 1;
  }
  
  sub before_create {
    my ($s, $context, $Upload) = @_;
    
    warn "About to create '$Upload->{filename_only}'";
    return 1;
  }
  
  sub after_create {
    my ($s, $context, $Upload) = @_;
    
    warn "Just created '$Upload->{filename_only}'";
    return $Response->Redirect( $ENV{HTTP_REFERER} );
  }
  
  sub before_update {
    my ($s, $context, $Upload) = @_;
    
    warn "About to update '$Upload->{filename_only}'";
    return 1;
  }
  
  sub after_update {
    my ($s, $context, $Upload) = @_;
    
    warn "Just updated '$Upload->{filename_only}'";
    return $Response->Redirect( $ENV{HTTP_REFERER} );
  }
  
  sub after_delete {
    my ($s, $context, $filename) = @_;
    
    warn "Just deleted '$filename'";
    return $Response->Redirect( $ENV{HTTP_REFERER} );
  }
  
  1;# return true:

Then create a file in your /htdocs folder named mm-test.asp:

  <html>
    <body>
      <h1>File Upload Test</h1>
      <form
          method="post"
          enctype="multipart/form-data"
          action="/handlers/MM?mode=create&uploadID=2kj4hkj234h">
        <input type="file" name="filename" >
        <input type="submit" value="Upload File Now">
      </form>
      
      <h2>Existing Files (if any)</h2>
      <p>
  <%
    opendir my $dir, $Config->web->media_manager_upload_root;
    while( my $file = readdir($dir) )
    {
      next unless -f $Config->web->media_manager_upload_root . '/' . $file;
  %>
        <a href="/handlers/MM?file=<%= $file %>"><%= $file %></a><br>
  <%
    }# end while()
  %>
      </o>
    </body>
  </html>

REALLY IMPORTANT!: Notice the ?mode=create&uploadID=2kj4hkj234h in the action attribute of the form tag. The mode tells the MediaManager that we are creating a file, and the uploadID will allow us to track the progress of the upload.

IMPORTANT: Check your configuration, where you see:

  <config>
    ...
    <web>
      ...
      <media_manager_upload_root>@ServerRoot@/MEDIA</media_manager_upload_root>
      ...
    </web>
    ...
  </config>

Make *sure* that your webserver has ownership or read/write access to that folder and all of its contents.

When you access http://yoursite.com/mm-test.asp in your browser and submit the form, Apache2::ASP::MediaManager will save it under your media_manager_upload_root folder for you.

DESCRIPTION

Handling file uploads can be a real pain. Restricting file uploads and downloads to a select group of users is also problematic.

And Then...

And then there was Apache2::ASP::MediaManager. Now you can have fully-functional file uploads in seconds.

UPLOAD PROGRESS INDICATORS

Apache2::ASP::MediaManager makes it easy to provide upload progress indicators.

Remember that uploadID parameter in the action attribute of your form? While the upload is taking place, the $Session object is getting updated with the status of the upload. If you were to make another handler - /handlers/UploadProgress.pm - and insert the following code:

  package UploadProgress;

  use strict;
  use base 'Apache2::ASP::FormHandler';
  use vars __PACKAGE__->VARS;

  sub run {
    my ($s, $context) = @_;
    
    my $uploadID = $Form->{uploadID};
    
    $Session->{"upload$uploadID" . "percent_complete"} ||= 0;
    
    $Response->Expires( -30 );
    $Response->Write( $Session->{"upload$uploadID" . "percent_complete"} );
  }# end run()

  1;# return true:

And add call out to it via AJAX - you can get real-time upload progress information about the current upload.

Example:

  window.setInterval(function() {
    httpOb.open("GET", "/handlers/UploadProgress?uploadID=2kj4hkj234h", true);
    httpOb.onreadystatechange = function() {
      if( httpOb.readyState == 4 ) {
        document.getElementById("percent_complete").innerHTML = httpOb.responseText + '%';
      }// end if()
    };
  }, 1000);

You should also add an element with an id of "percent_complete" to he form:

  <div id="percent_complete">0%</div>

PUBLIC PROPERTIES

None.

PUBLIC METHODS

Nothing you need to worry about.

EVENTS

About the $Upload argument:

The $Upload argument is an instance of Apache2::ASP::UploadHookArgs.

before_download( $self, $context )

Called before allowing a file to be downloaded from the server.

NOTE: This method must return true, or the file will not be downloaded.

after_download( $self, $context )

Called after allowing a file to be downloaded from the server.

before_create( $self, $context, $Upload )

Called before allowing a new file to be uploaded to the server.

NOTE: This method must return true, or the file will not be created.

after_create( $self, $context, $Upload )

Called after allowing a new file to be uploaded to the server.

before_update( $self, $context, $Upload )

Called before allowing a new file to be uploaded to replace an existing file.

NOTE: This method must return true, or the file will not be updated.

after_update( $self, $context, $Upload )

Called after a new file has been uploaded to replace an existing file.

before_delete( $self, $context, $filename )

Called before deleting a file.

NOTE: This method must return true, or the file will not be deleted.

after_delete( $self, $context, $filename )

Called after deleting a file.

BUGS

It's possible that some bugs have found their way into this release.

Use RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP to submit bug reports.

HOMEPAGE

Please visit the Apache2::ASP homepage at http://www.devstack.com/ to see examples of Apache2::ASP in action.

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.