-
Notifications
You must be signed in to change notification settings - Fork 1
/
gphotos.js
80 lines (64 loc) · 1.68 KB
/
gphotos.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
const gphoto = require('upload-gphotos').default;
const fs = require('fs');
module.exports = function (RED)
{
function PhotoAccount (config)
{
RED.nodes.createNode(this, config);
const node = this;
node.loginAttempt = false;
node.account = new gphoto({ username: node.credentials.username, password: node.credentials.password});
}
function UploadPhoto (config)
{
RED.nodes.createNode(this, config);
const node = this;
const fileName = config.fileName;
const albumName = config.albumName;
const photoAccount = RED.nodes.getNode(config.account);
async function upload (fileName, albumName)
{
if(!photoAccount.account.userId && !photoAccount.loginAttempt)
{
photoAccount.loginAttempt = true;
await photoAccount.account.login();
}
const photo = await photoAccount.account.upload(fileName);
const album = await photoAccount.account.searchOrCreateAlbum(albumName);
await album.addPhoto(photo);
}
node.on('input', function(msg)
{
upName = undefined;
upAlbum = undefined;
if( typeof msg.payload === "string")
{
upName = msg.payload;
}
if( msg.payload.fileName)
{
upName = msg.payload.fileName;
}
if( msg.payload.album)
{
upAlbum = msg.payload.album;
}
if(!upName) upName = fileName;
if(!upAlbum) upAlbum = albumName;
node.log(upName);
node.log(upAlbum);
if(upName && upAlbum)
{
node.log("uploading: " + upName + "to album: " + upAlbum);
upload(upName, upAlbum);
}
});
}
RED.nodes.registerType("photo-account", PhotoAccount, {
credentials: {
username: {type: "text"},
password: {type: "password"}
}
});
RED.nodes.registerType("upload-photo", UploadPhoto);
}