N°330 Improve \ormDocument::GetFormattedSize

* Fix typo in method name (many thanks @jbostoen !)
* Use \utils::BytesToFriendlyFormat
This commit is contained in:
Pierre Goiffon
2019-12-17 09:58:35 +01:00
parent d4dc739b30
commit 47d8e35639
5 changed files with 19 additions and 16 deletions

View File

@@ -591,18 +591,21 @@ class utils
* Format a value into a more friendly format (KB, MB, GB, TB) instead a juste a Bytes amount.
*
* @param float $value
* @param int $iPrecision
*
* @return string
*/
public static function BytesToFriendlyFormat($value)
public static function BytesToFriendlyFormat($value, $iPrecision = 0)
{
$sReturn = '';
$iPrecision = 0;
// Kilobytes
if ($value >= 1024)
{
$sReturn = 'K';
$value = $value / 1024;
$iPrecision = 1;
if ($iPrecision === 0) {
$iPrecision = 1;
}
}
// Megabytes
if ($value >= 1024)

View File

@@ -85,18 +85,16 @@ class ormDocument
return strlen($this->m_data);
}
public function GetFormatedSize($precision = 2)
/**
* @param int $precision
*
* @return string
* @uses utils::BytesToFriendlyFormat()
*/
public function GetFormattedSize($precision = 2)
{
$bytes = $this->GetSize();
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision).' '.$units[$pow];
return utils::BytesToFriendlyFormat($bytes, $precision);
}
public function GetData()
{

View File

@@ -445,7 +445,7 @@ CSS
$sFileName = utils::HtmlEntities($oDoc->GetFileName());
$sTrId = $this->GetAttachmentContainerId($iAttachmentId);
$sAttachmentMeta = $this->GetAttachmentHiddenInput($iAttachmentId, $bIsDeletedAttachment);
$sFileSize = $oDoc->GetFormatedSize();
$sFileSize = $oDoc->GetFormattedSize();
$bIsTempAttachment = ($oAttachment->Get('item_id') === 0);
$sAttachmentDate = '';
if (!$bIsTempAttachment)

View File

@@ -1164,7 +1164,7 @@ class ObjectController extends BrickController
$aData['icon'] = utils::GetAbsoluteUrlAppRoot().'env-'.utils::GetCurrentEnvironment().'/itop-attachments/icons/image.png';
$aData['att_id'] = $iAttId;
$aData['preview'] = $oDocument->IsPreviewAvailable() ? 'true' : 'false';
$aData['file_size'] = $oDocument->GetFormatedSize();
$aData['file_size'] = $oDocument->GetFormattedSize();
$aData['creation_date'] = $oAttachment->Get('creation_date');
$aData['user_id_friendlyname'] = $oAttachment->Get('user_id_friendlyname');
$aData['file_type'] = $oDocument->GetMimeType();

View File

@@ -314,6 +314,7 @@ JS
HTML
);
/** @var Attachment $oAttachment */
while ($oAttachment = $oSet->Fetch())
{
$iAttId = $oAttachment->GetKey();
@@ -322,6 +323,7 @@ HTML
$sAttachmentMeta = '<input id="attachment_'.$iAttId.'" type="hidden" name="attachments[]" value="'.$iAttId.'">';
/** @var \ormDocument $oDoc */
$oDoc = $oAttachment->Get('contents');
$sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8');
@@ -338,7 +340,7 @@ HTML
}
}
$sFileSize = $oDoc->GetFormatedSize();
$sFileSize = $oDoc->GetFormattedSize();
$sFileType = $oDoc->GetMimeType();
$bIsTempAttachment = ($oAttachment->Get('item_id') === 0);