1 package org.varienaja.util.coverart;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FilenameFilter;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 import org.musicontroller.service.FileUtils;
10 import org.varienaja.util.FileOperations;
11
12 /**
13 * This class manages a store of cover art pictures in different
14 * sizes. It hides the details of the file system handling from
15 * the users of the storage. The class uses long-typed keys to
16 * identify the different covers.
17 *
18 * @author Hans Drexler
19 * @version $Id: CoverArtManager.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
20 */
21 public class CoverArtManager {
22
23 /**
24 * Add cover art picture from the InputStream to the storage. Use
25 * the specified size. This will overwrite a previous picture with
26 * the same id and size.
27 *
28 * @param id The id of the added cover.
29 * @param in The input stream containing the picture.
30 * @param size The size of the picture.
31 * @throws IOException An error occured when reading the input stream or during writing the picture.
32 */
33 public static void add(long id, InputStream in) throws IOException {
34 FileOperations.streamToFile(in,createCoverFilename(id,0));
35 }
36
37 /**
38 * Returns the file containing the cover art of the specified id and size.
39 * @param id The requested cover art id.
40 * @param size The requested cover art size.
41 * @return A file containing the cover art of the requested id and size. This method
42 * allways returns a File object (possibly to a non-existent file) and never
43 * returns NULL.
44 */
45 public static File get(long id, int size) {
46 String filename = createCoverFilename(id, size);
47 File target = new File(filename);
48 return target;
49 }
50
51 /**
52 * Deletes all entries for this id.
53 * @param id The id to delete.
54 */
55 public static void purge(long id) {
56 final String matchString = "cover"+id+"_.*";
57 FilenameFilter matching = new FilenameFilter() {
58 public boolean accept(File dir, String name) {
59 return name.matches(matchString);
60 }
61 };
62 File coverDir = new File(FileUtils.getCoverdir());
63 File[] entries = coverDir.listFiles(matching);
64 for(File deleteThis: entries) {
65 if (!deleteThis.delete()) {
66 //TODO LOG
67 }
68 }
69
70 }
71
72 /**
73 * Tests the existence of a cover art picture for the specified id and size.
74 * @param id The id of the cover art.
75 * @param size The size of the cover art.
76 * @return True if the storage contains a picture of the specified id and size, otherwise false.
77 */
78 public static boolean contains (long id, int size) {
79 String filename = createCoverFilename(id, size);
80 File test = new File(filename);
81 return test.exists();
82 }
83
84 /**
85 * Scales the original image of the specified id to the specified size. Does nothing
86 * if the original version of the specified id is not present.
87 * @param id The cover art id.
88 * @param size The requested picture size to create.
89 * @throws FileNotFoundException
90 */
91 public static void scale(long id, int size) throws FileNotFoundException {
92 if(contains(id, size)) {
93 return; // Already done.
94 }
95 if(!contains(id,0)) {
96 return; // There is no original to scale.
97 }
98 String originalFilename = createCoverFilename(id, 0);
99 String resizedFilename = createCoverFilename(id, size);
100 ImageScaler.scale(originalFilename, resizedFilename, size);
101 }
102
103 /**
104 * Creates a filename for a cover-image
105 * @param playlistid The id of the playlist
106 * @param size The requested size (both x and y) in pixels.
107 * @return The filename, where this cover can be found.
108 */
109 private static String createCoverFilename(long playlistid, int size) {
110 StringBuilder sb = new StringBuilder();
111 sb.append(getBaseCoverDir());
112 sb.append(playlistid);
113 if (size>0) {
114 sb.append("_(");
115 sb.append(size);
116 sb.append("x");
117 sb.append(size);
118 sb.append(")");
119 } else {
120 sb.append("_(original)");
121 }
122 sb.append(".jpg");
123
124 return sb.toString();
125 }
126
127 /**
128 * Returns the name ofdirectory containing all the cover art.
129 * @return The name of the directory containing all cover art.
130 */
131 private static String getBaseCoverDir() {
132 StringBuilder sb = new StringBuilder();
133 sb.append(FileUtils.getCoverdir());
134 sb.append(File.separatorChar);
135 sb.append("cover");
136 return sb.toString();
137 }
138
139 }