Changeset 966

Show
Ignore:
Timestamp:
12/21/06 00:40:52 (2 years ago)
Author:
bchoate
Message:

Updates to better handle storage/removal of asset cache files.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/wheeljack/lib/MT/Asset.pm

    r937 r966  
    119119    my ($class) = @_; 
    120120    my $package = $Classes{$class}; 
    121    if ($package) { 
     121    if ($package) { 
    122122        if (defined *{$package.'::new'}) { 
    123123            return $package; 
     
    181181        } 
    182182    } 
     183    $asset->remove_cached_files; 
    183184    $asset->SUPER::remove(@_); 
     185} 
     186 
     187sub remove_cached_files { 
     188    my $asset = shift; 
     189 
     190    # remove any asset cache files that exist for this asset 
     191    my $blog = $asset->blog; 
     192    if ($asset->id && $blog) { 
     193        my $path = $blog->site_path; 
     194        my $cache_dir = MT->config('AssetCacheDir'); 
     195        if ($path && $cache_dir) { 
     196            my $fmgr = $blog->file_mgr; 
     197            if ($fmgr) { 
     198                my $cache_glob = File::Spec->catfile($path, $cache_dir, 
     199                    $asset->id . '.*'); 
     200                my @files = glob($cache_glob); 
     201                foreach my $file (@files) { 
     202                    $fmgr->delete($file); 
     203                } 
     204            } 
     205        } 
     206    } 
     207    1; 
    184208} 
    185209 
     
    210234    my $pkg = shift; 
    211235    my ($filename) = @_; 
    212     my $classes = $pkg->classes || []; 
     236    my $classes; 
     237    if ($pkg eq 'MT::Asset') { 
     238        # special case to check for all registered classes, not just 
     239        # those that are subclasses of this package. 
     240        $classes = [ values %Types ]; 
     241    } 
     242    $classes ||= $pkg->classes || []; 
    213243    foreach my $class (@$classes) { 
    214244        my $this_pkg = $pkg->class_handler($class); 
     
    239269} 
    240270 
     271sub thumbnail_filename { 
     272    undef; 
     273} 
     274 
    241275sub stock_icon_url { 
    242276    undef; 
     
    245279sub thumbnail_url { 
    246280    my $asset = shift; 
    247     my $file_path = $asset->thumbnail_file(@_); 
    248     if ((defined $file_path) && (-f $file_path)) { 
     281    if (my $blog = $asset->blog) { 
    249282        require File::Basename; 
    250         my ($base) = File::Basename::basename($file_path); 
    251         my $url = $asset->url; 
    252         my $file = $asset->file_name; 
    253         $url =~ s/%([A-F0-9]{2})/chr(hex($1))/gei; 
    254         $url =~ s!\Q$file\E$!$base!; 
    255         return $url; 
     283        my $file = File::Basename::basename($asset->thumbnail_file(@_)); 
     284        my $site_url = $blog->site_url; 
     285        if ($file && $site_url) { 
     286            $file =~ s/%([A-F0-9]{2})/chr(hex($1))/gei; 
     287            $site_url .= '/' unless $site_url =~ m#/$#; 
     288            $site_url .= MT->config('AssetCacheDir') . '/' . $file; 
     289            return $site_url; 
     290        } 
    256291    } 
    257292    # Use a stock icon 
     
    281316    my $asset = shift; 
    282317    my ($param) = @_; 
     318    $asset->remove_cached_files; 
    283319    1; 
    284320} 
  • branches/wheeljack/lib/MT/Asset/Image.pm

    r957 r966  
    3434    return undef unless @imginfo; 
    3535 
    36     my $h = $param{Height}; 
    37     my $w = $param{Width}; 
    38     require File::Basename; 
    39     my $path = File::Basename::dirname($file_path); 
    40     my $file = $asset->file_name; 
    41     $file =~ s!\.[a-z]+$!!i; 
    42     my $thumbnail = File::Spec->catfile($path, $file . '-thumb-' . $h . 'x' . $w . '.' . $asset->file_ext); 
     36    my $blog = $param{Blog} || $asset->blog; 
     37    return undef unless $blog; 
     38    my $fmgr; 
     39 
     40    require MT::Util; 
     41    my $asset_cache_path = File::Spec->catdir($blog->site_path, 
     42        MT->config('AssetCacheDir')); 
     43    if (!-d $asset_cache_path) { 
     44        $fmgr = $blog->file_mgr; 
     45        $fmgr->mkpath($asset_cache_path) or return undef; 
     46    } 
     47 
     48    my $file = $asset->thumbnail_filename(@_); 
     49    my $thumbnail = File::Spec->catfile($asset_cache_path, $file); 
    4350    my @thumbinfo = stat($thumbnail); 
    4451 
     
    4956 
    5057    # stale or non-existent thumbnail. let's create one! 
    51     my $blog = $param{Blog} || $asset->blog; 
    52     return undef unless $blog; 
    53     my $fmgr = $blog->file_mgr; 
     58    $fmgr ||= $blog->file_mgr; 
    5459    return undef unless $fmgr; 
     60 
     61    return undef unless $fmgr->can_write($asset_cache_path); 
    5562 
    5663    # create a thumbnail for this file 
     
    6572    # 100000px wide/tall => 164x164 
    6673    #     scale the horizontal to fit 
     74 
     75    my $h = $param{Height}; 
     76    my $w = $param{Width}; 
    6777 
    6878    # find the longest dimension of the image: 
     
    104114        or return $asset->error(MT->translate("Error creating thumbnail file: [_1]", $fmgr->errstr)); 
    105115    return $thumbnail; 
     116} 
     117 
     118sub thumbnail_filename { 
     119    my $asset = shift; 
     120    my (%param) = @_; 
     121 
     122    require MT::Util; 
     123    my $signature = sprintf 'height:%d;width:%d', 
     124        $param{Height}, $param{Width}; 
     125    my $suffix = MT::Util::perl_sha1_digest_hex($signature); 
     126    return $asset->id . '.' . $suffix . '.' . $asset->file_ext; 
    106127} 
    107128 
     
    206227    my ($param) = @_; 
    207228 
     229    $asset->SUPER::on_upload(@_); 
     230 
    208231    my $app = MT->instance; 
    209232    require MT::Util; 
     
    284307        $thumb = $url . MT::Util::encode_url($base . '-thumb' . $ext); 
    285308 
    286         my $img_pkg = MT::Asset->class_handler('image'); 
     309        my $img_pkg = MT::Asset->handler_for_file($t_file); 
    287310        my $asset_thumb = new $img_pkg; 
    288311        my $original = $asset_thumb->clone; 
     
    297320        $asset_thumb->image_height($thumb_height); 
    298321        $asset_thumb->created_by($app->user->id); 
     322        $asset_thumb->parent($asset->id); 
    299323        $asset_thumb->save; 
    300324        MT->run_callbacks('CMSPostSave.asset', $app, $asset_thumb, $original); 
     
    302326        $param->{thumb_asset_id} = $asset_thumb->id; 
    303327 
    304         MT->run_callbacks('CMSUploadFile.thumbnail'
     328        MT->run_callbacks('CMSUploadFile.' . $asset_thumb->class
    305329                          File => $t_file, Url => $thumb, Size => length($blob), 
    306330                          Asset => $asset_thumb, 
     
    308332                          Blog => $blog); 
    309333 
    310         MT->run_callbacks('CMSUploadImage.thumbnail', 
     334        MT->run_callbacks('CMSUploadImage', 
    311335                          File => $t_file, Url => $thumb, 
    312336                          Asset => $asset_thumb, 
     
    375399            $asset_html->file_ext($blog->file_extension); 
    376400            $asset_html->created_by($app->user->id); 
     401            $asset_html->parent($asset->id); 
    377402            $asset_html->save; 
    378403            MT->run_callbacks('CMSPostSave.asset', $app, $asset_html, $original); 
    379404 
    380             MT->run_callbacks('CMSUploadFile.popup'
     405            MT->run_callbacks('CMSUploadFile.' . $asset_html->class
    381406                          File => $abs_file_path, Url => $url, 
    382407                          Asset => $asset_html,