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
14
15
16
17
18
19
20
21 public class CoverArtManager {
22
23
24
25
26
27
28
29
30
31
32
33 public static void add(long id, InputStream in) throws IOException {
34 FileOperations.streamToFile(in,createCoverFilename(id,0));
35 }
36
37
38
39
40
41
42
43
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
53
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
67 }
68 }
69
70 }
71
72
73
74
75
76
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
86
87
88
89
90
91 public static void scale(long id, int size) throws FileNotFoundException {
92 if(contains(id, size)) {
93 return;
94 }
95 if(!contains(id,0)) {
96 return;
97 }
98 String originalFilename = createCoverFilename(id, 0);
99 String resizedFilename = createCoverFilename(id, size);
100 ImageScaler.scale(originalFilename, resizedFilename, size);
101 }
102
103
104
105
106
107
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
129
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 }