From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 11:59:35 +0000 (+0200) Subject: Remove the possibility to create images for unknown IDs. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=58497297d2b9a18cd2877a226870acfe9e8837af;p=Sone.git Remove the possibility to create images for unknown IDs. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index d20549f..fd3b1d1 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -643,40 +643,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return newAlbum; } - /** - * Returns the image with the given ID, creating it if necessary. - * - * @param imageId - * The ID of the image - * @return The image with the given ID - */ - public Image getImage(String imageId) { - return getImage(imageId, true); - } - - /** - * Returns the image with the given ID, optionally creating it if it does - * not exist. - * - * @param imageId - * The ID of the image - * @param create - * {@code true} to create an image if none exists with the given - * ID - * @return The image with the given ID, or {@code null} if none exists and - * none was created - */ - public Image getImage(String imageId, boolean create) { - Optional image = database.getImage(imageId); - if (image.isPresent()) { - return image.get(); - } - if (!create) { - return null; - } - Image newImage = database.newImageBuilder().withId(imageId).build(); - database.storeImage(newImage); - return newImage; + public Optional getImage(String imageId) { + return database.getImage(imageId); } /** @@ -1290,7 +1258,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* load avatar. */ String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null); if (avatarId != null) { - profile.setAvatar(getImage(avatarId, false)); + profile.setAvatar(getImage(avatarId).orNull()); } /* load options. */ @@ -1700,9 +1668,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (temporaryImages) { temporaryImages.remove(imageId); } - Image image = getImage(imageId, false); - if (image != null) { - imageInserter.cancelImageInsert(image); + Optional image = getImage(imageId); + if (image.isPresent()) { + imageInserter.cancelImageInsert(image.get()); } } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 9e50886..db302b0 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -498,7 +498,7 @@ public class SoneDownloader extends AbstractService { /* process avatar. */ if (avatarId != null) { - profile.setAvatar(core.getImage(avatarId, false)); + profile.setAvatar(core.getImage(avatarId).orNull()); } /* okay, apparently everything was parsed correctly. Now import. */ diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index 7e3723c..55cf4b1 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -69,7 +69,7 @@ public class ImageLinkFilter implements Filter { public Object format(TemplateContext templateContext, Object data, Map parameters) { Image image = null; if (data instanceof String) { - image = core.getImage((String) data, false); + image = core.getImage((String) data).orNull(); } else if (data instanceof Image) { image = (Image) data; } diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java index 30c0a52..f072041 100644 --- a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java @@ -65,7 +65,7 @@ public class ProfileAccessor extends ReflectionAccessor { if (avatarId == null) { return null; } - if (core.getImage(avatarId, false) == null) { + if (!core.getImage(avatarId).isPresent()) { /* avatar ID but no matching image? show nothing. */ return null; } diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteImagePage.java b/src/main/java/net/pterodactylus/sone/web/DeleteImagePage.java index 77f3ab8..6b423ef 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteImagePage.java @@ -23,6 +23,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user delete an {@link Image}. * @@ -53,19 +55,19 @@ public class DeleteImagePage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String imageId = (request.getMethod() == Method.POST) ? request.getHttpRequest().getPartAsStringFailsafe("image", 36) : request.getHttpRequest().getParam("image"); - Image image = webInterface.getCore().getImage(imageId, false); - if (image == null) { + Optional image = webInterface.getCore().getImage(imageId); + if (!image.isPresent()) { throw new RedirectException("invalid.html"); } - if (!image.getSone().isLocal()) { + if (!image.get().getSone().isLocal()) { throw new RedirectException("noPermission.html"); } if (request.getMethod() == Method.POST) { if (request.getHttpRequest().isPartSet("abortDelete")) { - throw new RedirectException("imageBrowser.html?image=" + image.getId()); + throw new RedirectException("imageBrowser.html?image=" + image.get().getId()); } - webInterface.getCore().deleteImage(image); - throw new RedirectException("imageBrowser.html?album=" + image.getAlbum().getId()); + webInterface.getCore().deleteImage(image.get()); + throw new RedirectException("imageBrowser.html?album=" + image.get().getAlbum().getId()); } templateContext.set("image", image); } diff --git a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java index 9e4aae9..5e07011 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java @@ -68,7 +68,7 @@ public class EditAlbumPage extends SoneTemplatePage { throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId()); } String albumImageId = request.getHttpRequest().getPartAsStringFailsafe("album-image", 36); - if (webInterface.getCore().getImage(albumImageId, false) == null) { + if (!webInterface.getCore().getImage(albumImageId).isPresent()) { albumImageId = null; } album.modify().setAlbumImage(albumImageId).update(); diff --git a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java index 9a29c85..cec6990 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java @@ -24,6 +24,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user edit title and description of an {@link Image}. * @@ -56,24 +58,24 @@ public class EditImagePage extends SoneTemplatePage { if (request.getMethod() == Method.POST) { String imageId = request.getHttpRequest().getPartAsStringFailsafe("image", 36); String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - Image image = webInterface.getCore().getImage(imageId, false); - if (image == null) { + Optional image = webInterface.getCore().getImage(imageId); + if (!image.isPresent()) { throw new RedirectException("invalid.html"); } - if (!image.getSone().isLocal()) { + if (!image.get().getSone().isLocal()) { throw new RedirectException("noPermission.html"); } if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) { - image.getAlbum().moveImageUp(image); + image.get().getAlbum().moveImageUp(image.get()); } else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) { - image.getAlbum().moveImageDown(image); + image.get().getAlbum().moveImageDown(image.get()); } else { String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim(); String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1024).trim(); if (title.length() == 0) { templateContext.set("titleMissing", true); } - image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + image.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); } webInterface.getCore().touchConfiguration(); throw new RedirectException(returnPage); diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index c79a1e9..d814212 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -82,7 +82,7 @@ public class EditProfilePage extends SoneTemplatePage { profile.setMiddleName(middleName.length() > 0 ? middleName : null); profile.setLastName(lastName.length() > 0 ? lastName : null); profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear); - profile.setAvatar(webInterface.getCore().getImage(avatarId, false)); + profile.setAvatar(webInterface.getCore().getImage(avatarId).orNull()); for (Field field : fields) { String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400); field.setValue(value); diff --git a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java index 766f018..1b00d81 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -27,8 +27,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import com.google.common.base.Optional; - import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; @@ -38,6 +36,8 @@ import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; + /** * The image browser page is the entry page for the image management. * @@ -77,9 +77,9 @@ public class ImageBrowserPage extends SoneTemplatePage { } String imageId = request.getHttpRequest().getParam("image", null); if (imageId != null) { - Image image = webInterface.getCore().getImage(imageId, false); + Optional image = webInterface.getCore().getImage(imageId); templateContext.set("imageRequested", true); - templateContext.set("image", image); + templateContext.set("image", image.orNull()); return; } String soneId = request.getHttpRequest().getParam("sone", null); diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index e9241a1..ceb192e 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -367,7 +367,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getImageId(String phrase) { String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null; + return webInterface.getCore().getImage(imageId).isPresent() ? imageId : null; } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java index 0c45225..ace775d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java @@ -24,6 +24,7 @@ import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; /** @@ -59,28 +60,28 @@ public class EditImageAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String imageId = request.getHttpRequest().getParam("image"); - Image image = webInterface.getCore().getImage(imageId, false); - if (image == null) { + Optional image = webInterface.getCore().getImage(imageId); + if (!image.isPresent()) { return createErrorJsonObject("invalid-image-id"); } - if (!image.getSone().isLocal()) { + if (!image.get().getSone().isLocal()) { return createErrorJsonObject("not-authorized"); } if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Image swappedImage = image.getAlbum().moveImageUp(image); + Image swappedImage = image.get().getAlbum().moveImageUp(image.get()); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); + return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId()); } if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Image swappedImage = image.getAlbum().moveImageDown(image); + Image swappedImage = image.get().getAlbum().moveImageDown(image.get()); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); + return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId()); } String title = request.getHttpRequest().getParam("title").trim(); String description = request.getHttpRequest().getParam("description").trim(); - image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + image.get().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", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.builder().put("sone", image.getSone()).build())); + return createSuccessJsonObject().put("imageId", image.get().getId()).put("title", image.get().getTitle()).put("description", image.get().getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.get().getDescription(), ImmutableMap.builder().put("sone", image.get().getSone()).build())); } }