Changeset 1434 for trunk/src/org/apollo/io/AudioPathManager.java
- Timestamp:
- 10/02/19 14:19:48 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/apollo/io/AudioPathManager.java
r1327 r1434 7 7 import java.io.IOException; 8 8 import java.io.UnsupportedEncodingException; 9 import java.util.ArrayList; 10 import java.util.List; 9 11 import java.util.Random; 12 import java.util.function.Predicate; 10 13 11 14 import org.apollo.audio.structure.AudioStructureModel; 12 15 import org.apollo.util.ApolloSystemLog; 13 16 import org.apollo.util.RegExpFileFilter; 14 import org.expeditee. auth.AuthenticatorBrowser;17 import org.expeditee.gui.DisplayController; 15 18 import org.expeditee.gui.FrameIO; 19 import org.expeditee.gui.management.ResourceManager; 16 20 import org.expeditee.settings.UserSettings; 17 21 … … 29 33 30 34 /** Suffixed with the native file separator */ 31 public static String AUDIO_ HOME_DIRECTORY = null;35 public static String AUDIO_USERNAME_HOME_DIRECTORY = null; 32 36 33 37 /** Stores all virtual names created by this repository. */ … … 39 43 40 44 private static Random rand = new Random(System.currentTimeMillis()); 41 42 // **** DB 43 // Some work to be done here => remove dependency in code on 'AUDIO_HOME_DIRECTORY' 44 // and change it to use audio dir list from UserSettings 45 public static void changeParentAndSubFolders(String newFolder) { 46 47 if (AuthenticatorBrowser.isAuthenticationRequired() && System.getProperty("user.name").equals(AuthenticatorBrowser.USER_NOBODY)) { 48 System.err.println("**** AudioPathManager::changeParentAndSubFolder(): Nothing to do for user '" + AuthenticatorBrowser.USER_NOBODY + "'"); 49 return; 50 } 51 52 if (UserSettings.PublicAndPrivateResources) { 53 //String resourcesPublicPath = FrameIO.PARENT_FOLDER + "resources-public" + File.separator; 54 String userName = UserSettings.UserName.get(); 55 //String resourcesPrivateIndividualPath = FrameIO.PARENT_FOLDER + "resources-" + userName + File.separator; 56 if (AuthenticatorBrowser.isAuthenticationRequired()) { 57 System.err.println("**** AudioPathManager::changeParentAndSubFolder(): Setting 'audio' folder to resources-" + userName); 58 } 59 //AUDIO_HOME_DIRECTORY = resourcesPrivateIndividualPath + "audio" + File.separatorChar; 60 AUDIO_HOME_DIRECTORY = FrameIO.AUDIO_PRIVATE_PATH; 61 } 62 else { 63 AUDIO_HOME_DIRECTORY = FrameIO.PARENT_FOLDER + "audio" + File.separatorChar;; 64 } 65 } 66 67 public static void activateAndScanAudioDir() 68 { 69 changeParentAndSubFolders(FrameIO.PARENT_FOLDER); 70 71 // Ensure audio directory exists 72 File dir = new File(AUDIO_HOME_DIRECTORY); 73 if (!dir.exists()) { 74 if (!dir.mkdir()) { 75 ApolloSystemLog.println("Unable to create missing audio home directory"); 76 } 77 } 78 45 46 // TODO: this function called during startup? In browser construction? When FrameIO.changeParentAndSubFolders() is called? Separation of apollo from expeditee core? 47 public static void activateAndScanAudioDirs() { 48 // If we are in old regime then there is no AUDIO_USERNAME_PRIVATE_PATH. 49 AUDIO_USERNAME_HOME_DIRECTORY = FrameIO.AUDIO_USERNAME_PRIVATE_PATH == null ? FrameIO.AUDIO_PATH : FrameIO.AUDIO_USERNAME_PRIVATE_PATH; 50 79 51 // Load counter 80 loadCounter(AUDIO_HOME_DIRECTORY); 81 } 82 52 loadCounter(); 53 } 83 54 84 55 /** 85 56 * Loads the counter 86 * @param dir The dir where the audio files live. 87 */ 88 private static void loadCounter(String audio_dir_str) { 89 File audio_dir = new File(audio_dir_str); 90 91 String[] files = getAudioFileNames(audio_dir); 92 if (files == null) { // Due to IO exception 93 ApolloSystemLog.printError("Failed to read audio directory"); 94 return; 95 } 57 */ 58 private static void loadCounter() { 59 List<String> audioFileNames = getAudioFileNames(); 60 96 61 long count = 0; 97 if (files != null) { 98 for (String f : files) { 99 100 int start = -1, end = -1; 101 for (int i = 0; i < f.length(); i++) { 102 if (Character.isDigit(f.charAt(i)) && start == -1) { 103 start = i; 104 } else if (!Character.isDigit(f.charAt(i)) && start != -1) { 105 end = i; 106 break; 107 } 108 } 109 110 if (start >= 0 && end == -1) { 111 end = f.length(); 112 } 113 114 if (start > 0 && end >= start) { // not start >= 0 since missing preable 115 116 String tmp = f.substring(start, end); 117 long l = Long.parseLong(tmp); 118 if (l >= count) 119 count = l + 1; 120 } 121 } 122 } 123 62 for (String f : audioFileNames) { 63 64 int start = -1, end = -1; 65 for (int i = 0; i < f.length(); i++) { 66 if (Character.isDigit(f.charAt(i)) && start == -1) { 67 start = i; 68 } else if (!Character.isDigit(f.charAt(i)) && start != -1) { 69 end = i; 70 break; 71 } 72 } 73 74 if (start >= 0 && end == -1) { 75 end = f.length(); 76 } 77 78 if (start > 0 && end >= start) { // not start >= 0 since missing preable 79 80 String tmp = f.substring(start, end); 81 long l = Long.parseLong(tmp); 82 if (l >= count) 83 count = l + 1; 84 } 85 } 124 86 counter = count; 125 }126 127 /**128 * @param dir129 * @return130 * Null if an IO exception occurs131 */132 private static String[] getAudioFileNames(File dir) {133 return dir.list(new RegExpFileFilter("^[A-Z]+\\d+.*$"));134 87 } 135 88 … … 140 93 * read list of files. 141 94 */ 142 public static String[] getAudioFileNames() { 143 File audioDir = new File(AUDIO_HOME_DIRECTORY); 144 if (!audioDir.exists() || !audioDir.isDirectory()) return null; 145 return getAudioFileNames(audioDir); 95 public static List<String> getAudioFileNames() { 96 Predicate<File> isFile = file -> { 97 File dirForFile = file.toPath().getParent().toFile(); 98 if (!dirForFile.exists()) { 99 if (!dirForFile.mkdir()) { 100 ApolloSystemLog.println("Unable to create missing audio directory: " + dirForFile.toString()); 101 } 102 } 103 if (file.isFile()) { 104 return true; 105 } 106 return false; 107 }; 108 List<File> allFilesInAudioDirectories = ResourceManager.gatherAudioResources(isFile, DisplayController.getCurrentFrame()); 109 List<String> audioFileNames = new ArrayList<String>(); 110 RegExpFileFilter regExpFileFilter = new RegExpFileFilter("^[A-Z]+\\d+.*$"); 111 for (File file: allFilesInAudioDirectories) { 112 if (regExpFileFilter.accept(file, file.getAbsolutePath())) { 113 audioFileNames.add(file.getName()); 114 } 115 } 116 return audioFileNames; 146 117 } 147 118 … … 168 139 String filename = generateRandomFilenameBase() + "." + extension; 169 140 170 File f = new File(AUDIO_ HOME_DIRECTORY + filename);141 File f = new File(AUDIO_USERNAME_HOME_DIRECTORY + filename); 171 142 172 143 if (AudioStructureModel.getInstance().getTrackGraphInfo(filename, null) != null || … … 206 177 207 178 // Build the filename string 208 return preamble + counter;179 return UserSettings.UserName.get() + preamble + counter; 209 180 210 181 } … … 222 193 223 194 // Ensure that the name base file exists. Assumes the audio home directory exists. 224 File vnameBase = new File(AUDIO_ HOME_DIRECTORY + VIRTUAL_NAME_BASEFILE);195 File vnameBase = new File(AUDIO_USERNAME_HOME_DIRECTORY + VIRTUAL_NAME_BASEFILE); 225 196 if (!vnameBase.exists()) { 226 197 try {
Note:
See TracChangeset
for help on using the changeset viewer.