Merge branch 'release-0.8.9'
[Sone.git] / src / main / java / net / pterodactylus / sone / core / ImageInserter.java
1 /*
2  * Sone - ImageInserter.java - Copyright © 2011–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
30 import net.pterodactylus.sone.data.Image;
31 import net.pterodactylus.sone.data.TemporaryImage;
32 import net.pterodactylus.util.logging.Logging;
33
34 /**
35  * The image inserter is responsible for inserting images using
36  * {@link FreenetInterface#insertImage(TemporaryImage, Image, InsertToken)} and
37  * also tracks running inserts, giving the possibility to abort a running
38  * insert.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class ImageInserter {
43
44         /** The logger. */
45         private static final Logger logger = Logging.getLogger(ImageInserter.class);
46
47         /** The freenet interface. */
48         private final FreenetInterface freenetInterface;
49
50         /** The tokens of running inserts. */
51         private final Map<String, InsertToken> insertTokens = Collections.synchronizedMap(new HashMap<String, InsertToken>());
52
53         /**
54          * Creates a new image inserter.
55          *
56          * @param freenetInterface
57          *            The freenet interface
58          */
59         public ImageInserter(FreenetInterface freenetInterface) {
60                 this.freenetInterface = freenetInterface;
61         }
62
63         /**
64          * Inserts the given image.
65          *
66          * @param temporaryImage
67          *            The temporary image data
68          * @param image
69          *            The image
70          */
71         public void insertImage(TemporaryImage temporaryImage, Image image) {
72                 checkNotNull(temporaryImage, "temporaryImage must not be null");
73                 checkNotNull(image, "image must not be null");
74                 checkArgument(image.getId().equals(temporaryImage.getId()), "image IDs must match");
75                 try {
76                         InsertToken insertToken = freenetInterface.new InsertToken(image);
77                         insertTokens.put(image.getId(), insertToken);
78                         freenetInterface.insertImage(temporaryImage, image, insertToken);
79                 } catch (SoneException se1) {
80                         logger.log(Level.WARNING, "Could not insert image!", se1);
81                 }
82         }
83
84         /**
85          * Cancels a running image insert. If no insert is running for the given
86          * image, nothing happens.
87          *
88          * @param image
89          *            The image being inserted
90          */
91         public void cancelImageInsert(Image image) {
92                 InsertToken insertToken = insertTokens.remove(image.getId());
93                 if (insertToken == null) {
94                         return;
95                 }
96                 insertToken.cancel();
97         }
98
99 }