Skip to content

Commit

Permalink
Merge pull request #13073 from woocommerce/remove-need-for-common-io
Browse files Browse the repository at this point in the history
Update function usage in SSR and remove need for commons-io
  • Loading branch information
hafizrahman authored Dec 11, 2024
2 parents c891053 + c616faf commit 385f8bb
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [*] Blaze Campaign Intro screen now offers creating a new product if the site has no products yet [https://github.com/woocommerce/woocommerce-android/pull/13001]
- [Internal] Updated androidx-lifecycle to 2.8.7. [https://github.com/woocommerce/woocommerce-android/pull/13046/]
- [*] When entering a wrong WordPress.com account for login, retrying will bring the step back to the email input screen [https://github.com/woocommerce/woocommerce-android/pull/13024]
- [Internal] Replaces a function in WCSSRExt that then removes the need for commons-io dependency. [https://github.com/woocommerce/woocommerce-android/pull/13073]
- [Internal] Removes coupons feature announcement banner [https://github.com/woocommerce/woocommerce-android/pull/13077]

- [Internal] Updated androidx-compose-bom to 2024.09.00 [https://github.com/woocommerce/woocommerce-android/pull/13060]
Expand Down
1 change: 0 additions & 1 deletion WooCommerce/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ dependencies {
testImplementation(libs.cashapp.turbine)

implementation(libs.apache.commons.text)
implementation(libs.commons.io)

implementation(libs.tinder.statemachine)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.woocommerce.android.extensions

import org.apache.commons.text.StringEscapeUtils
import java.text.DecimalFormat
import java.util.Locale
import kotlin.math.log10
import kotlin.math.pow

const val BYTES_IN_KILOBYTE = 1024.0

/**
* Checks if a given string is a Float
Expand Down Expand Up @@ -100,3 +105,27 @@ fun String.toCamelCase(delimiter: String = " "): String {
fun String.capitalize(locale: Locale = Locale.getDefault()) = replaceFirstChar {
if (it.isLowerCase()) it.titlecase(locale) else it.toString()
}

/**
* Converts a numeric string representing bytes into a human-readable file size string.
*
* Source: https://stackoverflow.com/a/5599842
*
* Examples:
* "1024".readableFileSize() -> "1 kB"
* "1500000".readableFileSize() -> "1.4 MB"
* "0".readableFileSize() -> "0"
* Invalid input returns "0"
*
* @return Formatted string with size and unit (e.g., "1.5 GB")
*/
fun String.readableFileSize(): String {
val size = this.toLongOrNull()
if (size == null || size <= 0) return "0"

val units = arrayOf("B", "kB", "MB", "GB", "TB", "PB", "EB")
val digitGroups = (log10(size.toDouble()) / log10(BYTES_IN_KILOBYTE)).toInt()

return DecimalFormat("#,##0.#")
.format(size / BYTES_IN_KILOBYTE.pow(digitGroups.toDouble())) + " " + units[digitGroups]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.woocommerce.android.extensions

import androidx.core.text.HtmlCompat
import com.woocommerce.android.util.WooLog
import org.apache.commons.io.FileUtils.byteCountToDisplaySize
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -38,7 +37,7 @@ fun WCSSRModel.formatResult(): String {
sb.append(HEADING_SSR)

// Environment
environment?.let { it ->
environment?.let {
try {
sb.append(formatEnvironmentData(JSONObject(it)))
} catch (e: JSONException) {
Expand Down Expand Up @@ -110,7 +109,7 @@ private fun formatEnvironmentData(data: JSONObject): String {

val memoryLimit = data.optString("wp_memory_limit", MISSING_VALUE)
if (memoryLimit != MISSING_VALUE) {
sb.append("WP Memory Limit: ${byteCountToDisplaySize(memoryLimit.toLong())}\n")
sb.append("WP Memory Limit: ${memoryLimit.readableFileSize()}\n")
}

sb.append("WP Debug Mode: ${checkIfTrue(data.optBoolean("wp_debug_mode", false))}\n")
Expand All @@ -123,7 +122,7 @@ private fun formatEnvironmentData(data: JSONObject): String {

val postMaxSize = data.optString("php_post_max_size", MISSING_VALUE)
if (postMaxSize != MISSING_VALUE) {
sb.append("PHP Post Max Size: ${byteCountToDisplaySize(postMaxSize.toLong())}\n")
sb.append("PHP Post Max Size: ${postMaxSize.readableFileSize()}\n")
}

sb.append("PHP Time Limit: ${data.optString("php_max_execution_time", MISSING_VALUE)} s\n")
Expand All @@ -134,7 +133,7 @@ private fun formatEnvironmentData(data: JSONObject): String {

val maxUploadSize = data.optString("max_upload_size", MISSING_VALUE)
if (maxUploadSize != MISSING_VALUE) {
sb.append("PHP Post Max Size: ${byteCountToDisplaySize(maxUploadSize.toLong())}\n")
sb.append("Max Upload Size: ${maxUploadSize.readableFileSize()}\n")
}

sb.append("Default Timezone: ${data.optString("default_timezone", MISSING_VALUE)}\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.woocommerce.android.extensions

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class StringExtTest {
@Test
fun `given string with valid size, when readableFileSize called, then returns formatted size`() {
// Given
val inputs = mapOf(
"1024" to "1 kB",
"1500000" to "1.4 MB",
"1234567" to "1.2 MB",
"1073741824" to "1 GB",
"1099511627776" to "1 TB"
)

// When & then
inputs.forEach { (input, expected) ->
assertThat(input.readableFileSize()).isEqualTo(expected)
}
}

@Test
fun `given zero or negative size, when readableFileSize called, then returns zero`() {
// Given
val inputs = listOf("0", "-1024", "-1")

// When & then
inputs.forEach { input ->
assertThat(input.readableFileSize()).isEqualTo("0")
}
}

@Test
fun `given invalid number string, when readableFileSize called, then returns zero`() {
// Given
val inputs = listOf("", "abc", "12.34", "1,000")

// When & then
inputs.forEach { input ->
assertThat(input.readableFileSize()).isEqualTo("0")
}
}

@Test
fun `given very large number, when readableFileSize called, then returns correct unit`() {
// Given
val size = "1" + "0".repeat(18) // 1 quintillion bytes

// When
val result = size.readableFileSize()

// Then
assertThat(result).isEqualTo("888.2 PB")
}
}
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ bumptech-glide = '4.16.0'
cashapp-turbine = '1.0.0'
coil = '2.1.0'
commons-fileupload = '1.5'
commons-io = '2.11.0'
dependency-analysis = '1.28.0'
detekt = '1.23.5'
facebook-flipper = '0.176.1'
Expand Down Expand Up @@ -171,7 +170,6 @@ cashapp-turbine = { group = "app.cash.turbine", name = "turbine", version.ref =
coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" }
coil-svg = { group = "io.coil-kt", name = "coil-svg", version.ref = "coil" }
commons-fileupload = { group = "commons-fileupload", name = "commons-fileupload", version.ref = "commons-fileupload" }
commons-io = { group = "commons-io", name = "commons-io", version.ref = "commons-io" }
detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" }
facebook-flipper-main = { group = "com.facebook.flipper", name = "flipper", version.ref = "facebook-flipper" }
facebook-flipper-network-plugin = { group = "com.facebook.flipper", name = "flipper-network-plugin", version.ref = "facebook-flipper" }
Expand Down

0 comments on commit 385f8bb

Please sign in to comment.