Skip to content

Commit 2521caf

Browse files
committed
[#343 state:resolved] Moved asset autodirification code into MT::Asset::save
1 parent 814cf30 commit 2521caf

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/MT/Asset.pm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,36 @@ sub save {
169169
if ( defined $asset->file_ext ) {
170170
$asset->file_ext( lc( $asset->file_ext ) );
171171
}
172+
if (MT->config->EnableAssetAutoDirify) {
173+
use MT::Util qw(dirify decode_url);
174+
my $delimiter = MT->config->AssetAutoDirifyDelimiter;
175+
my $fmgr = $asset->blog->file_mgr;
176+
my $old_path = $asset->file_path;
177+
178+
##file_ext is stored as jpg, not .jpg, hence the -1
179+
my $basename = substr($asset->file_name, 0, index($asset->file_name, $asset->file_ext)-1);
180+
my $basename_dirified = dirify($basename, $delimiter);
181+
my $path = $asset->file_path;
182+
my $blog_site_url = $asset->blog->site_url();
183+
184+
$path =~ s/$basename/$basename_dirified/;
185+
##Assets with spaces and such in them are URL encoded initially.
186+
##If they are not decoded, then the Perl regexs below will fail.
187+
my $url = decode_url $asset->url;
188+
$url =~ s/$basename/$basename_dirified/;
189+
$url =~ s/$blog_site_url/\%r\//;
190+
191+
##URL should now look like %r/myfolder/myfile.jpeg, not http://www.mydomain.com/myfolder/myfile.jpeg
192+
##This value will let the asset be stored as having a relative path to the blog, making it easier
193+
##to manually move assets in cases like a shared host moving a user between servers.
194+
$asset->file_path($path);
195+
$asset->url($url);
196+
$asset->file_name($basename);
197+
##No need to call $asset->save because it is called below.
198+
199+
$fmgr->rename($old_path, $path); ##Move the file to the new dirified file name
172200

201+
}
173202
unless ( $asset->SUPER::save(@_) ) {
174203

175204
# TODO - note to committers: should this be here? Seems odd.

lib/MT/CMS/Asset.pm

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,7 @@ sub _upload_file {
10051005
my $basename = $q->param('file') || $q->param('fname');
10061006
$basename =~ s!\\!/!g; ## Change backslashes to forward slashes
10071007
$basename =~ s!^.*/!!; ## Get rid of full directory paths
1008-
if (MT->config->EnableAssetAutoDirify) {
1009-
my $delimiter = MT->config->AssetAutoDirifyDelimiter;
1010-
my (@parts) = ($basename =~ m/([\w\W]+)(\.[\w]+)/g);
1011-
use MT::Util;
1012-
$basename = scalar(@parts) == 2 ? MT::Util::dirify($parts[0], $delimiter) . $parts[1] : MT::Util::dirify($basename, $delimiter);
1013-
}
1008+
10141009
if ( $basename =~ m!\.\.|\0|\|! ) {
10151010
return
10161011
start_upload( $app, %param,

0 commit comments

Comments
 (0)