package com.bkromhout.minerva.data;

import com.bkromhout.minerva.Minerva;
import com.bkromhout.minerva.util.Util;
import com.google.common.io.ByteStreams;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Class containing methods to help with storing/retrieving cover images.
 */
public class CoverHelper {
    /**
     * Extension to append to cover image files.
     */
    private static final String COVER_EXT = ".cover";
    /**
     * Instance.
     */
    private static CoverHelper INSTANCE = null;
    /**
     * Directory where cover image files are stored.
     */
    private File dir = null;

    public static CoverHelper get() {
        if (INSTANCE == null) INSTANCE = new CoverHelper();
        return INSTANCE;
    }

    // Private constructor.
    private CoverHelper() {
        dir = Minerva.getAppCtx().getFilesDir();
    }

    /**
     * Save an input stream to a file with the ".cover" extension.
     * @param in      Input stream.
     * @param relPath Relative path to use to create cover file path.
     */
    public void saveStreamAsCoverImage(InputStream in, String relPath) {
        File coverFile = new File(dir, relPath + COVER_EXT);
        try (FileOutputStream out = new FileOutputStream(coverFile)) {
            ByteStreams.copy(in, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Delete the cover image saved using the given relative path, if there is one.
     * @param relPath Relative path which was used to create cover file path.
     */
    public void deleteCoverImage(String relPath) {
        File coverFile = Util.getFileFromRelPath(dir, relPath + COVER_EXT);
        if (coverFile == null || !coverFile.exists()) return;
        coverFile.delete();
    }

    /**
     * Get the cover file saved using the given relative path, if there is one.
     * @param relPath Relative path which was used to create cover file path.
     * @return Cover file, or null.
     */
    public File getCoverImageFile(String relPath) {
        return Util.getFileFromRelPath(dir, relPath + COVER_EXT);
    }
}

@@ -148,16 +148,51 @@
 Streams;
+%0Aimport com.google.common.io.Files;
 %0A%0Aimport
@@ -1261,32 +1261,97 @@
 h + COVER_EXT);%0A
+        try %7B%0A            Files.createParentDirs(coverFile);%0A    
         try (Fil
@@ -1417,16 +1417,20 @@
         
+    
 ByteStre
@@ -1448,16 +1448,30 @@
 , out);%0A
+            %7D%0A
         

Make sure that subdirectories are created before trying to create files.