-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.js
201 lines (169 loc) · 6.67 KB
/
FileManager.js
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
class FileManager {
constructor(flipper, showAlert) {
this.flipper = flipper;
this.showAlert = showAlert;
this.deviceTypeMap = {
'ACS': 'AC',
'AIR_PURIFIERS': 'Air Purifier',
'AUDIO_AND_VIDEO_RECEIVERS': 'AV Receiver',
'BIDET': 'Bidet',
'BLU-RAY': 'Blu-Ray',
'CCTV': 'CCTV',
'CD_PLAYERS': 'CD Player',
'CABLE_BOXES': 'Cable Box',
'CAMERAS': 'Camera',
'CAR_MULTIMEDIA': 'Car Multimedia',
'CONSOLES': 'Game Console',
'CONVERTERS': 'Converter',
'DVB-T': 'DVB-T',
'DVD_PLAYERS': 'DVD Player',
'DIGITAL_SIGNS': 'Digital Sign',
'FANS': 'Fan',
'FIREPLACES': 'Fireplace',
'HEAD_UNITS': 'Head Unit',
'HEATERS': 'Heater',
'HUMIDIFIERS': 'Humidifier',
'LED_LIGHTING': 'LED Light',
'MONITORS': 'Monitor',
'PROJECTORS': 'Projector',
'SOUNDBARS': 'Soundbar',
'SPEAKERS': 'Speaker',
'STREAMING_DEVICES': 'Streaming Device',
'TVS': 'TV',
'VACUUM_CLEANERS': 'Vacuum',
'VIDEOCONFERENCING': 'Video Conference'
};
this.modelPatterns = [
/^[A-Z]{0,3}\d{2,3}[A-Z]{2,3}\d{3,4}[A-Z]?$/,
/^[A-Z]+\d{3,4}[A-Z]?$/,
/^[A-Z]{2,3}\d{2,3}[A-Z]\d{2,3}[A-Z]?(_20\d{2})?$/
];
this.brandPattern = /^[A-Z]{2,15}$/;
}
async scanDirectory(path) {
const files = await this.flipper.listDirectory(path);
const irFiles = files.filter(f => f.name.endsWith('.ir') && !f.name.startsWith('.'));
const directories = files.filter(f => f.isDirectory);
const pathComponents = path.split('/').filter(p => p);
const isInIRDB = pathComponents.some(comp => comp.toUpperCase() === 'IRDB');
let processedFiles = 0;
const totalFiles = irFiles.length;
const processFile = async (file) => {
processedFiles++;
try {
const content = await this.flipper.readFile(file.path);
let metadata = this.parseIRMetadata(content);
if (metadata) {
return { file, metadata, content };
}
if (!isInIRDB) {
metadata = this.guessMetadata(file.name, pathComponents);
if (metadata) {
return { file, metadata, content };
}
}
} catch (fileErr) {
console.error(`Error reading ${file.name}:`, fileErr);
}
return null;
};
const results = [];
for (let i = 0; i < irFiles.length; i += 3) {
const chunk = irFiles.slice(i, i + 3);
const chunkResults = await Promise.all(chunk.map(processFile));
results.push(...chunkResults.filter(r => r !== null));
}
for (const dir of directories) {
const dirResults = await this.scanDirectory(dir.path);
results.push(...dirResults);
}
return results;
}
parseIRMetadata(content) {
const required = ['brand', 'device_type', 'model'];
const metadata = {};
const lines = content.split('\n');
for (let i = 0; i < lines.length && required.length > 0; i++) {
const line = lines[i].trim();
if (!line.startsWith('#')) continue;
const [key, ...valueParts] = line.substring(2).split(':');
const cleanKey = key.toLowerCase().replace(/ /g, '_');
if (required.includes(cleanKey)) {
metadata[cleanKey] = valueParts.join(':').trim();
required.splice(required.indexOf(cleanKey), 1);
}
}
return required.length === 0 ? metadata : null;
}
guessMetadata(filename, pathComponents) {
const parts = filename.replace('.ir', '').split('_');
if (parts.length < 2) return null;
let deviceType = null;
for (const component of pathComponents) {
const upperComponent = component.toUpperCase().replace(/\s+/g, '_');
if (this.deviceTypeMap[upperComponent]) {
deviceType = this.deviceTypeMap[upperComponent];
break;
}
}
if (!deviceType) return null;
const brand = parts[0].toUpperCase();
const model = parts.slice(1).join('_').toUpperCase();
if (!this.brandPattern.test(brand)) return null;
if (!this.modelPatterns.some(pattern => pattern.test(model))) return null;
return {
brand,
model,
device_type: deviceType,
isGuessed: true
};
}
async saveMetadataToFile(path, content, metadata) {
const metadataComments = [
'# Brand: ' + metadata.brand,
'# Model: ' + metadata.model,
'# Device Type: ' + metadata.device_type
].join('\n');
const lines = content.split('\n');
let insertPosition = 0;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('Version:')) {
insertPosition = i + 1;
break;
}
}
lines.splice(insertPosition, 0, metadataComments);
const newContent = lines.join('\n');
await this.flipper.writeFile(path, newContent);
}
formatFileSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
}
groupFilesByMetadata(files) {
const grouped = {
device_type: {},
brand: {},
all: files
};
Object.entries(files).forEach(([id, file]) => {
const { metadata } = file;
if (!metadata) return;
if (metadata.device_type) {
if (!grouped.device_type[metadata.device_type]) {
grouped.device_type[metadata.device_type] = {};
}
grouped.device_type[metadata.device_type][id] = file;
}
if (metadata.brand) {
if (!grouped.brand[metadata.brand]) {
grouped.brand[metadata.brand] = {};
}
grouped.brand[metadata.brand][id] = file;
}
});
return grouped;
}
}
export default FileManager;