use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotOptimizationJobRunner method run.
@Override
public void run(JobService jobService, PkgScreenshotOptimizationJobSpecification specification) throws JobRunnerException {
Preconditions.checkArgument(null != jobService);
Preconditions.checkArgument(null != specification);
long startMs = System.currentTimeMillis();
LOGGER.info("will optimize {} screenshot images", specification.getPkgScreenshotCodes().size());
for (String pkgScreenshotCode : specification.getPkgScreenshotCodes()) {
ObjectContext context = serverRuntime.newContext();
Optional<PkgScreenshot> pkgScreenshotOptional = PkgScreenshot.tryGetByCode(context, pkgScreenshotCode);
if (pkgScreenshotOptional.isPresent()) {
try {
if (screenshotService.optimizeScreenshot(context, pkgScreenshotOptional.get())) {
context.commitChanges();
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
} catch (BadPkgScreenshotException bpse) {
throw new JobRunnerException("unable to process a screenshot image", bpse);
}
}
LOGGER.info("did optimize {} screenshot images in {}ms", specification.getPkgScreenshotCodes().size(), System.currentTimeMillis() - startMs);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotServiceImpl method reorderPkgScreenshotsByHashSha256s.
private void reorderPkgScreenshotsByHashSha256s(PkgSupplement pkgSupplement, List<String> hashSha256s) {
List<PkgScreenshot> screenshots = new ArrayList<>(pkgSupplement.getPkgScreenshots());
screenshots.sort((o1, o2) -> {
int o1i = hashSha256s.indexOf(o1.getHashSha256());
int o2i = hashSha256s.indexOf(o2.getHashSha256());
if (-1 == o1i && -1 == o2i) {
return o1.getCode().compareTo(o2.getCode());
}
if (-1 == o1i) {
o1i = Integer.MAX_VALUE;
}
if (-1 == o2i) {
o2i = Integer.MAX_VALUE;
}
return Integer.compare(o1i, o2i);
});
for (int i = 0; i < screenshots.size(); i++) {
PkgScreenshot pkgScreenshot = screenshots.get(i);
pkgScreenshot.setOrdering(i + 1);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgApiImpl method getPkgScreenshot.
@Override
public GetPkgScreenshotResult getPkgScreenshot(GetPkgScreenshotRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkNotNull(request.code);
final ObjectContext context = serverRuntime.newContext();
Optional<PkgScreenshot> pkgScreenshotOptional = PkgScreenshot.tryGetByCode(context, request.code);
if (pkgScreenshotOptional.isEmpty()) {
throw new ObjectNotFoundException(PkgScreenshot.class.getSimpleName(), request.code);
}
GetPkgScreenshotResult result = new GetPkgScreenshotResult();
result.code = pkgScreenshotOptional.get().getCode();
result.height = pkgScreenshotOptional.get().getHeight();
result.width = pkgScreenshotOptional.get().getWidth();
result.length = pkgScreenshotOptional.get().getLength();
return result;
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunnerIT method assertScreenshotHashes.
private void assertScreenshotHashes(String[] expectedSha1Sums) {
ObjectContext context = serverRuntime.newContext();
Pkg pkg1 = Pkg.getByName(context, "pkg1");
PkgSupplement pkg1Supplement = pkg1.getPkgSupplement();
Assertions.assertThat(pkg1Supplement.getPkgScreenshots()).hasSize(expectedSha1Sums.length);
List<PkgScreenshot> screenshots = pkg1Supplement.getSortedPkgScreenshots();
for (int i = 0; i < screenshots.size(); i++) {
PkgScreenshot screenshot = screenshots.get(i);
String actualSha1Sum = Hashing.sha1().hashBytes(screenshot.getPkgScreenshotImage().getData()).toString();
String expectedSha1Sum = expectedSha1Sums[i];
Assertions.assertThat(actualSha1Sum).isEqualTo(expectedSha1Sum);
}
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotServiceImpl method storePkgScreenshotImage.
/**
* <p>Note that if the screenshot is already stored then this method will simply return that screenshot.</p>
*
* @param ordering can be NULL; in which case the screenshot will come at the end.
*/
@Override
public PkgScreenshot storePkgScreenshotImage(InputStream input, ObjectContext context, PkgSupplement pkgSupplement, Integer ordering) throws IOException, BadPkgScreenshotException {
Preconditions.checkArgument(null != input, "the input must be provided");
Preconditions.checkArgument(null != context, "the context must be provided");
Preconditions.checkArgument(null != pkgSupplement, "the pkg supplement must be provided");
byte[] pngData = ByteStreams.toByteArray(new BoundedInputStream(input, SCREENSHOT_SIZE_LIMIT));
ImageHelper.Size size = imageHelper.derivePngSize(pngData);
String hashSha256 = HASH_FUNCTION.hashBytes(pngData).toString();
if (null == size) {
LOGGER.warn("attempt to store a screenshot image that is not a png");
throw new BadPkgScreenshotException();
}
if (size.height > SCREENSHOT_SIDE_LIMIT || size.width > SCREENSHOT_SIDE_LIMIT) {
LOGGER.warn("attempt to store a screenshot image that is too large; " + size.toString());
throw new BadPkgScreenshotException();
}
for (PkgScreenshot pkgScreenshot : pkgSupplement.getPkgScreenshots()) {
if (pkgScreenshot.getHashSha256().equals(hashSha256)) {
LOGGER.warn("attempt to store a screenshot image that is already stored for this package");
return pkgScreenshot;
}
}
MediaType png = MediaType.tryGetByCode(context, com.google.common.net.MediaType.PNG.toString()).get();
// now we need to know the largest ordering so we can add this one at the end of the orderings
// such that it is the next one in the list.
int actualOrdering = null == ordering ? pkgSupplement.getHighestPkgScreenshotOrdering().orElse(0) + 1 : ordering;
PkgScreenshot screenshot = context.newObject(PkgScreenshot.class);
screenshot.setCode(UUID.randomUUID().toString());
screenshot.setOrdering(actualOrdering);
screenshot.setHeight(size.height);
screenshot.setWidth(size.width);
screenshot.setLength(pngData.length);
screenshot.setHashSha256(hashSha256);
pkgSupplement.addToManyTarget(PkgSupplement.PKG_SCREENSHOTS.getName(), screenshot, true);
PkgScreenshotImage screenshotImage = context.newObject(PkgScreenshotImage.class);
screenshotImage.setMediaType(png);
screenshotImage.setData(pngData);
screenshot.addToManyTarget(PkgScreenshot.PKG_SCREENSHOT_IMAGES.getName(), screenshotImage, true);
pkgSupplement.setModifyTimestamp();
LOGGER.info("a screenshot #{} has been added to package [{}] ({})", actualOrdering, pkgSupplement.getBasePkgName(), screenshot.getCode());
return screenshot;
}
Aggregations