Skip to content

Commit

Permalink
[#343 state:resolved] Moved asset autodirification code into MT::Asse…
Browse files Browse the repository at this point in the history
…t::save
  • Loading branch information
MikeThomsen committed Jan 8, 2011
1 parent 814cf30 commit 2521caf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
29 changes: 29 additions & 0 deletions lib/MT/Asset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,36 @@ sub save {
if ( defined $asset->file_ext ) {
$asset->file_ext( lc( $asset->file_ext ) );
}
if (MT->config->EnableAssetAutoDirify) {
use MT::Util qw(dirify decode_url);
my $delimiter = MT->config->AssetAutoDirifyDelimiter;
my $fmgr = $asset->blog->file_mgr;
my $old_path = $asset->file_path;

##file_ext is stored as jpg, not .jpg, hence the -1
my $basename = substr($asset->file_name, 0, index($asset->file_name, $asset->file_ext)-1);
my $basename_dirified = dirify($basename, $delimiter);
my $path = $asset->file_path;
my $blog_site_url = $asset->blog->site_url();

$path =~ s/$basename/$basename_dirified/;
##Assets with spaces and such in them are URL encoded initially.
##If they are not decoded, then the Perl regexs below will fail.
my $url = decode_url $asset->url;
$url =~ s/$basename/$basename_dirified/;
$url =~ s/$blog_site_url/\%r\//;

##URL should now look like %r/myfolder/myfile.jpeg, not http://www.mydomain.com/myfolder/myfile.jpeg
##This value will let the asset be stored as having a relative path to the blog, making it easier
##to manually move assets in cases like a shared host moving a user between servers.
$asset->file_path($path);
$asset->url($url);
$asset->file_name($basename);
##No need to call $asset->save because it is called below.

$fmgr->rename($old_path, $path); ##Move the file to the new dirified file name

}
unless ( $asset->SUPER::save(@_) ) {

# TODO - note to committers: should this be here? Seems odd.
Expand Down
7 changes: 1 addition & 6 deletions lib/MT/CMS/Asset.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,7 @@ sub _upload_file {
my $basename = $q->param('file') || $q->param('fname');
$basename =~ s!\\!/!g; ## Change backslashes to forward slashes
$basename =~ s!^.*/!!; ## Get rid of full directory paths
if (MT->config->EnableAssetAutoDirify) {
my $delimiter = MT->config->AssetAutoDirifyDelimiter;
my (@parts) = ($basename =~ m/([\w\W]+)(\.[\w]+)/g);
use MT::Util;
$basename = scalar(@parts) == 2 ? MT::Util::dirify($parts[0], $delimiter) . $parts[1] : MT::Util::dirify($basename, $delimiter);
}
if ( $basename =~ m!\.\.|\0|\|! ) {
return
start_upload( $app, %param,
Expand Down

0 comments on commit 2521caf

Please sign in to comment.