Changeset 966
- Timestamp:
- 12/21/06 00:40:52 (2 years ago)
- Files:
-
- branches/wheeljack/lib/MT/Asset.pm (modified) (6 diffs)
- branches/wheeljack/lib/MT/Asset/Image.pm (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/wheeljack/lib/MT/Asset.pm
r937 r966 119 119 my ($class) = @_; 120 120 my $package = $Classes{$class}; 121 if ($package) {121 if ($package) { 122 122 if (defined *{$package.'::new'}) { 123 123 return $package; … … 181 181 } 182 182 } 183 $asset->remove_cached_files; 183 184 $asset->SUPER::remove(@_); 185 } 186 187 sub 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; 184 208 } 185 209 … … 210 234 my $pkg = shift; 211 235 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 || []; 213 243 foreach my $class (@$classes) { 214 244 my $this_pkg = $pkg->class_handler($class); … … 239 269 } 240 270 271 sub thumbnail_filename { 272 undef; 273 } 274 241 275 sub stock_icon_url { 242 276 undef; … … 245 279 sub thumbnail_url { 246 280 my $asset = shift; 247 my $file_path = $asset->thumbnail_file(@_); 248 if ((defined $file_path) && (-f $file_path)) { 281 if (my $blog = $asset->blog) { 249 282 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 } 256 291 } 257 292 # Use a stock icon … … 281 316 my $asset = shift; 282 317 my ($param) = @_; 318 $asset->remove_cached_files; 283 319 1; 284 320 } branches/wheeljack/lib/MT/Asset/Image.pm
r957 r966 34 34 return undef unless @imginfo; 35 35 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); 43 50 my @thumbinfo = stat($thumbnail); 44 51 … … 49 56 50 57 # 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; 54 59 return undef unless $fmgr; 60 61 return undef unless $fmgr->can_write($asset_cache_path); 55 62 56 63 # create a thumbnail for this file … … 65 72 # 100000px wide/tall => 164x164 66 73 # scale the horizontal to fit 74 75 my $h = $param{Height}; 76 my $w = $param{Width}; 67 77 68 78 # find the longest dimension of the image: … … 104 114 or return $asset->error(MT->translate("Error creating thumbnail file: [_1]", $fmgr->errstr)); 105 115 return $thumbnail; 116 } 117 118 sub 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; 106 127 } 107 128 … … 206 227 my ($param) = @_; 207 228 229 $asset->SUPER::on_upload(@_); 230 208 231 my $app = MT->instance; 209 232 require MT::Util; … … 284 307 $thumb = $url . MT::Util::encode_url($base . '-thumb' . $ext); 285 308 286 my $img_pkg = MT::Asset-> class_handler('image');309 my $img_pkg = MT::Asset->handler_for_file($t_file); 287 310 my $asset_thumb = new $img_pkg; 288 311 my $original = $asset_thumb->clone; … … 297 320 $asset_thumb->image_height($thumb_height); 298 321 $asset_thumb->created_by($app->user->id); 322 $asset_thumb->parent($asset->id); 299 323 $asset_thumb->save; 300 324 MT->run_callbacks('CMSPostSave.asset', $app, $asset_thumb, $original); … … 302 326 $param->{thumb_asset_id} = $asset_thumb->id; 303 327 304 MT->run_callbacks('CMSUploadFile. thumbnail',328 MT->run_callbacks('CMSUploadFile.' . $asset_thumb->class, 305 329 File => $t_file, Url => $thumb, Size => length($blob), 306 330 Asset => $asset_thumb, … … 308 332 Blog => $blog); 309 333 310 MT->run_callbacks('CMSUploadImage .thumbnail',334 MT->run_callbacks('CMSUploadImage', 311 335 File => $t_file, Url => $thumb, 312 336 Asset => $asset_thumb, … … 375 399 $asset_html->file_ext($blog->file_extension); 376 400 $asset_html->created_by($app->user->id); 401 $asset_html->parent($asset->id); 377 402 $asset_html->save; 378 403 MT->run_callbacks('CMSPostSave.asset', $app, $asset_html, $original); 379 404 380 MT->run_callbacks('CMSUploadFile. popup',405 MT->run_callbacks('CMSUploadFile.' . $asset_html->class, 381 406 File => $abs_file_path, Url => $url, 382 407 Asset => $asset_html,
