diff --git a/haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/pkg/PkgImportServiceImplIT.java b/haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/pkg/PkgImportServiceImplIT.java index 97b28158a..dd5bed374 100644 --- a/haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/pkg/PkgImportServiceImplIT.java +++ b/haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/pkg/PkgImportServiceImplIT.java @@ -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; @@ -29,6 +30,7 @@ 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; @@ -36,6 +38,8 @@ @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; @@ -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); - } - /** *
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.
@@ -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; @@ -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 @@ -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(); @@ -233,10 +224,16 @@ public void testImport_payloadLength() throws Exception { )).get(); Assertions.assertThat(pkgVersion.getPayloadLength()).isEqualTo(expectedPayloadLength); + + ListThis will read in the payload into a temporary file. From there it will parse it + * and take up any data from it such as the icon and the length of the download in + * bytes.
+ */ + + private void populateFromPayload(ObjectContext objectContext, PkgVersion persistedPkgVersion) { + persistedPkgVersion.tryGetHpkgURL(ExposureType.INTERNAL_FACING) + .ifPresentOrElse( + u -> populateFromPayload(objectContext, persistedPkgVersion, u), + () -> LOGGER.info( + "no package payload data recorded because there is no " + + "hpkg url for pkg [{}] version [{}]", + persistedPkgVersion.getPkg(), persistedPkgVersion)); + } + + private void populateFromPayload( + ObjectContext objectContext, + PkgVersion persistedPkgVersion, URL url) { + File temporaryFile = null; + + try { + String prefix = persistedPkgVersion.getPkg().getName() + "_" + RandomStringUtils.randomAlphabetic(3) + "_"; + // ^ need to ensure minimum length of the prefix + temporaryFile = File.createTempFile(prefix, ".hpkg"); - if (pkgVersionHpkgURLOptional.isPresent()) { try { - urlHelperService.tryGetPayloadLength(pkgVersionHpkgURLOptional.get()) - .filter(l -> l > 0L) - .ifPresent(persistedPkgVersion::setPayloadLength); - } catch (IOException ioe) { - LOGGER.error("unable to get the payload length for; " + persistedPkgVersion, ioe); + urlHelperService.transferPayloadToFile(url, temporaryFile); } - } else { - LOGGER.info("no package length recorded because there is no " - + "hpkg url for [" + persistedPkgVersion + "]"); + catch (IOException ioe) { + // if we can't download then don't stop the entire import process - just log and carry on. + LOGGER.warn("unable to download from the url [{}] --> [{}]; will ignore", url, temporaryFile); + return; + } + + // the length of the payload is interesting and trivial to capture from + // the data downloaded. + + if (null == persistedPkgVersion.getPayloadLength() + || persistedPkgVersion.getPayloadLength() != temporaryFile.length()) { + persistedPkgVersion.setPayloadLength(temporaryFile.length()); + LOGGER.info("recording new length for [{}] version [{}] of {}bytes", + persistedPkgVersion.getPkg(), persistedPkgVersion, temporaryFile.length()); + } + + // more complex is the capture of the data in the parsed payload data. + + HpkgFileExtractor hpkgFileExtractor; + + try { + hpkgFileExtractor = new HpkgFileExtractor(temporaryFile); + } + catch (Throwable th) { + // if it is not possible to parse the HPKG then log and carry on. + LOGGER.warn("unable to parse the payload from [{}]", url, th); + return; + } + + populateIconFromPayload(objectContext, persistedPkgVersion, hpkgFileExtractor); + } + catch (IOException ioe) { + throw new UncheckedIOException(ioe); + } + finally { + if (null != temporaryFile && temporaryFile.exists()) { + if (temporaryFile.delete()) { + LOGGER.debug("did delete the temporary file"); + } + else { + LOGGER.error("unable to delete the temporary file [{}]", temporaryFile); + } + } + } + } + + private void populateIconFromPayload( + ObjectContext objectContext, PkgVersion persistedPkgVersion, + HpkgFileExtractor hpkgFileExtractor) { + AttributeContext context = hpkgFileExtractor.getTocContext(); + ListThis object represents an object that can extract an Hpkg (Haiku Pkg) file. If you are wanting to
@@ -104,6 +112,15 @@ public AttributeIterator getTocIterator() {
return new AttributeIterator(getTocContext(), tocAttributeOffset);
}
+ public List The attribute type {@link AttributeId#FILE_ATTRIBUTE} has a value
+ * which is a string that defines what sort of attribute it is.