Delete temporary image before removing image.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 78cd58d..f5d695c 100644 (file)
@@ -1847,6 +1847,29 @@ public class Core implements IdentityListener, UpdateListener, ImageInsertListen
        }
 
        /**
+        * Deletes the given album. The owner of the album has to be a local Sone,
+        * and the album has to be {@link Album#isEmpty() empty} to be deleted.
+        *
+        * @param album
+        *            The album to remove
+        */
+       public void deleteAlbum(Album album) {
+               Validation.begin().isNotNull("Album", album).check().is("Local Sone", isLocalSone(album.getSone())).check();
+               if (!album.isEmpty()) {
+                       return;
+               }
+               if (album.getParent() == null) {
+                       album.getSone().removeAlbum(album);
+               } else {
+                       album.getParent().removeAlbum(album);
+               }
+               synchronized (albums) {
+                       albums.remove(album.getId());
+               }
+               saveSone(album.getSone());
+       }
+
+       /**
         * Creates a new image.
         *
         * @param sone
@@ -1864,10 +1887,29 @@ public class Core implements IdentityListener, UpdateListener, ImageInsertListen
                synchronized (images) {
                        images.put(image.getId(), image);
                }
+               imageInserter.insertImage(temporaryImage, image);
                return image;
        }
 
        /**
+        * Deletes the given image. This method will also delete a matching
+        * temporary image.
+        *
+        * @see #deleteTemporaryImage(TemporaryImage)
+        * @param image
+        *            The image to delete
+        */
+       public void deleteImage(Image image) {
+               Validation.begin().isNotNull("Image", image).check().is("Local Sone", isLocalSone(image.getSone())).check();
+               deleteTemporaryImage(image.getId());
+               image.getAlbum().removeImage(image);
+               synchronized (images) {
+                       images.remove(image.getId());
+               }
+               saveSone(image.getSone());
+       }
+
+       /**
         * Creates a new temporary image.
         *
         * @param mimeType
@@ -1891,7 +1933,7 @@ public class Core implements IdentityListener, UpdateListener, ImageInsertListen
         * @param temporaryImage
         *            The temporary image to delete
         */
-       public void deteleTemporaryImage(TemporaryImage temporaryImage) {
+       public void deleteTemporaryImage(TemporaryImage temporaryImage) {
                Validation.begin().isNotNull("Temporary Image", temporaryImage).check();
                deleteTemporaryImage(temporaryImage.getId());
        }
@@ -1907,6 +1949,10 @@ public class Core implements IdentityListener, UpdateListener, ImageInsertListen
                synchronized (temporaryImages) {
                        temporaryImages.remove(imageId);
                }
+               Image image = getImage(imageId, false);
+               if (image != null) {
+                       imageInserter.cancelImageInsert(image);
+               }
        }
 
        /**