Skip to content

Commit

Permalink
Download (v1/v2): Don't display file size if it is not available. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Feb 15, 2024
1 parent a120983 commit 9517586
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<body>

<release version="2.0.2-2.23.2" date="not released">
<action type="fix" dev="sseifert" issue="20">
Download (v1/v2): Don't display file size if it is not available.
</action>
</release>

<release version="2.0.0-2.23.2" date="2024-01-26">
<action type="update" dev="sseifert"><![CDATA[
Version 2.0.0-2.23.2 requires Handler 2.x, See <a href=https://wcm-io.atlassian.net/wiki/x/AYBxsw">Migrate from wcm.io Handler 1.x to 2.x</a> for details.
Expand Down

0 comments on commit 9517586

Please sign in to comment.