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());
}
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;
}
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;
}
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));
}
Aggregations