Skip to content

Commit

Permalink
pickup icons from the hpkg data - closes #110
Browse files Browse the repository at this point in the history
  • Loading branch information
andponlin committed Feb 17, 2021
1 parent 13a66a1 commit 01c9c13
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* Copyright 2018-2020, Andrew Lindesay
* Copyright 2018-2021, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

package org.haiku.haikudepotserver.pkg;

import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import org.apache.cayenne.ObjectContext;
Expand All @@ -29,13 +30,16 @@
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@ContextConfiguration(classes = TestConfig.class)
public class PkgImportServiceImplIT extends AbstractIntegrationTest {

private static final String RESOURCE_TEST = "tipster-1.1.1-1-x86_64.hpkg";

@Resource
private PkgImportService pkgImportService;

Expand All @@ -51,19 +55,6 @@ public class PkgImportServiceImplIT extends AbstractIntegrationTest {
@Resource
private IntegrationTestSupportService integrationTestSupportService;

private Pkg createPkg(String minor) {
return new Pkg(
"testpkg",
new PkgVersion("1", minor, "3", "4", 5),
PkgArchitecture.X86_64,
null,
Collections.emptyList(),
Collections.emptyList(),
"test-summary-en",
"test-description-en",
null);
}

/**
* <p>When a "_devel" package is imported there is a special behaviour that the localization and the
* icons are copied from the main package over to the "_devel" package.</p>
Expand Down Expand Up @@ -171,7 +162,7 @@ public void testImport_develPkgHandling() throws Exception {
*/

@Test
public void testImport_payloadLength() throws Exception {
public void testImport_payloadData() throws Exception {

File repositoryDirectory = null;
int expectedPayloadLength;
Expand All @@ -193,11 +184,10 @@ public void testImport_payloadLength() throws Exception {
throw new IllegalStateException("unable to create the on-disk repository");
}

Random random = new Random(System.currentTimeMillis());
File fileF = new File(repositoryDirectory, "testpkg-1.3.3~4-5-x86_64.hpkg");
byte[] buffer = new byte[1000 + (Math.abs(random.nextInt()) % 10*1000)];
Files.write(buffer,fileF);
expectedPayloadLength = buffer.length;
byte[] payload = Resources.toByteArray(Resources.getResource(RESOURCE_TEST));
Files.write(payload, fileF);
expectedPayloadLength = payload.length;
}

// now load the next package version in
Expand All @@ -219,7 +209,8 @@ public void testImport_payloadLength() throws Exception {
context.commitChanges();
}

// check the length on that package is there and is correct.
// check the length on that package is there and is correct and that the
// package icon is loaded in.

{
ObjectContext context = serverRuntime.newContext();
Expand All @@ -233,10 +224,16 @@ public void testImport_payloadLength() throws Exception {
)).get();

Assertions.assertThat(pkgVersion.getPayloadLength()).isEqualTo(expectedPayloadLength);

List<PkgIcon> pkgIcons = pkg.getPkgSupplement().getPkgIcons();
Assertions.assertThat(pkgIcons).hasSize(1);
PkgIcon pkgIcon = Iterables.getOnlyElement(pkgIcons);
byte[] actualIconData = pkgIcon.getPkgIconImage().getData();
Assertions.assertThat(actualIconData).hasSize(544);
}
}
finally {
if(null!=repositoryDirectory) {
if (null != repositoryDirectory) {
FileHelper.delete(repositoryDirectory);
}
}
Expand Down Expand Up @@ -344,4 +341,17 @@ public void testImport_versionRegressionDeactivatesNewerVersions() {

}

private Pkg createPkg(String minor) {
return new Pkg(
"testpkg",
new PkgVersion("1", minor, "3", "4", 5),
PkgArchitecture.X86_64,
null,
Collections.emptyList(),
Collections.emptyList(),
"test-summary-en",
"test-description-en",
null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2021, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/
package org.haiku.haikudepotserver.support;

import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import junit.framework.AssertionFailedError;
import org.fest.assertions.Assertions;
import org.haiku.pkg.AttributeContext;
import org.haiku.pkg.HpkgFileExtractor;
import org.haiku.pkg.model.Attribute;
import org.haiku.pkg.model.AttributeId;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class HpkgHelperTest {

private static final String RESOURCE_TEST = "tipster-1.1.1-1-x86_64.hpkg";

private static final int[] HVIF_MAGIC = {
0x6e, 0x63, 0x69, 0x66
};

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testFindIconAttributesFromAppExecutableDirEntries() throws Exception {
// GIVEN
File file = prepareTestFile(RESOURCE_TEST);
HpkgFileExtractor fileExtractor = new HpkgFileExtractor(file);
AttributeContext tocContext = fileExtractor.getTocContext();

// WHEN
List<Attribute> attributes = HpkgHelper.findIconAttributesFromExecutableDirEntries(
tocContext, fileExtractor.getToc());

// THEN
Assertions.assertThat(attributes).hasSize(1);
Attribute iconA = Iterables.getOnlyElement(attributes);
Attribute iconDataA = iconA.getChildAttribute(AttributeId.DATA);
ByteSource byteSource = (ByteSource) iconDataA.getValue(tocContext);
byte[] data = byteSource.read();
Assertions.assertThat(data).hasSize(544);
assertIsHvif(data);
}

File prepareTestFile(String resource) throws IOException {
byte[] payload = Resources.toByteArray(Resources.getResource(resource));
File temporaryFile = temporaryFolder.newFile(resource);
Files.write(payload, temporaryFile);
return temporaryFile;
}

private void assertIsHvif(byte[] payload) {
Assertions.assertThat(payload.length).isGreaterThan(HVIF_MAGIC.length);
for (int i = 0; i < HVIF_MAGIC.length; i++) {
if ((0xff & payload[i]) != HVIF_MAGIC[i]) {
throw new AssertionFailedError("mismatch on the magic in the data payload");
}
}
}

}
10 changes: 9 additions & 1 deletion haikudepotserver-core-test/src/test/resources/README.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ The file "sample-repo.hpkr" was obtained from;

http://haiku-files.org/files/repo/9818164862edcbf69404a90267090b9d595908a11941951e904dfc6244c3d566/repo

2013-09-30
2013-09-30

---

The file "tipster-1.1.1-1-x86_64.hpkg" was obtained from;

https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/tipster-1.1.1-1-x86_64.hpkg

2021-02-08
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2016, Andrew Lindesay
* Copyright 2013-2021, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand All @@ -17,15 +17,15 @@ public class ImageHelper {

protected static Logger LOGGER = LoggerFactory.getLogger(ImageHelper.class);

private int HVIF_MAGIC[] = {
private static final int[] HVIF_MAGIC = {
0x6e, 0x63, 0x69, 0x66
};

private int PNG_MAGIC[] = {
private static final int[] PNG_MAGIC = {
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A
};

private int PNG_IHDR[] = {
private static final int[] PNG_IHDR = {
0x49, 0x48, 0x44, 0x52
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019, Andrew Lindesay
* Copyright 2018-2021, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand Down Expand Up @@ -34,10 +34,10 @@
@Service
public class PkgIconServiceImpl implements PkgIconService {

protected static Logger LOGGER = LoggerFactory.getLogger(PkgIconServiceImpl.class);
protected static final Logger LOGGER = LoggerFactory.getLogger(PkgIconServiceImpl.class);

@SuppressWarnings("FieldCanBeLocal")
private static int ICON_SIZE_LIMIT = 100 * 1024; // 100k
private static final int ICON_SIZE_LIMIT = 100 * 1024; // 100k

private final RenderedPkgIconRepository renderedPkgIconRepository;
private final PngOptimizationService pngOptimizationService;
Expand Down Expand Up @@ -157,16 +157,20 @@ public PkgIcon storePkgIconImage(
pkgIconOptional = Optional.of(pkgIcon);
}

pkgIconImage.setData(imageData);
pkgSupplement.setModifyTimestamp();
pkgSupplement.setIconModifyTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
renderedPkgIconRepository.evict(context, pkgSupplement);
if (pkgIconImage.getData() == null || !Arrays.equals(pkgIconImage.getData(), imageData)) {
pkgIconImage.setData(imageData);
pkgSupplement.setModifyTimestamp();
pkgSupplement.setIconModifyTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
renderedPkgIconRepository.evict(context, pkgSupplement);

if (null != size) {
LOGGER.info("the icon {}px for package {} has been updated", size, pkgSupplement.getBasePkgName());
if (null != size) {
LOGGER.info("the icon {}px for package [{}] has been updated", size, pkgSupplement.getBasePkgName());
} else {
LOGGER.info("the icon for package [{}] has been updated", pkgSupplement.getBasePkgName());
}
}
else {
LOGGER.info("the icon for package {} has been updated", pkgSupplement.getBasePkgName());
LOGGER.info("no change to package icon for [{}] ", pkgSupplement.getBasePkgName());
}

return pkgIconOptional.orElseThrow(IllegalStateException::new);
Expand All @@ -184,7 +188,7 @@ private List<MediaType> getInUsePkgIconMediaTypes(final ObjectContext context) {

return codes
.stream()
.map(c -> MediaType.tryGetByCode(context, c).get())
.map(c -> MediaType.getByCode(context, c))
.collect(Collectors.toList());

}
Expand Down
Loading

0 comments on commit 01c9c13

Please sign in to comment.