-
Notifications
You must be signed in to change notification settings - Fork 0
/
droplr.js
executable file
·117 lines (100 loc) · 2.77 KB
/
droplr.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
#!/usr/bin/env node
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs');
const path = require('path');
const Droplr = require('droplr-api');
const moment = require('moment');
const configFile = path.resolve( __dirname, 'config.json' );
/**
* Make sure the config file exists before trying to use it.
*/
if( !fs.existsSync(configFile) ) {
//console.error("Couldn't find config.json. Did you remember to setup your credentials?");
exec(`notify-send -c transfer.error "Couldn't find config.json. Did you remember to setup your credentials?"`);
process.exit();
}
const config = require(configFile);
const state = {
dir: path.resolve(__dirname, config.tempDir),
filename: `screenshot_${ moment().format('Y-MM-DD_HH:mm:ss') }.png`,
filePath: '',
};
/**
* Create a new authorized client for the API.
*
* @type {Client}
*/
const client = new Droplr.Client({
auth: new Droplr.BasicAuth(config.username, config.password),
});
/**
* Copy the screenshot's shortlink to the system clipboard and remove the temp screenshot file.
*
* @param url
*/
const copyClipLink = (url) => {
exec(`echo ${url} | tr -d '[:space:]' | xsel -ib`);
exec(`notify-send -c transfer.complete 'Drop created' 'Share link ${url} copied to clipboard.'`);
exec(`rm ${state.filePath}`);
};
/**
* Post the screenshot to Droplr.
*/
const postClip = () => {
client.drops.create({
type: 'FILE',
variant: 'image/png',
title: state.filename,
content: fs.createReadStream(state.filePath),
}).then((response) => {
copyClipLink(response.shortlink);
}, (error) => {
//console.error(error);
exec(`notify-send -c transfer.error "Unable to publish screenshot." "${error}"`);
});
}
/**
* Create the screenshot.
*
* @returns {Promise<void>}
*/
const createClip = async () => {
try {
const { stdout, stderr } = await exec( `gnome-screenshot -a -f ${state.filePath}` );
if ( stderr ) {
//console.error('stderr:', stderr);
//exec(`notify-send -c transfer.error "Unable to create screenshot." "${stderr}"`);
//process.exit();
}
} catch ( error ) {
//console.error('error:', error);
//exec(`notify-send -c transfer.error "Unable to create screenshot." "${error}"`);
//process.exit();
}
postClip();
}
/**
* Set the state values necessary for the script to run properly.
*/
const handleInitialState = () => {
if ( !fs.existsSync(state.dir) ) {
exec(`mkdir -p ${state.dir}`);
}
state.filePath = path.resolve(__dirname, config.tempDir, state.filename);
}
/**
* Run this thing.
*
* @returns {Promise<void>}
*/
const init = async () => {
try {
await handleInitialState();
createClip();
} catch ( error ) {
//console.error('error:', error);
exec(`notify-send -c transfer.error "Unable to create screenshot." "${error}"`);
}
};
init();