use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunner method importScreenshotsFromArchiveAndReport.
/**
* <p>If this screenshot coming in from the archive does not exist persisted then load it in.</p>
*/
private void importScreenshotsFromArchiveAndReport(CSVWriter writer, ScreenshotImportMetadatas data, ArchiveInputStream archiveInputStream, ArchiveEntry archiveEntry, String pkgName, int order) {
String[] row = { // path
archiveEntry.getName(), // pkg
pkgName, // action
"", // message
"", // code
"" };
if (data.isNotFound()) {
row[CSV_COLUMN_ACTION] = Action.NOTFOUND.name();
} else {
FromArchiveScreenshotMetadata fromArchiveScreenshotMetadata = data.getFromArchiveScreenshots().stream().filter((as) -> as.getLength() == archiveEntry.getSize()).filter((as) -> as.getOrder() == order).findAny().orElseThrow(() -> new IllegalStateException("unable to find the from-archive screenshot metadata"));
Optional<ExistingScreenshotMetadata> existingScreenshotMetadata = data.getExistingScreenshots().stream().filter((es) -> archiveEntry.getSize() == es.getLength()).filter((es) -> fromArchiveScreenshotMetadata.getDataHash().equals(es.getDataHash())).findAny();
if (existingScreenshotMetadata.isPresent()) {
row[CSV_COLUMN_ACTION] = Action.PRESENT.name();
row[CSV_COLUMN_CODE] = existingScreenshotMetadata.get().getCode();
} else {
ObjectContext context = serverRuntime.newContext();
try {
PkgScreenshot screenshot = pkgScreenshotService.storePkgScreenshotImage(archiveInputStream, context, Pkg.getByName(context, pkgName).getPkgSupplement(), fromArchiveScreenshotMetadata.getDerivedOrder());
row[CSV_COLUMN_CODE] = screenshot.getCode();
row[CSV_COLUMN_ACTION] = Action.ADDED.name();
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
} catch (BadPkgScreenshotException e) {
row[CSV_COLUMN_ACTION] = Action.INVALID.name();
row[CSV_COLUMN_MESSAGE] = e.getMessage();
}
context.commitChanges();
}
}
writer.writeNext(row);
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunner method deletePersistedScreenshotsThatAreNotPresentInArchiveAndReport.
private int deletePersistedScreenshotsThatAreNotPresentInArchiveAndReport(CSVWriter writer, ScreenshotImportMetadatas metadata, ExistingScreenshotMetadata existingScreenshot) {
boolean fromArchiveScreenshotMatches = metadata.getFromArchiveScreenshots().stream().filter((as) -> as.getLength() == existingScreenshot.getLength()).anyMatch((as) -> as.getDataHash().equals(existingScreenshot.getDataHash()));
if (!fromArchiveScreenshotMatches) {
ObjectContext context = serverRuntime.newContext();
PkgScreenshot pkgScreenshot = PkgScreenshot.getByCode(context, existingScreenshot.getCode());
String[] row = new String[] { "", pkgScreenshot.getPkgSupplement().getBasePkgName(), Action.REMOVED.name(), "", pkgScreenshot.getCode() };
pkgScreenshotService.deleteScreenshot(context, pkgScreenshot);
writer.writeNext(row);
// job-length txn so won't *actually* be committed here.
context.commitChanges();
return 1;
}
return 0;
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotImportArchiveJobRunner method generateHashCode.
private HashCode generateHashCode(String pkgScreenshotCode) {
ObjectContext context = serverRuntime.newContext();
PkgScreenshot pkgScreenshot = PkgScreenshot.getByCode(context, pkgScreenshotCode);
PkgScreenshotImage pkgScreenshotImage = pkgScreenshot.getPkgScreenshotImage();
return HASH_FUNCTION.hashBytes(pkgScreenshotImage.getData());
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgApiImpl method removePkgScreenshot.
@Override
public RemovePkgScreenshotResult removePkgScreenshot(RemovePkgScreenshotRequest removePkgScreenshotRequest) {
Preconditions.checkNotNull(removePkgScreenshotRequest);
Preconditions.checkNotNull(removePkgScreenshotRequest.code);
final ObjectContext context = serverRuntime.newContext();
Optional<PkgScreenshot> screenshotOptional = PkgScreenshot.tryGetByCode(context, removePkgScreenshotRequest.code);
if (screenshotOptional.isEmpty()) {
throw new ObjectNotFoundException(PkgScreenshot.class.getSimpleName(), removePkgScreenshotRequest.code);
}
if (screenshotOptional.get().getPkgSupplement().getPkgs().stream().noneMatch(p -> permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), p, Permission.PKG_EDITSCREENSHOT))) {
throw new AccessDeniedException("unable to remove the package screenshot for package");
}
pkgScreenshotService.deleteScreenshot(context, screenshotOptional.get());
context.commitChanges();
LOGGER.info("did remove the screenshot {}", removePkgScreenshotRequest.code);
return new RemovePkgScreenshotResult();
}
use of org.haiku.haikudepotserver.dataobjects.PkgScreenshot in project haikudepotserver by haiku.
the class PkgScreenshotController method handleHeadOrGet.
private void handleHeadOrGet(RequestMethod requestMethod, HttpServletResponse response, Integer targetWidth, Integer targetHeight, String format, String screenshotCode) throws IOException {
if (targetWidth <= 0 || targetWidth > SCREENSHOT_SIDE_LIMIT) {
throw new BadSize();
}
if (targetHeight <= 0 || targetHeight > SCREENSHOT_SIDE_LIMIT) {
throw new BadSize();
}
if (Strings.isNullOrEmpty(screenshotCode)) {
throw new MissingScreenshotCode();
}
if (Strings.isNullOrEmpty(format) || !"png".equals(format)) {
throw new MissingOrBadFormat();
}
ObjectContext context = serverRuntime.newContext();
PkgScreenshot screenshot = PkgScreenshot.tryGetByCode(context, screenshotCode).orElseThrow(ScreenshotNotFound::new);
response.setContentType(MediaType.PNG.toString());
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=3600");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, screenshot.getPkgSupplement().getLatestPkgModifyTimestampSecondAccuracy().getTime());
switch(requestMethod) {
case HEAD:
ByteCounterOutputStream byteCounter = new ByteCounterOutputStream(ByteStreams.nullOutputStream());
pkgScreenshotService.writePkgScreenshotImage(byteCounter, context, screenshot, targetWidth, targetHeight);
response.setHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(byteCounter.getCounter()));
break;
case GET:
pkgScreenshotService.writePkgScreenshotImage(response.getOutputStream(), context, screenshot, targetWidth, targetHeight);
break;
default:
throw new IllegalStateException("unhandled request method; " + requestMethod);
}
}
Aggregations