<?php if (!defined('PmWiki')) exit();

/**
Attach Size
Written by Maxim in 2009

This recipe adds (:attachsize filename.ext:) markup.

It is hereby released into the public domain. You may do
anything you want with this with no implied warranty.
*/

function GetAttachSize($path) {
  global $UploadFileFmt, $pagename;

  if (preg_match('!^(.*)/([^/]+)$!', $path, $match)) {
    $pagename = MakePageName($pagename, $match[1]);
    $path = $match[2];
  }
  $upname = MakeUploadName($pagename, $path);
  $filepath = FmtPageName("$UploadFileFmt/".$upname, $pagename);

  if (file_exists($filepath))
  {
    $bytes = filesize($filepath);
    if ($bytes > 1024)
    {
      return sprintf('%dKB', intval($bytes / 1024 + 0.5));
    }
    else
    {
      return sprintf('%dB', $bytes);
    }
  }
  else
  {
    return "File not found: " . $filepath;
  }
}

Markup('attachsize', 'inline', '/\\(:attachsize ([^:]+):\\)/e', "GetAttachSize('$1')");