-
Notifications
You must be signed in to change notification settings - Fork 1
/
iconfinder.js
242 lines (194 loc) · 6.16 KB
/
iconfinder.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
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
const Icns = require('apple-icns');
const resourceFork = require('resourceforkjs').resourceFork;
const fs = require('fs');
const plist = require('plist');
const winIco = require('./windows').default;
const gnomeIcon = require('./gnome');
const _p = require('promisfy');
const exists = _p.promisfyNoError(fs.exists);
const readFile = _p.promisfy(fs.readFile);
const RESOURCEFORK = 'rf';
const APPFOLDER = 'apf';
const VOLUMEFOLDER = 'vf';
const MOUNTVOLUME = 'mvf';
const GNOME = 'gf';
const WINDOWS = 'wf';
class AbstractFile {
constructor(folder) {
this.folder = folder;
this.type = RESOURCEFORK;
this.mount = '/';
}
async init() {
switch(process.platform) {
case 'darwin':
if (/\.app/.test(this.folder)) {
this.type = APPFOLDER;
} else {
let mount = this.folder.split('/');
if (mount[1] == 'Volumes' && mount.length > 2) {
let _mount = '/' + mount[1] + '/' + mount[2] + '/.VolumeIcon.icns';
if (await exists(_mount)) {
this.mount = _mount;
this.type = MOUNTVOLUME;
} else {
this.type = VOLUMEFOLDER;
}
}
}
break;
case 'win32':
this.type = WINDOWS;
break;
case 'linux':
this.type = GNOME;
break;
default:
break;
}
}
read() {
let iconReading = (icon) => new Promise((resolve,reject) => {
try {
icon.open((error) => {
if (error) {
return reject(error);
}
//icon.entries.forEach((entry) => console.log(entry.type));
var found = icon.entries.find((entry) => {
return (entry.type === 'ic07' || entry.type === 'ic08');
})
if (found) {
icon.readEntryData(found, ( error, _buffer ) => {
if (error) {
return reject(error);
}
resolve({buffer:_buffer,format:'image/png'});
});
} else {
reject('Icon not found');
}
})
} catch (exception) {
reject('Cannot open icns ' + exception);
}
});
switch (this.type) {
case RESOURCEFORK:
return new Promise((resolve,reject) => {
let _findIcon = (folder) => new Promise(async (resolve,reject) => {
while (!await exists(folder + '/Icon\r')) {
folder = folder.split('/').slice(0,-1).join('/');
if (folder == "") {
break;
}
}
if (folder == "") {
const data = await readFile('/.VolumeIcon.icns').catch(err => reject(err));
let icon = new Icns(data);
const ret = iconReading(icon).catch(err => reject(err));
resolve(ret);
} else {
let file = new resourceFork(folder + '/Icon\r');
try {
await file.read();
let buffer = Buffer.from(file.resources['icns'][49081].data.buffer,260);
let icon = new Icns(buffer);
const ret = await iconReading(icon);
resolve(ret);
} catch (error) {
reject(error);
}
}
});
_findIcon(this.folder.slice())
.then(ret => resolve(ret))
.catch(error => reject(error));
});
break;
case VOLUMEFOLDER:
return new Promise((resolve,reject) => {
fs.readFile('/.VolumeIcon.icns', (err,data) => {
if (err) {
return reject(err);
}
let icon = new Icns(data);
iconReading(icon)
.then((ret) => resolve(ret))
.catch((err) => reject(err));
});
});
break;
case MOUNTVOLUME:
return new Promise((resolve,reject) => {
fs.readFile(this.mount, (err,data) => {
if (err) {
return reject(err);
}
let icon = new Icns(data);
iconReading(icon)
.then((ret) => resolve(ret))
.catch((err) => reject(err));
});
});
break;
case APPFOLDER:
return new Promise(async (resolve,reject) => {
let findApp = (folders) => {
for (let i = 0; i < folders.length; i++) {
if (folders[i].substr(folders[i].length - 4) === '.app') {
return i;
}
}
return undefined;
}
let folderIndex = findApp(this.folder.split('/'));
if (!folderIndex) {
reject('No app found');
}
let folder = this.folder.split('/').slice(0,folderIndex + 1).join('/');
const data =
await readFile(folder + '/Contents/Info.plist')
.catch(err => reject('No icon found'));
let info = plist.parse(data.toString('utf8'));
if (!info.hasOwnProperty('CFBundleIconFile')) {
reject('No icon found');
}
const iconData =
await readFile(folder
+ '/Contents/Resources/'
+ info.CFBundleIconFile)
.catch(err => reject(err));
let icon = new Icns(iconData);
iconReading(icon)
.then((ret) => resolve(ret))
.catch((err) => reject(err));
});
break;
case GNOME:
return new Promise((resolve,reject) => {
gnomeIcon(this.folder)
.then((img) => resolve(img))
.catch((err) => reject(err));
});
break;
case WINDOWS:
return new Promise((resolve,reject) => {
winIco(this.folder)
.then((png) => resolve({buffer:png,format:'image/png'}))
.catch((err) => reject(err));
});
break;
default:
break;
}
}
}
var iconFinder = (folder) => new Promise((resolve,reject) => {
let file = new AbstractFile(folder);
file.init();
file.read()
.then((buffer) => resolve(buffer))
.catch((error) => reject(error));
});
exports.iconFinder = iconFinder;