Search in sources :

Example 1 with HpkgFileExtractor

use of org.haiku.pkg.HpkgFileExtractor in project haikudepotserver by haiku.

the class PkgImportServiceImpl method populateFromPayload.

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");
        try {
            urlHelperService.transferPayloadToFile(url, temporaryFile);
        } 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;
        }
        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);
            }
        }
    }
}
Also used : HpkgFileExtractor(org.haiku.pkg.HpkgFileExtractor) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File)

Example 2 with HpkgFileExtractor

use of org.haiku.pkg.HpkgFileExtractor in project haikudepotserver by haiku.

the class AttributeDumpTool method runHpkg.

private void runHpkg() throws IOException {
    try (HpkgFileExtractor hpkgFileExtractor = new HpkgFileExtractor(hpkFile);
        OutputStreamWriter streamWriter = new OutputStreamWriter(System.out);
        AttributeWriter attributeWriter = new AttributeWriter(streamWriter)) {
        writeHeader(streamWriter, "package attributes");
        attributeWriter.write(hpkgFileExtractor.getPackageAttributesIterator());
        writeHeader(streamWriter, "toc");
        attributeWriter.write(hpkgFileExtractor.getTocIterator());
        attributeWriter.flush();
    }
}
Also used : AttributeWriter(org.haiku.pkg.output.AttributeWriter) HpkgFileExtractor(org.haiku.pkg.HpkgFileExtractor)

Example 3 with HpkgFileExtractor

use of org.haiku.pkg.HpkgFileExtractor in project haikudepotserver by haiku.

the class HpkgHelperTest method testFindIconAttributesFromAppExecutableDirEntries.

@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);
}
Also used : AttributeContext(org.haiku.pkg.AttributeContext) Attribute(org.haiku.pkg.model.Attribute) ByteSource(com.google.common.io.ByteSource) HpkgFileExtractor(org.haiku.pkg.HpkgFileExtractor) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

HpkgFileExtractor (org.haiku.pkg.HpkgFileExtractor)3 File (java.io.File)2 ByteSource (com.google.common.io.ByteSource)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 AttributeContext (org.haiku.pkg.AttributeContext)1 Attribute (org.haiku.pkg.model.Attribute)1 AttributeWriter (org.haiku.pkg.output.AttributeWriter)1 Test (org.junit.jupiter.api.Test)1