| 72 | | # and now the magic for getting files by a path |
|---|
| 73 | | MogileFS::register_global_hook( 'cmd_get_paths', sub { |
|---|
| 74 | | my $args = shift; |
|---|
| 75 | | |
|---|
| 76 | | # ensure we got a valid seeming path and filename |
|---|
| 77 | | my ($path, $filename) = |
|---|
| 78 | | ($args->{key} =~ m!^(/(?:[\w\-\.]+/)*)([\w\-\.]+)$!) ? ($1, $2) : (undef, undef); |
|---|
| 79 | | return 0 unless $path && $filename; |
|---|
| 80 | | |
|---|
| 81 | | # now try to get the end of the path |
|---|
| 82 | | my $parentnodeid = MogileFS::Plugin::FilePaths::load_path( $args->{dmid}, $path ); |
|---|
| 83 | | return 0 unless defined $parentnodeid; |
|---|
| 84 | | |
|---|
| 85 | | # great, find this file |
|---|
| 86 | | my $fid = MogileFS::Plugin::FilePaths::get_file_mapping( $args->{dmid}, $parentnodeid, $filename ); |
|---|
| 87 | | return 0 unless defined $fid && $fid > 0; |
|---|
| 88 | | |
|---|
| 89 | | # now pretend they asked for it and continue |
|---|
| 90 | | $args->{key} = "fid:$fid"; |
|---|
| 91 | | return 1; |
|---|
| 92 | | }); |
|---|
| | 72 | # and now magic conversions that make the rest of the MogileFS commands work |
|---|
| | 73 | # without having to understand how the path system works |
|---|
| | 74 | MogileFS::register_global_hook( 'cmd_get_paths', \&_path_to_key ); |
|---|
| | 75 | MogileFS::register_global_hook( 'cmd_delete', \&_path_to_key ); |
|---|
| | 213 | # generic sub that converts a file path to a key name that |
|---|
| | 214 | # MogileFS will understand |
|---|
| | 215 | sub _path_to_key { |
|---|
| | 216 | my $args = shift; |
|---|
| | 217 | |
|---|
| | 218 | # ensure we got a valid seeming path and filename |
|---|
| | 219 | my ($path, $filename) = |
|---|
| | 220 | ($args->{key} =~ m!^(/(?:[\w\-\.]+/)*)([\w\-\.]+)$!) ? ($1, $2) : (undef, undef); |
|---|
| | 221 | return 0 unless $path && $filename; |
|---|
| | 222 | |
|---|
| | 223 | # now try to get the end of the path |
|---|
| | 224 | my $parentnodeid = MogileFS::Plugin::FilePaths::load_path( $args->{dmid}, $path ); |
|---|
| | 225 | return 0 unless defined $parentnodeid; |
|---|
| | 226 | |
|---|
| | 227 | # great, find this file |
|---|
| | 228 | my $fid = MogileFS::Plugin::FilePaths::get_file_mapping( $args->{dmid}, $parentnodeid, $filename ); |
|---|
| | 229 | return 0 unless defined $fid && $fid > 0; |
|---|
| | 230 | |
|---|
| | 231 | # now pretend they asked for it and continue |
|---|
| | 232 | $args->{key} = "fid:$fid"; |
|---|
| | 233 | return 1; |
|---|
| | 234 | } |
|---|
| | 235 | |
|---|