forked from ScienceVikings/IconExtractor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwin-iconPromise.js
More file actions
164 lines (143 loc) · 6.03 KB
/
win-iconPromise.js
File metadata and controls
164 lines (143 loc) · 6.03 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
let fs = require('fs');
let child_process = require('child_process');
let os = require('os');
let path = require('path');
let _ = require('lodash');
function IconPromise(){
// Default location of the IconExtractor.exe executable
let processPath = path.join(__dirname,'/bin/IconExtractor.exe');
/**
* Extracts image data from a file's icon and provides it wrapped in a
* promise, with an optional argument that allows for the adjustment of
* size. Default size is 32x32 pixels.
* @param {string} path The file path for the document that we would like to
* extract the icon data of.
* @param {string=} sizeArg The size argument string to be passed through to
* the .NET framework code.
* @param {string=} context A context string that will be returned with the
* JSON object to lend insight into the function caller
* @return {Promise} Returns a promise for a javascript object that contains the
* icon image data.
*/
this.getIcon = function(path, sizeArg = '--size-32', context = "No Context Provided"){
let iconProcess = child_process.spawn(getPlatformIconProcess(), ['--x', sizeArg]);
// Initialize buffer
let iconDataBuffer = "";
// Start process to parse icon data
let json = JSON.stringify({context: context, path: path}) + "\n";
iconProcess.stdin.write(json);
// Return a promise that we will return a json object for the icon:
return new Promise((resolve, reject) => {
// Trigger event on stdout buffer
iconProcess.stdout.on('data', data => {
let str = (new Buffer.from(data, 'utf8')).toString('utf8');
// Add data to buffer
iconDataBuffer += str;
//Wait until later if we do not have the full icon data to parse
if (!_.endsWith(str, '\n')){
return;
}
// There may be more than one event for the buffer; consider each
_.each(iconDataBuffer.split('\n'), function(buf){
// Wait until later if the buffer is empty
if(!buf || buf.length == 0){
return;
}
// Attempt to parse the full buffer
try{
return resolve(JSON.parse(buf));
} catch(ex){
return reject(ex);
}
});
});
// Throw promise reject if error in iconProcess
iconProcess.on('error', err => {
return reject(err.toString());
});
// Throw promise reject if stderr
iconProcess.stderr.on('data', err => {
return reject(err.toString());
});
});
}
/**
* Extracts image data from a file's icon and provides it wrapped in a
* promise, with the size set as 16x16 pixels.
* @param {string} path The file path for the document that we would like to
* extract the icon data of.
* @param {string=} context A context string that will be returned with the
* JSON object to lend insight into the function caller
* @return {Promise} Returns a promise for a javascript object that contains the
* icon image data.
*/
this.getIcon16 = function(path, context = "No Context Provided"){
return this.getIcon(path, '--size-16', context);
}
/**
* Extracts image data from a file's icon and provides it wrapped in a
* promise, with the size set as 32x32 pixels.
* @param {string} path The file path for the document that we would like to
* extract the icon data of.
* @param {string=} context A context string that will be returned with the
* JSON object to lend insight into the function caller
* @return {Promise} Returns a promise for a javascript object that contains the
* icon image data.
*/
this.getIcon32 = function(path, context = "No Context Provided"){
return this.getIcon(path, '--size-32', context);
}
/**
* Extracts image data from a file's icon and provides it wrapped in a
* promise, with the size set as 48x48 pixels.
* @param {string} path The file path for the document that we would like to
* extract the icon data of.
* @param {string=} context A context string that will be returned with the
* JSON object to lend insight into the function caller
* @return {Promise} Returns a promise for a javascript object that contains the
* icon image data.
*/
this.getIcon48 = function(path, context = "No Context Provided"){
return this.getIcon(path, '--size-48', context);
}
/**
* Extracts image data from a file's icon and provides it wrapped in a
* promise, with the size set as 256x256 pixels.
* @param {string} path The file path for the document that we would like to
* extract the icon data of.
* @param {string=} context A context string that will be returned with the
* JSON object to lend insight into the function caller
* @return {Promise} Returns a promise for a javascript object that contains the
* icon image data.
*/
this.getIcon256 = function(path, context = "No Context Provided"){
return this.getIcon(path, '--size-256', context);
}
/**
* Manually sets the path of the IconExtractor executable.
* @param {string} extractorPath The new folder path for the IconExtractor
* executable file.
* @param {string=} newName Indicate the new name of the IconExtractor
* executable file, if it has changed from "IconExtractor.exe". This
* argument does not need the file extension.
*/
this.overrideExtractorPath = function(extractorPath, newName = 'IconExtractor.exe'){
newName = (newName.split('.').pop() == "exe") ? newName : (newName + '.exe');
processPath = path.join(extractorPath, newName);
return;
}
/**
* Confirms that we are not using Windows NT.
* @return {string} Returns the string path of where the IconExtractor
* executable is.
*/
function getPlatformIconProcess(){
if(os.type() == 'Windows_NT'){
return processPath;
//Do stuff here to get the icon that doesn't have the shortcut thing on it
} else {
throw('This platform (' + os.type() + ') is unsupported =(');
}
}
}
module.exports = new IconPromise();