Search in sources :

Example 1 with SizeLimitReachedException

use of org.haiku.haikudepotserver.pkg.model.SizeLimitReachedException in project haikudepotserver by haiku.

the class PkgScreenshotController method handleAdd.

/**
 * <p>This handler will take-up an HTTP POST that provides a new screenshot for the package.</p>
 */
@RequestMapping(value = "/{" + KEY_PKGNAME + "}/add", method = RequestMethod.POST)
public void handleAdd(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = KEY_FORMAT) String format, @PathVariable(value = KEY_PKGNAME) String pkgName) throws IOException {
    if (Strings.isNullOrEmpty(pkgName) || !Pkg.PATTERN_NAME.matcher(pkgName).matches()) {
        throw new MissingPkgName();
    }
    if (Strings.isNullOrEmpty(format) || !"png".equals(format)) {
        throw new MissingOrBadFormat();
    }
    ObjectContext context = serverRuntime.newContext();
    Pkg pkg = Pkg.tryGetByName(context, pkgName).orElseThrow(PkgNotFound::new);
    // check the authorization
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), pkg, Permission.PKG_EDITSCREENSHOT)) {
        LOGGER.warn("attempt to add a pkg screenshot, but there is no user present or that user is not able to edit the pkg");
        throw new PkgAuthorizationFailure();
    }
    String screenshotCode;
    try {
        screenshotCode = pkgScreenshotService.storePkgScreenshotImage(request.getInputStream(), context, pkg.getPkgSupplement(), null).getCode();
    } catch (SizeLimitReachedException sizeLimit) {
        LOGGER.warn("attempt to load in a screenshot larger than the size limit");
        throw new MissingOrBadFormat();
    } catch (BadPkgScreenshotException badIcon) {
        throw new MissingOrBadFormat();
    }
    context.commitChanges();
    // trigger optimization of the screenshot image.
    jobService.submit(new PkgScreenshotOptimizationJobSpecification(screenshotCode), JobSnapshot.COALESCE_STATUSES_QUEUED_STARTED);
    response.setHeader(HEADER_SCREENSHOTCODE, screenshotCode);
    response.setStatus(HttpServletResponse.SC_OK);
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) PkgScreenshotOptimizationJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotOptimizationJobSpecification) ObjectContext(org.apache.cayenne.ObjectContext) SizeLimitReachedException(org.haiku.haikudepotserver.pkg.model.SizeLimitReachedException) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)1 User (org.haiku.haikudepotserver.dataobjects.User)1 BadPkgScreenshotException (org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException)1 PkgScreenshotOptimizationJobSpecification (org.haiku.haikudepotserver.pkg.model.PkgScreenshotOptimizationJobSpecification)1 SizeLimitReachedException (org.haiku.haikudepotserver.pkg.model.SizeLimitReachedException)1