-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnipcatcher.js
352 lines (291 loc) · 10.2 KB
/
snipcatcher.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const version = '1.0.0';
const creators = ['Sebastian Doe', 'Alex Walker'];
// --------------------------------------------------------- //
const process = require('process');
const os = require('os');
const fs = require('fs');
const hidefile = require('hidefile');
const path = require('path');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const sizeOf = require('image-size');
const chalk = require('chalk');
console.log(chalk.green(`SnipCatcher: v${version}`));
console.log(`Creators: ${creators.join(', ')}\n`);
const username = os.userInfo().username;
const staticDesktop = `C:\\Users\\${username}\\Documents\\`;
var desktopPath = `C:\\Users\\${username}\\Desktop\\`;
const usualDefaultLocation = `C:\\Users\\${username}\\AppData\\Local\\Packages\\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\\TempState\\ScreenClip\\`;
let isDefaultPath;
if (fs.existsSync(usualDefaultLocation)) {
isDefaultPath = true;
} else {
isDefaultPath = false;
}
if (fs.existsSync(staticDesktop + '.snipcatcher-redir.txt')) {
console.log(`Redirect file detected in ${chalk.green('Documents')}\n`);
desktopPath = fs.readFileSync(staticDesktop + '.snipcatcher-redir.txt');
}
console.log(`Using SnipCatcher as`, chalk.green(`${username}\n`));
console.log('View dependencies at https://raw.githubusercontent.com/sd0e/snipcatcher/main/README.md\n');
console.log(`Your screenshots will be saved to ${chalk.green(desktopPath)}\n`);
process.chdir(`C:/Users/${username}/AppData/Local`);
var twirlTimer = (function() {
var P = ["\\", "|", "/", "-"];
var x = 0;
var times = 0;
var s = 0;
return setInterval(function() {
process.stdout.write(chalk.yellow(`\r${P[x++]} Loading Directory (${s}s)`));
x &= 3;
times++;
if (times % 4 === 0) {
s++;
};
}, 250);
})();
twirlTimer;
const createJSONIfNotExist = () => {
const pathToJSON = desktopPath + 'snipcatcher.txt';
if (!fs.existsSync(pathToJSON)) {
fs.writeFileSync(pathToJSON, '');
}
}
createJSONIfNotExist();
const getListOfFiles = () => {
return fs.readFileSync(desktopPath + 'snipcatcher.txt').toString();
}
const hasImageBeenDealtWith = file => {
const listOfFiles = getListOfFiles().split('\n');
for (let i = 0; i < listOfFiles.length; i++) {
const fileInList = listOfFiles[i];
if (fileInList.trim() === file.trim()) {
return true;
}
}
return false;
}
const addImageToJSON = file => {
const pathToJSON = desktopPath + 'snipcatcher.txt';
fs.appendFileSync(pathToJSON, file + '\n');
}
global.allRes = {};
const dealWithImage = file => {
return new Promise(resolve => {
if (!hasImageBeenDealtWith(file)) {
fs.stat(file, (err, stats) => {
const creationTime = stats.birthtimeMs;
foundRelated = false;
sizeOf(file, (err, res) => {
global.allRes[creationTime.toString()] = [file, res.width * res.height];
resolve();
});
});
addImageToJSON(file);
} else {
resolve();
}
});
}
const copyFile = src => {
return new Promise(resolve => {
const desktopFolderPath = desktopPath + 'SnipCatcher\\';
const toCopyPath = desktopFolderPath + path.basename(src);
if (!fs.existsSync(desktopFolderPath)) {
fs.mkdirSync(desktopFolderPath);
}
if (!fs.existsSync(toCopyPath)) {
fs.copyFile(src, toCopyPath, err => {
if (err) throw err;
resolve();
});
} else {
resolve();
}
});
}
const checkAllFinished = () => {
for (i = 0; i < global.allArray.length; i++) {
if (global.allArray[i] !== true) return;
}
// All files have now been added to array
// Sort keys by increasing numerical value
let keys = Object.keys(global.allRes);
for (i = 0; i < keys.length; i++) {
keys[i] = Number(keys[i]);
}
keys.sort((a, b) => a - b);
// Ensure that images are all pairs
for (i = 0; i < keys.length; i += 2) {
const thisKey = keys[i];
const nextKey = keys[i + 1];
if (nextKey - thisKey === NaN) {
allRes[Number(thisKey) + 0.1] = [false, 65520];
checkAllFinished();
return;
} else if (nextKey - thisKey > 1000) {
allRes[Number(thisKey) + 0.1] = [false, 65520];
checkAllFinished();
return;
}
}
// Loop through every pair of images
for (i = 0; i < keys.length; i += 2) {
const thisKey = keys[i];
const nextKey = keys[i + 1] === undefined ? keys[i] : keys[i + 1];
if (nextKey - thisKey <= 1000) {
let keyToCopy;
if (global.allRes[thisKey][1] == 65520 && global.allRes[nextKey][1] == 65520) {
keyToCopy = thisKey;
} else if (global.allRes[thisKey][1] == 65520) {
keyToCopy = nextKey;
} else if (global.allRes[nextKey][1] == 65520) {
keyToCopy = thisKey;
} else {
keyToCopy = global.allRes[thisKey][1] >= global.allRes[nextKey][1] ? thisKey : nextKey;
}
copyFile(global.allRes[keyToCopy][0]).then(() => {
console.log(chalk.green(path.basename(global.allRes[keyToCopy][0])), 'copied to the', chalk.green('SnipCatcher'), 'folder at', `${chalk.green(desktopPath)}\n`);
setTimeout(alertWatching, 500);
});
}
}
}
const alertWatching = () => {
// Prevent alert spamming by limiting rate
if (new Date().getTime() - global.timeSinceLastAlert < 500) return;
console.log(chalk.green('\nWatching for new files...'));
console.log('Press', chalk.green('Ctrl + X'), 'at any time to change the save directory\n\n');
global.timeSinceLastAlert = new Date().getTime();
}
const directoryFound = directory => {
const filesInDirectory = fs.readdirSync(directory);
let numberOfImages = 0;
for (let i = 0; i < filesInDirectory.length; i++) {
const file = filesInDirectory[i];
const ext = path.extname(file);
if (ext === '.png' || ext === '.jpg') {
numberOfImages++;
}
}
global.allArray = new Array(numberOfImages);
let currentNumOfImage = 0;
for (let i = 0; i < filesInDirectory.length; ++i) {
const file = filesInDirectory[i];
const ext = path.extname(file);
if (ext === '.png' || ext === '.jpg') {
const thisImageNumber = currentNumOfImage;
if (!global.inProgress) {
dealWithImage(directory + file).then(() => {
global.allArray[thisImageNumber] = true;
checkAllFinished();
});
}
currentNumOfImage++;
}
}
// Watch for new files
global.numOfNewFiles = 0;
fs.watch(directory, (eventType, fileName) => {
const ext = path.extname(fileName);
if (eventType === 'change') {
if (ext === '.png' || ext === '.jpg') {
global.numOfNewFiles++;
if (global.numOfNewFiles % 2 === 0) {
global.newEventSince = true;
console.log(chalk.yellow('New screenshot detected\n'));
dealWithImage(directory + fileName).then(() => {
global.allArray[1] = true;
checkAllFinished();
});
} else {
global.allRes = {};
global.allArray = new Array(2);
global.newEventSince = false;
dealWithImage(directory + fileName).then(() => {
global.allArray[0] = true;
checkAllFinished();
});
setTimeout(() => {
if (global.newEventSince === false) {
global.numOfNewFiles = 0;
global.allArray = global.allArray[0] === true ? [true] : [false];
checkAllFinished();
}
}, 1500);
}
}
}
});
}
if (isDefaultPath) {
clearInterval(twirlTimer);
setTimeout(alertWatching, 500);
console.log(chalk.green('\n\nDirectory found\n'));
directoryFound(usualDefaultLocation);
} else {
exec('dir ScreenClip /s', { cwd: `C:/Users/${username}/AppData/Local` }).then(res => {
clearInterval(twirlTimer);
setTimeout(alertWatching, 500);
console.log(chalk.green('\n\nDirectory found\n'));
const directory = res.stdout.split('Directory of ')[1].split('\\TempState')[0] + '\\TempState\\ScreenClip\\';
directoryFound(directory);
}).catch(err => {
// If folder doesn't exist
clearInterval(twirlTimer);
if (err.stderr.includes('File Not Found')) {
console.log(chalk.red('\n\nNo screenshots exist in the directory. Take a screenshot and run the program again.'));
console.log('Press Ctrl+C to exit.');
process.stdin.resume();
}
});
}
process.on('SIGINT', () => process.exit());
const readline = require('readline');
const setNewLocation = () => {
console.log('By continuing, you allow a new hidden file to be generated in your', chalk.green('Documents'), 'folder\n');
const prompt = require('prompt');
prompt.start();
prompt.get(['New Path', 'Would you like to move your SnipCatcher folder (y/n)'], (err, res) => {
if (err) throw err;
let newPath = res['New Path'];
if (newPath.toLowerCase() === 'desktop') {
newPath = `C:\\Users\\${username}\\Desktop\\`;
} else if (newPath.toLowerCase() === 'documents') {
newPath = `C:\\Users\\${username}\\Documents\\`;
} else if (newPath.toLowerCase() === 'downloads') {
newPath = `C:\\Users\\${username}\\Downloads\\`;
} else if (newPath.toLowerCase() === 'music') {
newPath = `C:\\Users\\${username}\\Music\\`;
} else if (newPath.toLowerCase() === 'pictures') {
newPath = `C:\\Users\\${username}\\Pictures\\`;
} else if (newPath === '') {
console.log(chalk.red('A path must be entered.\n'));
return;
}
newPath = newPath.slice(newPath.length - 1) === '\\' ? newPath : newPath + '\\';
newPath = newPath.replace(/\//g, '\\');
if (!fs.existsSync(newPath)) {
fs.mkdirSync(newPath, { recursive: true });
}
// Move snipcatcher.txt to new directory
fs.renameSync(desktopPath + 'snipcatcher.txt', newPath + 'snipcatcher.txt');
const moveFolderChoice = res['Would you like to move your SnipCatcher folder (y/n)'].toLowerCase();
if (moveFolderChoice === 'y' || moveFolderChoice === 'yes') {
if (fs.existsSync(desktopPath + 'SnipCatcher')) {
fs.renameSync(desktopPath + 'SnipCatcher', newPath + 'SnipCatcher');
}
}
fs.writeFileSync(staticDesktop + 'snipcatcher-redir.txt', newPath);
hidefile.hideSync(staticDesktop + 'snipcatcher-redir.txt');
desktopPath = newPath;
console.log('\nDirectory successfully changed to', chalk.green(desktopPath) + '\n');
});
}
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.name === 'x' && key.ctrl) {
setNewLocation();
}
});