Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Merge branch 'multi-services'
Browse files Browse the repository at this point in the history
Conflicts:
	app.js
	settings.json.default
  • Loading branch information
Hydrog3n committed Jul 8, 2015
2 parents f14c543 + 711174e commit e86fb5c
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
settings.json
settings.json
.settings/
79 changes: 30 additions & 49 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,49 @@
var chokidar = require('chokidar');
var request = require('request');
var fs = require('fs');
var copy = require("copy-paste");
var notifier = require('node-notifier');
var fs = require('fs');
var settings = require('./settings.json');
var p = require('path');
var debug = require('debug')('upload-screenshot');
var async = require('async');

var service = settings.services[settings.used];

var watcher = chokidar.watch(settings.dir, {
ignoreInitial: true,
persistent: true,
ignored: /[\/\\]\./ //ignore dotfiles
});

var q = async.queue(function(path, callback) {

var formData = {
upload: fs.createReadStream(path)
};

var form = {
key: settings.key ? settings.key : undefined
};

debug('Posting picture from path %s to url %s', path, settings.urlapi)

request.post(settings.urlapi, {formData: formData, form: form}, function (err, res, body) {

if (err) {
notifier.notify({
'title': 'Error !',
'message': err
});

return console.error('upload failed:', err);
}

var response = JSON.parse(body);
try {
var Serv = require('./services/'+service.name);
var s = new Serv(service);

} catch(e) {
console.error(e);
process.exit(1);
}

if (response.status_code !== 200) {
return notifier.notify({
'title': 'Error while updating screenshot!',
'message': response.status_txt
});
var q = async.queue(function(path, callback) {

}
s.upload(path, function(shortlink){
copyShortLink(shortlink, path);
});
callback();
}, 1);

var shortlink = response.data.image_short_url;
function copyShortLink(shortlink, path) {

//copy to clipboard
copy.copy(shortlink, function() {
notifier.notify({
'title': 'Uploaded',
'message': shortlink,
'appIcon': __dirname + '/icones/up.png',
'contentImage': path,
'open': shortlink
});
copy.copy(shortlink, function() {
notifier.notify({
'title': 'Uploaded',
'message': shortlink,
'appIcon': __dirname + '/icones/up.png',
'contentImage': path,
'open': shortlink
});
});
callback();
}, 1);
}

function isPicture(path) {
return !!~['jpeg','jpg','png','gif','bmp','ico']
Expand All @@ -75,8 +57,7 @@ watcher.on('add', function(path) {
fs.exists(path, function(exist) {
if(!exist)
return;

q.push(path)
})

})

q.push(path);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"copy-paste": "^1.1.2",
"debug": "^2.2.0",
"fs": "0.0.2",
"imgur": "^0.1.5",
"node-notifier": "^4.2.3",
"path": "^0.11.14",
"request": "^2.58.0"
Expand Down
10 changes: 7 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Upload screenshot

Multi OS app for upload your screen on a chevereto hosting (if API enable).
You can use with my image hosting.
Multi-OS app for upload your screen on multiple image hosting (chevereto, imgur).

# Requirements

Expand Down Expand Up @@ -43,4 +42,9 @@ vim settings.json
```bash
npm install
pm2 start app.js
```
```

# BUGS

I test my app on Mac OS X (10.10.4).
Please if you use an others OS add a new issue with your OS.
41 changes: 41 additions & 0 deletions services/chevereto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var request = require('request');
var fs = require('fs');
var notifier = require('node-notifier');

function Chevereto(service) {
this.service = service;
};

Chevereto.prototype.upload = function(path, callback) {
var formData = {
upload: fs.createReadStream(path)
};

var form = {
key: this.service.key ? this.service.key : undefined
};

request.post(this.service.urlapi, {formData: formData, form: form}, function (err, res, body) {
if (err) {
notifier.notify({
'title': 'Error !',
'message': err
});

return console.error('upload failed:', err);
}

var response = JSON.parse(body);

if (response.status_code !== 200) {
return notifier.notify({
'title': 'Error while updating screenshot!',
'message': response.status_txt
});
}

callback(response.data.image_short_url);
});
};

module.exports = Chevereto;
25 changes: 25 additions & 0 deletions services/imgur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var img = require('imgur');
var notifier = require('node-notifier');

function Imgur(service) {
this.service = service;
};

Imgur.prototype.upload = function(path,callback) {

img.setClientId(this.service.clientId);

img.uploadFile(path)
.then(function (json) {
callback(json.data.link);
})
.catch(function (err) {
console.error(err);
notifier.notify({
'title': 'Error !',
'message': err
});
});
};

module.exports = Imgur;
40 changes: 40 additions & 0 deletions services/zupmage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var request = require('request');
var fs = require('fs');
var notifier = require('node-notifier');

function Zupmage(service) {
this.service = service;
};

Zupmage.prototype.upload = function(path, callback) {

var formData = {
f: fs.createReadStream(path),
k: this.service.k,
prive: this.service.prive
};

request.post("http://www.zupmage.eu/api", {formData: formData}, function (err, res, body) {
if (err) {
notifier.notify({
'title': 'Error !',
'message': err
});

return console.error('upload failed:', err);
}

var response = JSON.parse(body);

if (response.error !== false) {
return notifier.notify({
'title': 'Error while updating screenshot!',
'message': response.error
});
}

callback(response.url.viewer);
});
};

module.exports = Zupmage;
21 changes: 18 additions & 3 deletions settings.json.default
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
{
"urlapi":"http://pix.hydrog3n.fr/api",
"key":"",
"dir":"/Users/hydrog3n/Screenshots"
"used":0,
"dir":"/Users/hydrog3n/Screenshots",
"services": [
{
"name":"chevereto",
"urlapi":"http://pix.hydrog3n.fr/api",
"key":""
},
{
"name":"imgur",
"clientId":"YourClientID"
},
{
"name":"zupmage",
"k":"YourAPIKey",
"prive":1
}
]
}

0 comments on commit e86fb5c

Please sign in to comment.