Skip to content

Commit 01c9c13

Browse files
committed
pickup icons from the hpkg data - closes #110
1 parent 13a66a1 commit 01c9c13

File tree

17 files changed

+484
-87
lines changed

17 files changed

+484
-87
lines changed

haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/pkg/PkgImportServiceImplIT.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright 2018-2020, Andrew Lindesay
2+
* Copyright 2018-2021, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

66
package org.haiku.haikudepotserver.pkg;
77

8+
import com.google.common.collect.Iterables;
89
import com.google.common.io.Files;
910
import com.google.common.io.Resources;
1011
import org.apache.cayenne.ObjectContext;
@@ -29,13 +30,16 @@
2930
import javax.annotation.Resource;
3031
import java.io.File;
3132
import java.io.InputStream;
33+
import java.net.URL;
3234
import java.util.Collections;
3335
import java.util.List;
3436
import java.util.Random;
3537

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

41+
private static final String RESOURCE_TEST = "tipster-1.1.1-1-x86_64.hpkg";
42+
3943
@Resource
4044
private PkgImportService pkgImportService;
4145

@@ -51,19 +55,6 @@ public class PkgImportServiceImplIT extends AbstractIntegrationTest {
5155
@Resource
5256
private IntegrationTestSupportService integrationTestSupportService;
5357

54-
private Pkg createPkg(String minor) {
55-
return new Pkg(
56-
"testpkg",
57-
new PkgVersion("1", minor, "3", "4", 5),
58-
PkgArchitecture.X86_64,
59-
null,
60-
Collections.emptyList(),
61-
Collections.emptyList(),
62-
"test-summary-en",
63-
"test-description-en",
64-
null);
65-
}
66-
6758
/**
6859
* <p>When a "_devel" package is imported there is a special behaviour that the localization and the
6960
* icons are copied from the main package over to the "_devel" package.</p>
@@ -171,7 +162,7 @@ public void testImport_develPkgHandling() throws Exception {
171162
*/
172163

173164
@Test
174-
public void testImport_payloadLength() throws Exception {
165+
public void testImport_payloadData() throws Exception {
175166

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

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

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

222-
// check the length on that package is there and is correct.
212+
// check the length on that package is there and is correct and that the
213+
// package icon is loaded in.
223214

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

235226
Assertions.assertThat(pkgVersion.getPayloadLength()).isEqualTo(expectedPayloadLength);
227+
228+
List<PkgIcon> pkgIcons = pkg.getPkgSupplement().getPkgIcons();
229+
Assertions.assertThat(pkgIcons).hasSize(1);
230+
PkgIcon pkgIcon = Iterables.getOnlyElement(pkgIcons);
231+
byte[] actualIconData = pkgIcon.getPkgIconImage().getData();
232+
Assertions.assertThat(actualIconData).hasSize(544);
236233
}
237234
}
238235
finally {
239-
if(null!=repositoryDirectory) {
236+
if (null != repositoryDirectory) {
240237
FileHelper.delete(repositoryDirectory);
241238
}
242239
}
@@ -344,4 +341,17 @@ public void testImport_versionRegressionDeactivatesNewerVersions() {
344341

345342
}
346343

344+
private Pkg createPkg(String minor) {
345+
return new Pkg(
346+
"testpkg",
347+
new PkgVersion("1", minor, "3", "4", 5),
348+
PkgArchitecture.X86_64,
349+
null,
350+
Collections.emptyList(),
351+
Collections.emptyList(),
352+
"test-summary-en",
353+
"test-description-en",
354+
null);
355+
}
356+
347357
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2021, Andrew Lindesay
3+
* Distributed under the terms of the MIT License.
4+
*/
5+
package org.haiku.haikudepotserver.support;
6+
7+
import com.google.common.collect.Iterables;
8+
import com.google.common.io.ByteSource;
9+
import com.google.common.io.Files;
10+
import com.google.common.io.Resources;
11+
import junit.framework.AssertionFailedError;
12+
import org.fest.assertions.Assertions;
13+
import org.haiku.pkg.AttributeContext;
14+
import org.haiku.pkg.HpkgFileExtractor;
15+
import org.haiku.pkg.model.Attribute;
16+
import org.haiku.pkg.model.AttributeId;
17+
import org.junit.Rule;
18+
import org.junit.Test;
19+
import org.junit.rules.TemporaryFolder;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.List;
24+
25+
public class HpkgHelperTest {
26+
27+
private static final String RESOURCE_TEST = "tipster-1.1.1-1-x86_64.hpkg";
28+
29+
private static final int[] HVIF_MAGIC = {
30+
0x6e, 0x63, 0x69, 0x66
31+
};
32+
33+
@Rule
34+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
35+
36+
@Test
37+
public void testFindIconAttributesFromAppExecutableDirEntries() throws Exception {
38+
// GIVEN
39+
File file = prepareTestFile(RESOURCE_TEST);
40+
HpkgFileExtractor fileExtractor = new HpkgFileExtractor(file);
41+
AttributeContext tocContext = fileExtractor.getTocContext();
42+
43+
// WHEN
44+
List<Attribute> attributes = HpkgHelper.findIconAttributesFromExecutableDirEntries(
45+
tocContext, fileExtractor.getToc());
46+
47+
// THEN
48+
Assertions.assertThat(attributes).hasSize(1);
49+
Attribute iconA = Iterables.getOnlyElement(attributes);
50+
Attribute iconDataA = iconA.getChildAttribute(AttributeId.DATA);
51+
ByteSource byteSource = (ByteSource) iconDataA.getValue(tocContext);
52+
byte[] data = byteSource.read();
53+
Assertions.assertThat(data).hasSize(544);
54+
assertIsHvif(data);
55+
}
56+
57+
File prepareTestFile(String resource) throws IOException {
58+
byte[] payload = Resources.toByteArray(Resources.getResource(resource));
59+
File temporaryFile = temporaryFolder.newFile(resource);
60+
Files.write(payload, temporaryFile);
61+
return temporaryFile;
62+
}
63+
64+
private void assertIsHvif(byte[] payload) {
65+
Assertions.assertThat(payload.length).isGreaterThan(HVIF_MAGIC.length);
66+
for (int i = 0; i < HVIF_MAGIC.length; i++) {
67+
if ((0xff & payload[i]) != HVIF_MAGIC[i]) {
68+
throw new AssertionFailedError("mismatch on the magic in the data payload");
69+
}
70+
}
71+
}
72+
73+
}

haikudepotserver-core-test/src/test/resources/README.TXT

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ The file "sample-repo.hpkr" was obtained from;
22

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

5-
2013-09-30
5+
2013-09-30
6+
7+
---
8+
9+
The file "tipster-1.1.1-1-x86_64.hpkg" was obtained from;
10+
11+
https://eu.hpkg.haiku-os.org/haikuports/master/x86_64/current/packages/tipster-1.1.1-1-x86_64.hpkg
12+
13+
2021-02-08
Binary file not shown.

haikudepotserver-core/src/main/java/org/haiku/haikudepotserver/graphics/ImageHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016, Andrew Lindesay
2+
* Copyright 2013-2021, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

@@ -17,15 +17,15 @@ public class ImageHelper {
1717

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

20-
private int HVIF_MAGIC[] = {
20+
private static final int[] HVIF_MAGIC = {
2121
0x6e, 0x63, 0x69, 0x66
2222
};
2323

24-
private int PNG_MAGIC[] = {
24+
private static final int[] PNG_MAGIC = {
2525
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A
2626
};
2727

28-
private int PNG_IHDR[] = {
28+
private static final int[] PNG_IHDR = {
2929
0x49, 0x48, 0x44, 0x52
3030
};
3131

haikudepotserver-core/src/main/java/org/haiku/haikudepotserver/pkg/PkgIconServiceImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019, Andrew Lindesay
2+
* Copyright 2018-2021, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

@@ -34,10 +34,10 @@
3434
@Service
3535
public class PkgIconServiceImpl implements PkgIconService {
3636

37-
protected static Logger LOGGER = LoggerFactory.getLogger(PkgIconServiceImpl.class);
37+
protected static final Logger LOGGER = LoggerFactory.getLogger(PkgIconServiceImpl.class);
3838

3939
@SuppressWarnings("FieldCanBeLocal")
40-
private static int ICON_SIZE_LIMIT = 100 * 1024; // 100k
40+
private static final int ICON_SIZE_LIMIT = 100 * 1024; // 100k
4141

4242
private final RenderedPkgIconRepository renderedPkgIconRepository;
4343
private final PngOptimizationService pngOptimizationService;
@@ -157,16 +157,20 @@ public PkgIcon storePkgIconImage(
157157
pkgIconOptional = Optional.of(pkgIcon);
158158
}
159159

160-
pkgIconImage.setData(imageData);
161-
pkgSupplement.setModifyTimestamp();
162-
pkgSupplement.setIconModifyTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
163-
renderedPkgIconRepository.evict(context, pkgSupplement);
160+
if (pkgIconImage.getData() == null || !Arrays.equals(pkgIconImage.getData(), imageData)) {
161+
pkgIconImage.setData(imageData);
162+
pkgSupplement.setModifyTimestamp();
163+
pkgSupplement.setIconModifyTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
164+
renderedPkgIconRepository.evict(context, pkgSupplement);
164165

165-
if (null != size) {
166-
LOGGER.info("the icon {}px for package {} has been updated", size, pkgSupplement.getBasePkgName());
166+
if (null != size) {
167+
LOGGER.info("the icon {}px for package [{}] has been updated", size, pkgSupplement.getBasePkgName());
168+
} else {
169+
LOGGER.info("the icon for package [{}] has been updated", pkgSupplement.getBasePkgName());
170+
}
167171
}
168172
else {
169-
LOGGER.info("the icon for package {} has been updated", pkgSupplement.getBasePkgName());
173+
LOGGER.info("no change to package icon for [{}] ", pkgSupplement.getBasePkgName());
170174
}
171175

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

185189
return codes
186190
.stream()
187-
.map(c -> MediaType.tryGetByCode(context, c).get())
191+
.map(c -> MediaType.getByCode(context, c))
188192
.collect(Collectors.toList());
189193

190194
}

0 commit comments

Comments
 (0)