Skip to content

Commit 6beff8a

Browse files
committed
Move file size format function to Common lib
1 parent 8f72337 commit 6beff8a

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/libraries/Attachment.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,7 @@ public function getContentSize($format = false) {
141141
$bytes = strlen($this->getContent());
142142

143143
if ($format) {
144-
145-
$kilobytes = 1024;
146-
$megabytes = 1024 * $kilobytes;
147-
$gigabytes = 1024 * $megabytes;
148-
$terabytes = 1024 * $gigabytes;
149-
150-
if ($bytes >= $terabytes) {
151-
$bytes = round($bytes / $terabytes, 2) . " TiB";
152-
} else if ($bytes >= $gigabytes) {
153-
$bytes = round($bytes / $gigabytes, 2) . " GiB";
154-
} else if ($bytes >= $megabytes) {
155-
$bytes = round($bytes / $megabytes, 2) . " MiB";
156-
} else if ($bytes >= $kilobytes) {
157-
$bytes = round($bytes / $kilobytes, 2) . " KiB";
158-
} else {
159-
$bytes = $bytes . " B";
160-
}
161-
144+
$bytes = Common::formatFileSize($bytes);
162145
}
163146

164147
return $bytes;

src/libraries/Common.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ public static function curlRequest($url, $post_content = null,
6969
return $response;
7070
}
7171

72+
public static function formatFileSize($value) {
73+
74+
$bytes = $value;
75+
$kilobytes = 1024;
76+
$megabytes = 1024 * $kilobytes;
77+
$gigabytes = 1024 * $megabytes;
78+
$terabytes = 1024 * $gigabytes;
79+
80+
if ($bytes >= $terabytes) {
81+
$bytes = round($bytes / $terabytes, 2) . " TiB";
82+
} else if ($bytes >= $gigabytes) {
83+
$bytes = round($bytes / $gigabytes, 2) . " GiB";
84+
} else if ($bytes >= $megabytes) {
85+
$bytes = round($bytes / $megabytes, 2) . " MiB";
86+
} else if ($bytes >= $kilobytes) {
87+
$bytes = round($bytes / $kilobytes, 2) . " KiB";
88+
} else {
89+
$bytes = $bytes . " B";
90+
}
91+
92+
return $bytes;
93+
}
94+
7295
public static function getVersionProperties() {
7396
$versions = new StdClass();
7497
$versions->bnetdocs = file_get_contents("../etc/.rsync-version");

0 commit comments

Comments
 (0)