1 package org.musicontroller.service;
2
3 import java.io.File;
4
5 import org.musicontroller.core.MusicDirProvider;
6 import org.musicontroller.importer.ImporterException;
7 import org.musicontroller.importer.MusicArchiveEntryBean;
8 import org.varienaja.util.FileOperations;
9
10
11
12
13
14
15
16
17
18 public class FileUtils implements MusicDirProvider {
19
20
21
22
23
24 private static String _basedir;
25
26
27
28
29 public FileUtils() {
30 _basedir = null;
31 }
32
33
34
35
36
37
38 private void createDir(String dir) throws Exception {
39 File test = new File(dir);
40 if (!test.exists()) {
41 if (!test.mkdirs()) {
42 throw new Exception("Could not create MusiController-directory: "+dir);
43 }
44 }
45 }
46
47
48
49
50
51 public void setBasedir(String basedir) throws Exception {
52 _basedir = basedir;
53
54
55 createDir(_basedir);
56 createDir(getArchivedir());
57 createDir(getMyAccountdir());
58 createDir(getCoverdir());
59 createDir(getReviewdir());
60 createDir(getIndexdir());
61 createDir(getUploaddir());
62 createDir(getUnpackdir());
63 }
64
65
66
67
68
69
70
71
72 public static String getArchivedir() {
73 return _basedir==null ? "songs" : _basedir+File.separator+"songs";
74 }
75
76
77
78
79
80 public String getMusicDirectory() {
81 return getArchivedir();
82 }
83
84
85
86
87
88 public static String getMyAccountdir() {
89 return _basedir+File.separator+"myAccount";
90 }
91
92
93
94
95
96 public static String getCoverdir() {
97 return _basedir+File.separator+"covers";
98 }
99
100
101
102
103
104 public static String getReviewdir() {
105 return _basedir+File.separator+"reviews";
106 }
107
108
109
110
111
112 public static String getIndexdir() {
113 return _basedir+File.separator+"index";
114 }
115
116 public static String getUploaddir() {
117 return _basedir+File.separator+"upload";
118 }
119
120
121
122
123
124
125
126 public static String getUnpackdir() {
127 return _basedir+File.separator+"unpack";
128 }
129
130
131
132
133
134
135
136
137
138
139 protected static String getStorageHashname(MusicArchiveEntryBean song) {
140 if(song==null) {
141 return "_";
142 }
143 String bandname = song.getBandName();
144 if(bandname==null||bandname.length()<1) {
145 return "_";
146 }
147 bandname = bandname.trim();
148 if(bandname.isEmpty()) {
149 throw new IllegalArgumentException("The songs band name contains no non whitespace characters.");
150 }
151 return FileOperations.translateIllegalFileChars(bandname).substring(0,1);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public static String[] getNewSongDestination(MusicArchiveEntryBean song) {
168 if (song==null) {
169 throw new IllegalArgumentException("Song must not be null.");
170 }
171 if(song.getBandName()==null) {
172 throw new IllegalArgumentException("Bandname must not be null.");
173 }
174 if(song.getSongName()==null) {
175 throw new IllegalArgumentException("Songname must not be null.");
176 }
177 if(song.getBandName().trim().length()<1) {
178 throw new IllegalArgumentException("Bandname must not be empty.");
179 }
180 if(song.getSongName().trim().length()<1) {
181 throw new IllegalArgumentException("Songname must not be null.");
182 }
183
184 String[] result = new String[3];
185 result[0] = getArchivedir();
186 result[1] = getStorageHashname(song) + File.separator + FileOperations.translateIllegalFileChars(song.getBandName());
187 String tmp = FileOperations.translateIllegalFileChars(song.getSongName());
188 result[2] = generateUniqueFilename(result[0] + File.separator + result[1], tmp, ".mp3");
189
190 return result;
191 }
192
193
194
195
196
197
198
199
200
201 private static String generateUniqueFilename(String path, String name, String extension) {
202 String testname = name + extension;
203 File testFile = new File(path + File.separator + testname);
204 int counter=1;
205 while(testFile.exists()) {
206 testname = name + " (" + String.valueOf(counter) + ")" + extension;
207 testFile = new File(path + File.separator + testname);
208 counter++;
209 }
210 return testname;
211 }
212
213 }