+++ /dev/null
-/*
- * Sone - EditImageAjaxPage.java - Copyright © 2011–2016 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.ajax;
-
-import com.google.common.collect.ImmutableMap;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.template.ParserFilter;
-import net.pterodactylus.sone.template.RenderFilter;
-import net.pterodactylus.sone.template.ShortenFilter;
-import net.pterodactylus.sone.text.TextFilter;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.template.TemplateContext;
-
-/**
- * Page that stores a user’s image modifications.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class EditImageAjaxPage extends JsonPage {
-
- private final ParserFilter parserFilter;
- private final ShortenFilter shortenFilter;
- private final RenderFilter renderFilter;
-
- /**
- * Creates a new edit image AJAX page.
- *
- * @param webInterface
- * The Sone web interface
- * @param parserFilter
- * The parser filter for image descriptions
- */
- public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, ShortenFilter shortenFilter, RenderFilter renderFilter) {
- super("editImage.ajax", webInterface);
- this.parserFilter = parserFilter;
- this.shortenFilter = shortenFilter;
- this.renderFilter = renderFilter;
- }
-
- //
- // JSONPAGE METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected JsonReturnObject createJsonObject(FreenetRequest request) {
- String imageId = request.getHttpRequest().getParam("image");
- Image image = webInterface.getCore().getImage(imageId, false);
- if (image == null) {
- return createErrorJsonObject("invalid-image-id");
- }
- if (!image.getSone().isLocal()) {
- return createErrorJsonObject("not-authorized");
- }
- if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
- Image swappedImage = image.getAlbum().moveImageUp(image);
- webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
- }
- if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
- Image swappedImage = image.getAlbum().moveImageDown(image);
- webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
- }
- String title = request.getHttpRequest().getParam("title").trim();
- if (title.isEmpty()) {
- return createErrorJsonObject("invalid-image-title");
- }
- String description = request.getHttpRequest().getParam("description").trim();
- image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
- webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", renderImageDescription(image));
- }
-
- private String renderImageDescription(Image image) {
- TemplateContext templateContext = new TemplateContext();
- ImmutableMap<String, Object> parameters = ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build();
- Object parts = parserFilter.format(templateContext, image.getDescription(), parameters);
- Object shortenedParts = shortenFilter.format(templateContext, parts, parameters);
- return (String) renderFilter.format(templateContext, shortenedParts, parameters);
- }
-
-}
--- /dev/null
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.template.ParserFilter
+import net.pterodactylus.sone.template.RenderFilter
+import net.pterodactylus.sone.template.ShortenFilter
+import net.pterodactylus.sone.text.TextFilter
+import net.pterodactylus.sone.utils.headers
+import net.pterodactylus.sone.utils.ifTrue
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.template.TemplateContext
+
+/**
+ * Page that stores a user’s image modifications.
+ */
+class EditImageAjaxPage(webInterface: WebInterface,
+ private val parserFilter: ParserFilter,
+ private val shortenFilter: ShortenFilter,
+ private val renderFilter: RenderFilter) : JsonPage("editImage.ajax", webInterface) {
+
+ override fun createJsonObject(request: FreenetRequest) =
+ request.parameters["image"]
+ .let(webInterface.core::getImage)
+ ?.let { image ->
+ image.sone.isLocal.ifTrue {
+ when {
+ request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
+ put("sourceImageId", image.id)
+ put("destinationImageId", image.album.moveImageUp(image).id)
+ webInterface.core.touchConfiguration()
+ }
+ request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
+ put("sourceImageId", image.id)
+ put("destinationImageId", image.album.moveImageDown(image).id)
+ webInterface.core.touchConfiguration()
+ }
+ else -> request.parameters["title"]!!.let { title ->
+ title.trim().isNotBlank().ifTrue {
+ request.parameters["description"]!!.let { description ->
+ image.modify()
+ .setTitle(title)
+ .setDescription(TextFilter.filter(request.headers["Host"], description))
+ .update().let { newImage ->
+ createSuccessJsonObject().apply {
+ put("title", newImage.title)
+ put("description", newImage.description)
+ put("parsedDescription", newImage.description.let {
+ parserFilter.format(TemplateContext(), it, mutableMapOf("sone" to image.sone)).let {
+ shortenFilter.format(TemplateContext(), it, mutableMapOf()).let {
+ renderFilter.format(TemplateContext(), it, mutableMapOf()) as String
+ }
+ }
+ })
+ webInterface.core.touchConfiguration()
+ }
+ }
+ }
+ } ?: createErrorJsonObject("invalid-image-title")
+ }
+ }
+ } ?: createErrorJsonObject("not-authorized")
+ } ?: createErrorJsonObject("invalid-image-id")
+
+}