-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageManager.java
More file actions
338 lines (288 loc) · 11.4 KB
/
Copy pathStorageManager.java
File metadata and controls
338 lines (288 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Enumeration;
public class StorageManager {
private static String basePath = null;
private static final String APP_FOLDER = "VidmateME"; // ✅ CHANGED: Back to VidmateME
/**
* ✅ NEW: Auto-detect best available storage path
* Priority: E:/ → C:/ → TFCard → Other roots
*/
public static String getDownloadPath() {
if (basePath != null) {
return basePath;
}
// Try to get from settings first
try {
String settingsPath = SettingsManager.getInstance().getStoragePath();
if (settingsPath != null && settingsPath.length() > 0) {
// Extract root from settings path
if (testAndCreatePath(settingsPath)) {
basePath = settingsPath;
return basePath;
}
}
} catch (Exception e) {
// Settings not available, continue auto-detection
}
// ✅ NEW: Auto-detect available roots
String detectedPath = autoDetectStoragePath();
if (detectedPath != null) {
basePath = detectedPath;
saveToSettings(basePath);
return basePath;
}
// Fallback to default
basePath = "file:///E:/" + APP_FOLDER + "/";
saveToSettings(basePath);
return basePath;
}
/**
* ✅ NEW: Auto-detect best storage path
*/
private static String autoDetectStoragePath() {
String[] priorityPaths = {
"file:///E:/", // SD card (most common)
"file:///C:/", // Internal memory
"file:///TFCard/", // Some devices
"file:///MemoryCard/", // Alternative name
"file:///SDCard/", // Some Android-based
"file:///F:/", // Alternative drive letter
"file:///D:/" // Another alternative
};
// Try each path in priority order
for (int i = 0; i < priorityPaths.length; i++) {
String testPath = priorityPaths[i] + APP_FOLDER + "/";
if (testAndCreatePath(testPath)) {
return testPath;
}
}
// ✅ NEW: Try to enumerate file system roots
try {
String roots = System.getProperty("fileconn.dir.roots");
if (roots != null && roots.length() > 0) {
// Parse roots (usually comma-separated)
int start = 0;
while (start < roots.length()) {
int end = roots.indexOf(',', start);
if (end == -1) end = roots.length();
String root = roots.substring(start, end).trim();
if (root.length() > 0) {
if (!root.startsWith("file://")) {
root = "file:///" + root;
}
if (!root.endsWith("/")) {
root += "/";
}
String testPath = root + APP_FOLDER + "/";
if (testAndCreatePath(testPath)) {
return testPath;
}
}
start = end + 1;
}
}
} catch (Exception e) {
// Root enumeration failed
}
return null; // No valid path found
}
/**
* ✅ NEW: Test if path is accessible and create VidmateME folder
*/
private static boolean testAndCreatePath(String path) {
FileConnection fc = null;
try {
// Ensure path ends with /
if (!path.endsWith("/")) {
path += "/";
}
fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);
// Create directory if it doesn't exist
if (!fc.exists()) {
fc.mkdir();
}
// Test if we can write
if (fc.canWrite()) {
// Create subdirectories
createSubfolders(path);
fc.close();
return true;
}
fc.close();
return false;
} catch (Exception e) {
try { if (fc != null) fc.close(); } catch (Exception ex) {}
return false;
}
}
/**
* ✅ NEW: Create VidmateME subdirectories (videos, audios, thumbnails)
*/
private static void createSubfolders(String basePath) {
String[] subfolders = {"videos/", "audios/", "thumbnails/"};
for (int i = 0; i < subfolders.length; i++) {
FileConnection fc = null;
try {
String fullPath = basePath + subfolders[i];
fc = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);
if (!fc.exists()) {
fc.mkdir();
}
fc.close();
} catch (Exception e) {
try { if (fc != null) fc.close(); } catch (Exception ex) {}
}
}
}
/**
* ✅ NEW: Save detected path to settings
*/
private static void saveToSettings(String path) {
try {
SettingsManager.getInstance().setStoragePath(path);
SettingsManager.getInstance().saveSettings();
} catch (Exception e) {
// Ignore if settings not available
}
}
/**
* ✅ NEW: Get storage info (for diagnostic)
*/
public static String getStorageInfo() {
StringBuffer info = new StringBuffer();
info.append("=== STORAGE INFO ===\n\n");
String currentPath = getDownloadPath();
info.append("Current path:\n").append(currentPath).append("\n\n");
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(currentPath, Connector.READ);
if (fc.exists()) {
long totalSize = fc.totalSize();
long availSize = fc.availableSize();
long usedSize = totalSize - availSize;
info.append("Total space: ").append(formatSize(totalSize)).append("\n");
info.append("Used space: ").append(formatSize(usedSize)).append("\n");
info.append("Free space: ").append(formatSize(availSize)).append("\n");
info.append("Writable: ").append(fc.canWrite() ? "Yes" : "No").append("\n");
} else {
info.append("Status: Path not found\n");
}
fc.close();
} catch (Exception e) {
info.append("Error: ").append(e.getMessage()).append("\n");
} finally {
try { if (fc != null) fc.close(); } catch (Exception e) {}
}
// ✅ NEW: Test alternative paths
info.append("\n=== AVAILABLE STORAGE ===\n\n");
String[] testPaths = {
"file:///E:/",
"file:///C:/",
"file:///TFCard/",
"file:///MemoryCard/"
};
for (int i = 0; i < testPaths.length; i++) {
fc = null;
try {
fc = (FileConnection) Connector.open(testPaths[i], Connector.READ);
if (fc.exists()) {
long avail = fc.availableSize();
info.append("[OK] ").append(testPaths[i]).append(" (")
.append(formatSize(avail)).append(" free)\n");
} else {
info.append("[X] ").append(testPaths[i]).append(" (not found)\n");
}
fc.close();
} catch (Exception e) {
info.append("[X] ").append(testPaths[i]).append(" (error)\n");
} finally {
try { if (fc != null) fc.close(); } catch (Exception e) {}
}
}
return info.toString();
}
private static String formatSize(long bytes) {
if (bytes < 0) return "Unknown";
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 * 1024) return (bytes / 1024) + " KB";
return (bytes / (1024 * 1024)) + " MB";
}
// ========== EXISTING METHODS (kept for compatibility) ==========
public static OutputStream openOutputStream(String filePath, boolean append) throws Exception {
FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
if (!fc.exists()) {
fc.create();
}
OutputStream os;
if (append) {
os = fc.openOutputStream(fc.fileSize());
} else {
os = fc.openOutputStream();
}
return os;
}
public static InputStream openInputStream(String filePath) throws Exception {
FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ);
if (!fc.exists()) {
fc.close();
throw new Exception("File not found: " + filePath);
}
return fc.openInputStream();
}
public static boolean fileExists(String filePath) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(filePath, Connector.READ);
boolean exists = fc.exists();
fc.close();
return exists;
} catch (Exception e) {
try { if (fc != null) fc.close(); } catch (Exception ex) {}
return false;
}
}
public static void deleteFile(String filePath) throws Exception {
FileConnection fc = (FileConnection) Connector.open(filePath, Connector.READ_WRITE);
if (fc.exists()) {
fc.delete();
}
fc.close();
}
public static long getFileSize(String filePath) {
FileConnection fc = null;
try {
fc = (FileConnection) Connector.open(filePath, Connector.READ);
if (!fc.exists()) {
fc.close();
return 0;
}
long size = fc.fileSize();
fc.close();
return size;
} catch (Exception e) {
try { if (fc != null) fc.close(); } catch (Exception ex) {}
return 0;
}
}
/**
* ✅ NEW: Force re-detection of storage path
*/
public static void resetStoragePath() {
basePath = null;
getDownloadPath(); // Trigger re-detection
}
/**
* ✅ NEW: Manually set storage path
*/
public static void setStoragePath(String path) {
if (testAndCreatePath(path)) {
basePath = path;
saveToSettings(path);
} else {
throw new RuntimeException("Invalid storage path: " + path);
}
}
}