1 | package org.apollo.util;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import org.apollo.agents.MelodySearch;
|
---|
10 | import org.apollo.io.AudioPathManager;
|
---|
11 | import org.apollo.widgets.SampledTrack;
|
---|
12 | import org.expeditee.settings.folders.FolderSettings;
|
---|
13 | import org.expeditee.gui.DisplayController;
|
---|
14 | import org.expeditee.gui.management.ResourceManager;
|
---|
15 | import org.expeditee.items.ItemUtils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Purges a
|
---|
19 | * @author Brook Novak
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public final class AudioPurger {
|
---|
23 |
|
---|
24 | private AudioPurger() {
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | public static void purgeLocalAudio() {
|
---|
29 |
|
---|
30 | List<String> audioFiles = AudioPathManager.getAudioFileNames();
|
---|
31 | if (audioFiles == null || audioFiles.size() == 0) return;
|
---|
32 |
|
---|
33 |
|
---|
34 | // Search every profile and frameset
|
---|
35 | Set<String> referencedAudio = new HashSet<String>();
|
---|
36 |
|
---|
37 | for (String path : FolderSettings.FrameDirs.getAbsoluteDirs()) {
|
---|
38 |
|
---|
39 | File frameDir = new File(path);
|
---|
40 |
|
---|
41 | if (frameDir.exists() && frameDir.isDirectory()) {
|
---|
42 |
|
---|
43 | try {
|
---|
44 | getReferenceAudioFiles(frameDir, referencedAudio);
|
---|
45 | } catch (IOException e) {
|
---|
46 | e.printStackTrace();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Delelete all unferenced audio thats using up precious disk space
|
---|
52 | for (String audioFile : audioFiles) {
|
---|
53 | if (!referencedAudio.contains(audioFile)) {
|
---|
54 | File unreferencedFile = ResourceManager.getAudioResource(audioFile, DisplayController.getCurrentFrame());
|
---|
55 | //File unreferencedFile = new File(AudioPathManager.AUDIO_HOME_DIRECTORY + audioFile);
|
---|
56 | ApolloSystemLog.println("Deleting unreference audio file \"" + audioFile + "\"");
|
---|
57 | unreferencedFile.delete();
|
---|
58 |
|
---|
59 | // Also delete any melody meta
|
---|
60 | String melodyFileName = MelodySearch.MELODY_METAFILE_PREFIX + audioFile + MelodySearch.MELODY_METAFILE_SUFFIX;
|
---|
61 | File melodyMetaFile = ResourceManager.getAudioResource(melodyFileName, DisplayController.getCurrentFrame());
|
---|
62 |
|
---|
63 | if (melodyMetaFile.exists()) melodyMetaFile.delete();
|
---|
64 |
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static void getReferenceAudioFiles(File dir, Set<String> refedAudio) throws IOException {
|
---|
71 |
|
---|
72 | String[] filenames = dir.list();
|
---|
73 | if (filenames == null) throw new IOException();
|
---|
74 |
|
---|
75 | String trackPrefix = ItemUtils.GetTag(ItemUtils.TAG_IWIDGET)
|
---|
76 | + ": " + SampledTrack.class.getName();
|
---|
77 |
|
---|
78 | int filenameMetaLength = SampledTrack.META_LOCALNAME_TAG.length();
|
---|
79 |
|
---|
80 | for (String frameFile : filenames) {
|
---|
81 |
|
---|
82 | File f = new File(dir.getPath() + File.separatorChar + frameFile);
|
---|
83 | if (!f.exists()) continue;
|
---|
84 |
|
---|
85 | // Recurse
|
---|
86 | if (f.isDirectory()) {
|
---|
87 | getReferenceAudioFiles(f, refedAudio);
|
---|
88 | continue;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Perform text search on sampled track widgets
|
---|
92 | List<TextItemSearchResult> results =
|
---|
93 | ExpediteeFileTextSearch.prefixSearch(f.getAbsolutePath(), new String[] {trackPrefix});
|
---|
94 |
|
---|
95 | // For each track widget
|
---|
96 | for (TextItemSearchResult result : results) {
|
---|
97 |
|
---|
98 | // For each peice of meta data
|
---|
99 | if (result.data != null) {
|
---|
100 | for (String meta : result.data) {
|
---|
101 |
|
---|
102 | // Find the local-filename meta
|
---|
103 | if (meta.length() > filenameMetaLength &&
|
---|
104 | meta.startsWith(SampledTrack.META_LOCALNAME_TAG)) {
|
---|
105 |
|
---|
106 | // Keep meta
|
---|
107 | refedAudio.add(meta.substring(filenameMetaLength));
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static void main(String[] args) {
|
---|
120 | purgeLocalAudio();
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|