Search in sources :

Example 6 with PkgScreenshot

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);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) PkgScreenshotImage(org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage) PkgScreenshotService(org.haiku.haikudepotserver.pkg.model.PkgScreenshotService) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) HashingInputStream(com.google.common.hash.HashingInputStream) BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) Matcher(java.util.regex.Matcher) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) JobService(org.haiku.haikudepotserver.job.model.JobService) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) HashCode(com.google.common.hash.HashCode) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) CSVWriter(com.opencsv.CSVWriter) PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) java.io(java.io) ByteStreams(com.google.common.io.ByteStreams) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) HashFunction(com.google.common.hash.HashFunction) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) ObjectContext(org.apache.cayenne.ObjectContext)

Example 7 with PkgScreenshot

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;
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) PkgScreenshotImage(org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage) PkgScreenshotService(org.haiku.haikudepotserver.pkg.model.PkgScreenshotService) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) HashingInputStream(com.google.common.hash.HashingInputStream) BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) Matcher(java.util.regex.Matcher) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) JobService(org.haiku.haikudepotserver.job.model.JobService) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) HashCode(com.google.common.hash.HashCode) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) CSVWriter(com.opencsv.CSVWriter) PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) java.io(java.io) ByteStreams(com.google.common.io.ByteStreams) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) HashFunction(com.google.common.hash.HashFunction) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) ObjectContext(org.apache.cayenne.ObjectContext)

Example 8 with PkgScreenshot

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

Example 9 with PkgScreenshot

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

Example 10 with PkgScreenshot

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

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