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> 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<Image> getImage(String imageId) {
+ return database.getImage(imageId);
}
/**
/* 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. */
synchronized (temporaryImages) {
temporaryImages.remove(imageId);
}
- Image image = getImage(imageId, false);
- if (image != null) {
- imageInserter.cancelImageInsert(image);
+ Optional<Image> image = getImage(imageId);
+ if (image.isPresent()) {
+ imageInserter.cancelImageInsert(image.get());
}
}
/* 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. */
public Object format(TemplateContext templateContext, Object data, Map<String, Object> 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;
}
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;
}
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}.
*
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> 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);
}
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();
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}.
*
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> 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);
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);
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;
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.
*
}
String imageId = request.getHttpRequest().getParam("image", null);
if (imageId != null) {
- Image image = webInterface.getCore().getImage(imageId, false);
+ Optional<Image> 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);
*/
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;
}
/**
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;
/**
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
String imageId = request.getHttpRequest().getParam("image");
- Image image = webInterface.getCore().getImage(imageId, false);
- if (image == null) {
+ Optional<Image> 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.<String, Object>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.<String, Object>builder().put("sone", image.get().getSone()).build()));
}
}