| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::FileMgr; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::ErrorHandler ); |
|---|
| 11 | |
|---|
| 12 | sub new { |
|---|
| 13 | my $class = shift; |
|---|
| 14 | my $type = shift; |
|---|
| 15 | $class .= "::" . $type; |
|---|
| 16 | eval "use $class;"; |
|---|
| 17 | die "Unsupported file manager $class: $@" if $@; |
|---|
| 18 | my $fmgr = bless {}, $class; |
|---|
| 19 | $fmgr->init(@_) |
|---|
| 20 | or return $class->error( $fmgr->errstr ); |
|---|
| 21 | return $fmgr; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub init { |
|---|
| 25 | my $fmgr = shift; |
|---|
| 26 | $fmgr; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub get_data; |
|---|
| 30 | sub put; |
|---|
| 31 | sub put_data; |
|---|
| 32 | sub exists; |
|---|
| 33 | sub can_write; |
|---|
| 34 | sub mkpath; |
|---|
| 35 | sub rename; |
|---|
| 36 | sub delete; |
|---|
| 37 | sub content_is_updated { 1 } |
|---|
| 38 | |
|---|
| 39 | sub is_handle { |
|---|
| 40 | my($fmgr, $f) = @_; |
|---|
| 41 | my $fd = ref($f) || ref(\$f) eq 'GLOB' ? fileno($f) : undef; |
|---|
| 42 | return defined $fd; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | 1; |
|---|
| 46 | __END__ |
|---|
| 47 | |
|---|
| 48 | =head1 NAME |
|---|
| 49 | |
|---|
| 50 | MT::FileMgr - Base class for file management drivers |
|---|
| 51 | |
|---|
| 52 | =head1 SYNOPSIS |
|---|
| 53 | |
|---|
| 54 | use MT::FileMgr; |
|---|
| 55 | my $fmgr = MT::FileMgr->new('Local') |
|---|
| 56 | or die MT::FileMgr->errstr; |
|---|
| 57 | defined(my $bytes = $fmgr->put($src, $dest)) |
|---|
| 58 | or die $fmgr->errstr; |
|---|
| 59 | |
|---|
| 60 | =head1 DESCRIPTION |
|---|
| 61 | |
|---|
| 62 | I<MT::FileMgr> is a base class for file management driver implementations; |
|---|
| 63 | it provides an abstraction of file management such that other Movable Type |
|---|
| 64 | classes do not have to worry if files should be built locally or through |
|---|
| 65 | FTP. For example, the process that rebuilds a user's site only has to build |
|---|
| 66 | the content, then pass it off to the I<MT::FileMgr> driver; it does not have |
|---|
| 67 | to worry whether the user's site is hosted locally or on an FTP server. |
|---|
| 68 | I<MT::FileMgr> dispatches control to the appropriate driver, and the content |
|---|
| 69 | is pushed to wherever it needs to be pushed. |
|---|
| 70 | |
|---|
| 71 | =head1 USAGE |
|---|
| 72 | |
|---|
| 73 | =head2 MT::FileMgr->new($type [, @args ]) |
|---|
| 74 | |
|---|
| 75 | Loads the driver class I<$type> (either C<FTP> or C<Local>, currently) and |
|---|
| 76 | returns a new file manager object blessed into the appropriate driver |
|---|
| 77 | subclass. |
|---|
| 78 | |
|---|
| 79 | I<@args> is optional; it will be passed along to the subclass's I<init> |
|---|
| 80 | method, for driver-specific initialization. For example, the C<FTP> driver |
|---|
| 81 | requires the FTP host, username, and password in I<@args>, whereas the C<Local> |
|---|
| 82 | host does not require anything. |
|---|
| 83 | |
|---|
| 84 | If an initialization error occurs, returns C<undef>; see L<ERROR HANDLING>, |
|---|
| 85 | below. |
|---|
| 86 | |
|---|
| 87 | =head2 $fmgr->init() |
|---|
| 88 | |
|---|
| 89 | This is an internal method called by C<MT::FileMgr-E<gt>new> that |
|---|
| 90 | initializes an L<MT::ConfigMgr/instance> as the I<cfg> attribute. |
|---|
| 91 | |
|---|
| 92 | =head2 $fmgr->put($src, $dest [, $type ]) |
|---|
| 93 | |
|---|
| 94 | Puts the contents of the file I<$src> in the path I<$dest>. I<$src> can be |
|---|
| 95 | either a filehandle of the path to a local file; I<$dest> must be a path to |
|---|
| 96 | a file, either local or remote (depending on the driver). |
|---|
| 97 | |
|---|
| 98 | I<$type> is optional and defines whether the I<put> is for an uploaded file |
|---|
| 99 | or for an output HTML file; this tells the I<FileMgr> drivers what mode to |
|---|
| 100 | write the files in, what I<umask> settings to use, etc. The two values for |
|---|
| 101 | I<$type> are C<upload> and C<output>; C<output> is the default. |
|---|
| 102 | |
|---|
| 103 | Returns the number of bytes "put" (can be 0). |
|---|
| 104 | |
|---|
| 105 | On error, returns C<undef>; see L<ERROR HANDLING>, below. |
|---|
| 106 | |
|---|
| 107 | =head2 $fmgr->put_data($data, $dest [, $type ]) |
|---|
| 108 | |
|---|
| 109 | Puts the block of data I<$data> in the path I<$dest>. I<$src> should be a |
|---|
| 110 | scalar containing the data, and I<$dest> should be a path to a file, either |
|---|
| 111 | local or remote (depending on the driver). |
|---|
| 112 | |
|---|
| 113 | I<$type> is optional and defines whether the I<put_data> is for an uploaded |
|---|
| 114 | file or for an output HTML file; this tells the I<FileMgr> drivers what mode |
|---|
| 115 | to write the files in, what I<umask> settings to use, etc. The two values for |
|---|
| 116 | I<$type> are C<upload> and C<output>; C<output> is the default. |
|---|
| 117 | |
|---|
| 118 | Returns the number of bytes "put" (can be 0). |
|---|
| 119 | |
|---|
| 120 | On error, returns C<undef>; see L<ERROR HANDLING>, below. |
|---|
| 121 | |
|---|
| 122 | =head2 $fmgr->get_data($src [, $type ]) |
|---|
| 123 | |
|---|
| 124 | Gets a block of data from the path I<$src>; returns the block of data. |
|---|
| 125 | I<$src> should be a path to a file, either local or remote (depending on the |
|---|
| 126 | driver). |
|---|
| 127 | |
|---|
| 128 | I<$type> is optional and defines whether the I<get_data> is for an uploaded |
|---|
| 129 | file or for an output HTML file; this tells the I<FileMgr> drivers what mode |
|---|
| 130 | to write the files in, what I<umask> settings to use, etc. The two values for |
|---|
| 131 | I<$type> are C<upload> and C<output>; C<output> is the default. |
|---|
| 132 | |
|---|
| 133 | On error, returns C<undef>; see L<ERROR HANDLING>, below. |
|---|
| 134 | |
|---|
| 135 | =head2 $fmgr->exists($path) |
|---|
| 136 | |
|---|
| 137 | Returns true if the file or directory I<$path> exists, false otherwise. |
|---|
| 138 | |
|---|
| 139 | =head2 $fmgr->mkpath($path) |
|---|
| 140 | |
|---|
| 141 | Creates the path I<$path> recursively; in other words, if any of the |
|---|
| 142 | directories in the path do not exist, they are created. Returns true on |
|---|
| 143 | success. |
|---|
| 144 | |
|---|
| 145 | On error, returns C<undef>; see L<ERROR HANDLING>, below. |
|---|
| 146 | |
|---|
| 147 | =head2 $fmgr->rename($src, $dest) |
|---|
| 148 | |
|---|
| 149 | Renames the file or directory I<$src> to I<$dest>. Returns true on success. |
|---|
| 150 | |
|---|
| 151 | On error, returns C<undef>; see L<ERROR HANDLING>, below. |
|---|
| 152 | |
|---|
| 153 | =head2 content_is_updated() |
|---|
| 154 | |
|---|
| 155 | Return one (1). |
|---|
| 156 | |
|---|
| 157 | =head2 $fmgr->is_handle($file) |
|---|
| 158 | |
|---|
| 159 | Return the file descriptor of I<file> or C<undef> if not found. |
|---|
| 160 | |
|---|
| 161 | =head1 ERROR HANDLING |
|---|
| 162 | |
|---|
| 163 | On an error, all of the above methods (except I<exists>) return C<undef>, |
|---|
| 164 | and the error message can be obtained by calling the method I<errstr> on the |
|---|
| 165 | class or the object (depending on whether the method called was a class method |
|---|
| 166 | or an instance method). |
|---|
| 167 | |
|---|
| 168 | For example, called on a class name: |
|---|
| 169 | |
|---|
| 170 | my $fmgr = MT::FileMgr->new('Local') |
|---|
| 171 | or die MT::FileMgr->errstr; |
|---|
| 172 | |
|---|
| 173 | Or, called on an object: |
|---|
| 174 | |
|---|
| 175 | defined(my $bytes = $fmgr->put($src, $dest)) |
|---|
| 176 | or die $fmgr->errstr; |
|---|
| 177 | |
|---|
| 178 | =head1 AUTHOR & COPYRIGHT |
|---|
| 179 | |
|---|
| 180 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 181 | |
|---|
| 182 | =cut |
|---|