Search in sources :

Example 1 with PkgScreenshot

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);
    }
}
Also used : BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) UncheckedIOException(java.io.UncheckedIOException) ObjectContext(org.apache.cayenne.ObjectContext) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 2 with PkgScreenshot

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);
    }
}
Also used : org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot(org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot)

Example 3 with PkgScreenshot

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;
}
Also used : PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 4 with PkgScreenshot

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);
    }
}
Also used : PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) ObjectContext(org.apache.cayenne.ObjectContext) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg)

Example 5 with PkgScreenshot

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;
}
Also used : BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) PkgScreenshotImage(org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage) BoundedInputStream(org.apache.commons.compress.utils.BoundedInputStream) org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot(org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) MediaType(org.haiku.haikudepotserver.dataobjects.MediaType) ImageHelper(org.haiku.haikudepotserver.graphics.ImageHelper)

Aggregations

PkgScreenshot (org.haiku.haikudepotserver.dataobjects.PkgScreenshot)12 ObjectContext (org.apache.cayenne.ObjectContext)10 PkgScreenshotImage (org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage)4 BadPkgScreenshotException (org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException)4 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)3 JobRunnerException (org.haiku.haikudepotserver.job.model.JobRunnerException)3 Preconditions (com.google.common.base.Preconditions)2 HashCode (com.google.common.hash.HashCode)2 HashFunction (com.google.common.hash.HashFunction)2 Hashing (com.google.common.hash.Hashing)2 HashingInputStream (com.google.common.hash.HashingInputStream)2 ByteSource (com.google.common.io.ByteSource)2 ByteStreams (com.google.common.io.ByteStreams)2 MediaType (com.google.common.net.MediaType)2 CSVWriter (com.opencsv.CSVWriter)2 java.io (java.io)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2