Search in sources :

Example 1 with MediaType

use of org.haiku.haikudepotserver.dataobjects.MediaType in project haikudepotserver by haiku.

the class PkgIconServiceImpl method getInUsePkgIconMediaTypes.

private List<MediaType> getInUsePkgIconMediaTypes(final ObjectContext context) {
    EJBQLQuery query = new EJBQLQuery(String.join(" ", "SELECT", "DISTINCT pi." + PkgIcon.MEDIA_TYPE.getName() + "." + MediaType.CODE.getName(), "FROM", PkgIcon.class.getSimpleName(), "pi"));
    final List<String> codes = (List<String>) context.performQuery(query);
    return codes.stream().map(c -> MediaType.getByCode(context, c)).collect(Collectors.toList());
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) PkgIconService(org.haiku.haikudepotserver.pkg.model.PkgIconService) ObjectContext(org.apache.cayenne.ObjectContext) PkgIcon(org.haiku.haikudepotserver.dataobjects.PkgIcon) java.util(java.util) Logger(org.slf4j.Logger) BoundedInputStream(org.apache.commons.compress.utils.BoundedInputStream) ImageHelper(org.haiku.haikudepotserver.graphics.ImageHelper) PkgIconImage(org.haiku.haikudepotserver.dataobjects.PkgIconImage) BadPkgIconException(org.haiku.haikudepotserver.pkg.model.BadPkgIconException) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) PkgIconConfiguration(org.haiku.haikudepotserver.pkg.model.PkgIconConfiguration) Service(org.springframework.stereotype.Service) ByteStreams(com.google.common.io.ByteStreams) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Clock(java.time.Clock) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) DataObject(org.apache.cayenne.DataObject) MediaType(org.haiku.haikudepotserver.dataobjects.MediaType) PngOptimizationService(org.haiku.haikudepotserver.graphics.bitmap.PngOptimizationService) InputStream(java.io.InputStream) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery)

Example 2 with MediaType

use of org.haiku.haikudepotserver.dataobjects.MediaType in project haikudepotserver by haiku.

the class PkgIconServiceImpl method getInUsePkgIconConfigurations.

@Override
public List<PkgIconConfiguration> getInUsePkgIconConfigurations(ObjectContext objectContext) {
    Preconditions.checkArgument(null != objectContext, "the object context must be supplied");
    List<PkgIconConfiguration> result = new ArrayList<>();
    for (MediaType mediaType : getInUsePkgIconMediaTypes(objectContext)) {
        List<Integer> sizes = getInUsePkgIconSizes(objectContext, mediaType);
        if (sizes.isEmpty()) {
            result.add(new PkgIconConfiguration(mediaType, null));
        } else {
            for (Integer size : sizes) {
                result.add(new PkgIconConfiguration(mediaType, size));
            }
        }
    }
    Collections.sort(result);
    return result;
}
Also used : PkgIconConfiguration(org.haiku.haikudepotserver.pkg.model.PkgIconConfiguration) MediaType(org.haiku.haikudepotserver.dataobjects.MediaType)

Example 3 with MediaType

use of org.haiku.haikudepotserver.dataobjects.MediaType 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)

Example 4 with MediaType

use of org.haiku.haikudepotserver.dataobjects.MediaType in project haikudepotserver by haiku.

the class PkgIconConfigurationTest method provideComparisonExamples.

public static Stream<Triple<PkgIconConfiguration, PkgIconConfiguration, Integer>> provideComparisonExamples() {
    MediaType mediaTypeA = new MediaType();
    MediaType mediaTypeB = new MediaType();
    mediaTypeA.writeProperty("code", "a");
    mediaTypeB.writeProperty("code", "b");
    return Stream.of(ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, 32), new PkgIconConfiguration(mediaTypeA, 16), -1), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, 16), new PkgIconConfiguration(mediaTypeA, 32), 1), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, 16), new PkgIconConfiguration(mediaTypeA, 16), 0), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, null), new PkgIconConfiguration(mediaTypeA, 32), 1), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, 32), new PkgIconConfiguration(mediaTypeA, null), -1), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeB, 32), new PkgIconConfiguration(mediaTypeA, 32), -1), ImmutableTriple.of(new PkgIconConfiguration(mediaTypeA, 32), new PkgIconConfiguration(mediaTypeB, 32), 1));
}
Also used : MediaType(org.haiku.haikudepotserver.dataobjects.MediaType)

Aggregations

MediaType (org.haiku.haikudepotserver.dataobjects.MediaType)4 BoundedInputStream (org.apache.commons.compress.utils.BoundedInputStream)2 ImageHelper (org.haiku.haikudepotserver.graphics.ImageHelper)2 PkgIconConfiguration (org.haiku.haikudepotserver.pkg.model.PkgIconConfiguration)2 Preconditions (com.google.common.base.Preconditions)1 ByteStreams (com.google.common.io.ByteStreams)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Clock (java.time.Clock)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 DataObject (org.apache.cayenne.DataObject)1 ObjectContext (org.apache.cayenne.ObjectContext)1 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)1 ObjectSelect (org.apache.cayenne.query.ObjectSelect)1 PkgIcon (org.haiku.haikudepotserver.dataobjects.PkgIcon)1 PkgIconImage (org.haiku.haikudepotserver.dataobjects.PkgIconImage)1 PkgScreenshot (org.haiku.haikudepotserver.dataobjects.PkgScreenshot)1 PkgScreenshotImage (org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage)1 PkgSupplement (org.haiku.haikudepotserver.dataobjects.PkgSupplement)1