From 9517586245693a07f3626b204301d811730c0893 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Thu, 15 Feb 2024 11:52:47 +0100 Subject: [PATCH] Download (v1/v2): Don't display file size if it is not available. (#20) --- .../impl/models/v1/DownloadV1V2Impl.java | 9 +++++++- .../impl/models/v1/DownloadV1V2ImplTest.java | 22 +++++++++++++++++++ changes.xml | 6 +++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/bundles/core/src/main/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2Impl.java b/bundles/core/src/main/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2Impl.java index 8a876816..dd438c53 100644 --- a/bundles/core/src/main/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2Impl.java +++ b/bundles/core/src/main/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2Impl.java @@ -130,7 +130,7 @@ private void initPropertiesFromMedia() { if (rendition != null) { filename = rendition.getFileName(); format = rendition.getMimeType(); - size = FileUtils.byteCountToDisplaySize(rendition.getFileSize()); + size = toDisplayFileSize(rendition.getFileSize()); extension = rendition.getFileExtension(); } @@ -151,6 +151,13 @@ private void initPropertiesFromMedia() { } } + private static String toDisplayFileSize(long fileSize) { + if (fileSize > 0) { + return FileUtils.byteCountToDisplaySize(fileSize); + } + return null; + } + @Override @NotNull public Media getMediaObject() { diff --git a/bundles/core/src/test/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2ImplTest.java b/bundles/core/src/test/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2ImplTest.java index 7bc4f717..4fe88020 100644 --- a/bundles/core/src/test/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2ImplTest.java +++ b/bundles/core/src/test/java/io/wcm/wcm/core/components/impl/models/v1/DownloadV1V2ImplTest.java @@ -48,6 +48,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayInputStream; + import org.apache.sling.api.resource.Resource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -172,6 +174,26 @@ void testAssetReference_Inline(String resourceType) { String expectedMediaUrl = "/content/dam/sample/file1.pdf/_jcr_content/renditions/original./file1.pdf"; assertEquals(expectedMediaUrl, underTest.getUrl()); + assertEquals("15 KB", underTest.getSize()); + + assertValidMedia(underTest, expectedMediaUrl); + } + + @ParameterizedTest + @ValueSource(strings = { RESOURCE_TYPE_V1, RESOURCE_TYPE_V2 }) + void testAssetReference_Inline_0size(String resourceType) { + Asset asset = context.create().asset(DAM_ROOT + "/file1.pdf", new ByteArrayInputStream(new byte[0]), ContentType.PDF); + + context.currentResource(context.create().resource(page, "download", + PROPERTY_RESOURCE_TYPE, resourceType, + PN_MEDIA_REF_STANDARD, asset.getPath(), + PN_INLINE, true)); + Download underTest = AdaptTo.notNull(context.request(), Download.class); + + String expectedMediaUrl = "/content/dam/sample/file1.pdf/_jcr_content/renditions/original./file1.pdf"; + + assertEquals(expectedMediaUrl, underTest.getUrl()); + assertNull(underTest.getSize()); assertValidMedia(underTest, expectedMediaUrl); } diff --git a/changes.xml b/changes.xml index 5c1f0523..4e61ef7f 100644 --- a/changes.xml +++ b/changes.xml @@ -23,6 +23,12 @@ xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd"> + + + Download (v1/v2): Don't display file size if it is not available. + + + Migrate from wcm.io Handler 1.x to 2.x for details.