From 5a220074362b4880c18330a6d77674cde9da90b7 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Thu, 15 Feb 2024 11:37:02 +0100 Subject: [PATCH 1/2] Download (v1/v2): Don't display file size if it is not available. --- .../core/components/impl/models/v1/DownloadV1V2Impl.java | 4 +++- changes.xml | 6 ++++++ 2 files changed, 9 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..0db561b4 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,9 @@ private void initPropertiesFromMedia() { if (rendition != null) { filename = rendition.getFileName(); format = rendition.getMimeType(); - size = FileUtils.byteCountToDisplaySize(rendition.getFileSize()); + if (rendition.getFileSize() > 0) { + size = FileUtils.byteCountToDisplaySize(rendition.getFileSize()); + } extension = rendition.getFileExtension(); } diff --git a/changes.xml b/changes.xml index 5c1f0523..e16e7149 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. From ed39fdf426a83b240c797afb552e13c4df502afb Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Thu, 15 Feb 2024 11:42:36 +0100 Subject: [PATCH 2/2] add unit tests --- .../impl/models/v1/DownloadV1V2Impl.java | 11 +++++++--- .../impl/models/v1/DownloadV1V2ImplTest.java | 22 +++++++++++++++++++ changes.xml | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) 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 0db561b4..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,9 +130,7 @@ private void initPropertiesFromMedia() { if (rendition != null) { filename = rendition.getFileName(); format = rendition.getMimeType(); - if (rendition.getFileSize() > 0) { - size = FileUtils.byteCountToDisplaySize(rendition.getFileSize()); - } + size = toDisplayFileSize(rendition.getFileSize()); extension = rendition.getFileExtension(); } @@ -153,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 e16e7149..4e61ef7f 100644 --- a/changes.xml +++ b/changes.xml @@ -24,7 +24,7 @@ - + Download (v1/v2): Don't display file size if it is not available.