2 * Sone - ImageInserter.java - Copyright © 2011–2019 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 public class ImageInserter {
48 private static final Logger logger = getLogger(ImageInserter.class.getName());
50 /** The freenet interface. */
51 private final FreenetInterface freenetInterface;
52 private final Function<Image, InsertToken> insertTokenSupplier;
54 /** The tokens of running inserts. */
55 private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
58 * Creates a new image inserter.
60 * @param freenetInterface
61 * The freenet interface
62 * @param insertTokenSupplier
63 * The supplier for insert tokens
66 public ImageInserter(FreenetInterface freenetInterface, InsertTokenSupplier insertTokenSupplier) {
67 this.freenetInterface = freenetInterface;
68 this.insertTokenSupplier = insertTokenSupplier;
72 * Inserts the given image.
74 * @param temporaryImage
75 * The temporary image data
79 public void insertImage(TemporaryImage temporaryImage, Image image) {
80 checkNotNull(temporaryImage, "temporaryImage must not be null");
81 checkNotNull(image, "image must not be null");
82 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
84 InsertToken insertToken = insertTokenSupplier.apply(image);
85 insertTokens.put(image.getId(), insertToken);
86 freenetInterface.insertImage(temporaryImage, image, insertToken);
87 } catch (SoneException se1) {
88 logger.log(Level.WARNING, "Could not insert image!", se1);
93 * Cancels a running image insert. If no insert is running for the given
94 * image, nothing happens.
97 * The image being inserted
99 public void cancelImageInsert(Image image) {
100 InsertToken insertToken = insertTokens.remove(image.getId());
101 if (insertToken == null) {
104 insertToken.cancel();