2 * Sone - ImageInserter.java - Copyright © 2011–2016 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static java.util.logging.Logger.getLogger;
24 import java.util.Collections;
25 import java.util.HashMap;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import javax.inject.Inject;
32 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
33 import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier;
34 import net.pterodactylus.sone.data.Image;
35 import net.pterodactylus.sone.data.TemporaryImage;
37 import com.google.common.base.Function;
40 * The image inserter is responsible for inserting images using
41 * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
42 * also tracks running inserts, giving the possibility to abort a running
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class ImageInserter {
50 private static final Logger logger = getLogger(ImageInserter.class.getName());
52 /** The freenet interface. */
53 private final FreenetInterface freenetInterface;
54 private final Function<Image, InsertToken> insertTokenSupplier;
56 /** The tokens of running inserts. */
57 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
60 * Creates a new image inserter.
62 * @param freenetInterface
63 * The freenet interface
64 * @param insertTokenSupplier
65 * The supplier for insert tokens
68 public ImageInserter(FreenetInterface freenetInterface, InsertTokenSupplier insertTokenSupplier) {
69 this.freenetInterface = freenetInterface;
70 this.insertTokenSupplier = insertTokenSupplier;
74 * Inserts the given image.
76 * @param temporaryImage
77 * The temporary image data
81 public void insertImage(TemporaryImage temporaryImage, Image image) {
82 checkNotNull(temporaryImage, "temporaryImage must not be null");
83 checkNotNull(image, "image must not be null");
84 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
86 InsertToken insertToken = insertTokenSupplier.apply(image);
87 insertTokens.put(image.getId(), insertToken);
88 freenetInterface.insertImage(temporaryImage, image, insertToken);
89 } catch (SoneException se1) {
90 logger.log(Level.WARNING, "Could not insert image!", se1);
95 * Cancels a running image insert. If no insert is running for the given
96 * image, nothing happens.
99 * The image being inserted
101 public void cancelImageInsert(Image image) {
102 InsertToken insertToken = insertTokens.remove(image.getId());
103 if (insertToken == null) {
106 insertToken.cancel();