Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download (v1/v2): Don't display file size if it is not available. #20

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading