node-red-contrib-drive-collection 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +171 -0
- package/drive-config.html +81 -0
- package/drive-config.js +34 -0
- package/drive-download.html +55 -0
- package/drive-download.js +18 -0
- package/drive-upload.html +70 -0
- package/drive-upload.js +21 -0
- package/package.json +25 -0
- package/shared/drive-base.js +52 -0
- package/shared/providers/google-drive.js +59 -0
- package/shared/providers/onedrive.js +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# node-red-contrib-drive-collection
|
|
2
|
+
|
|
3
|
+
Node-RED nodes for Google Drive and OneDrive file operations.
|
|
4
|
+
|
|
5
|
+
Supports upload and download. Each provider has its own config node and operation nodes.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Nodes
|
|
10
|
+
|
|
11
|
+
| Node | Type | Purpose |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `google-drive-config` | config | Google Drive OAuth2 credentials |
|
|
14
|
+
| `google-drive-upload` | operation | Upload a file to Google Drive |
|
|
15
|
+
| `google-drive-download` | operation | Download a file from Google Drive |
|
|
16
|
+
| `onedrive-config` | config | OneDrive OAuth2 credentials |
|
|
17
|
+
| `onedrive-upload` | operation | Upload a file to OneDrive |
|
|
18
|
+
| `onedrive-download` | operation | Download a file from OneDrive |
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Google Drive
|
|
23
|
+
|
|
24
|
+
### `google-drive-config`
|
|
25
|
+
|
|
26
|
+
Shared credentials for all Google Drive nodes.
|
|
27
|
+
|
|
28
|
+
| Field | Description |
|
|
29
|
+
|---|---|
|
|
30
|
+
| Client ID | OAuth2 client ID from Google Cloud Console |
|
|
31
|
+
| Client Secret | Stored as a credential |
|
|
32
|
+
| Refresh Token | Paste manually from OAuth2 Playground or your own auth flow |
|
|
33
|
+
|
|
34
|
+
The access token is refreshed automatically in memory and is **never persisted**.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
### `google-drive-upload`
|
|
39
|
+
|
|
40
|
+
Upload a file to Google Drive.
|
|
41
|
+
|
|
42
|
+
**Input:**
|
|
43
|
+
|
|
44
|
+
| Property | Description |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `msg.payload` | Buffer or readable stream (required) |
|
|
47
|
+
| `msg.filename` | Overrides node filename. Default: timestamp |
|
|
48
|
+
| `msg.folderId` | Google Drive folder ID. Overrides node config |
|
|
49
|
+
| `msg.mimeType` | Overrides node MIME type. Default: `application/octet-stream` |
|
|
50
|
+
|
|
51
|
+
**Output:**
|
|
52
|
+
|
|
53
|
+
| Property | Description |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `msg.payload` | Google Drive file metadata (`{ id, name }`) |
|
|
56
|
+
| `msg.fileId` | ID of the uploaded file |
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### `google-drive-download`
|
|
61
|
+
|
|
62
|
+
Download a file from Google Drive as a readable stream.
|
|
63
|
+
|
|
64
|
+
**Input:**
|
|
65
|
+
|
|
66
|
+
| Property | Description |
|
|
67
|
+
|---|---|
|
|
68
|
+
| `msg.fileId` | Google Drive file ID. Overrides node config |
|
|
69
|
+
|
|
70
|
+
**Output:**
|
|
71
|
+
|
|
72
|
+
| Property | Description |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `msg.payload` | Readable stream of file content |
|
|
75
|
+
|
|
76
|
+
> Output is a stream, not a Buffer. Pipe it to a file writer or another node that consumes streams.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## OneDrive
|
|
81
|
+
|
|
82
|
+
### `onedrive-config`
|
|
83
|
+
|
|
84
|
+
Shared credentials for all OneDrive nodes.
|
|
85
|
+
|
|
86
|
+
| Field | Description |
|
|
87
|
+
|---|---|
|
|
88
|
+
| Tenant ID | Azure AD tenant ID |
|
|
89
|
+
| Client ID | App registration client ID |
|
|
90
|
+
| Client Secret | Stored as a credential |
|
|
91
|
+
| Refresh Token | Paste manually from your OAuth2 flow |
|
|
92
|
+
|
|
93
|
+
The access token is refreshed automatically in memory and is **never persisted**.
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### `onedrive-upload`
|
|
98
|
+
|
|
99
|
+
Upload a file to OneDrive (simple upload, suitable for files up to ~4 MB).
|
|
100
|
+
|
|
101
|
+
**Input:**
|
|
102
|
+
|
|
103
|
+
| Property | Description |
|
|
104
|
+
|---|---|
|
|
105
|
+
| `msg.payload` | Buffer or readable stream (required) |
|
|
106
|
+
| `msg.path` | OneDrive path, e.g. `Documents/file.txt`. Overrides node config |
|
|
107
|
+
|
|
108
|
+
**Output:**
|
|
109
|
+
|
|
110
|
+
| Property | Description |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `msg.payload` | OneDrive file metadata from Graph API |
|
|
113
|
+
| `msg.fileId` | ID of the uploaded file |
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### `onedrive-download`
|
|
118
|
+
|
|
119
|
+
Download a file from OneDrive as a readable stream.
|
|
120
|
+
|
|
121
|
+
**Input:**
|
|
122
|
+
|
|
123
|
+
| Property | Description |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `msg.fileId` | OneDrive item ID. Overrides node config |
|
|
126
|
+
|
|
127
|
+
**Output:**
|
|
128
|
+
|
|
129
|
+
| Property | Description |
|
|
130
|
+
|---|---|
|
|
131
|
+
| `msg.payload` | Readable stream of file content |
|
|
132
|
+
|
|
133
|
+
> Output is a stream, not a Buffer.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Node status
|
|
138
|
+
|
|
139
|
+
| Color | Meaning |
|
|
140
|
+
|---|---|
|
|
141
|
+
| Yellow | In progress |
|
|
142
|
+
| Green | Success |
|
|
143
|
+
| Red | Error |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## OAuth2 Bootstrap
|
|
148
|
+
|
|
149
|
+
These nodes use OAuth2 with a refresh token. You must obtain the refresh token externally:
|
|
150
|
+
|
|
151
|
+
- **Google Drive:** Use [OAuth2 Playground](https://developers.google.com/oauthplayground) with the Drive API scope (`https://www.googleapis.com/auth/drive`)
|
|
152
|
+
- **OneDrive:** Register an app in Azure Portal, then run an auth code flow with the `Files.ReadWrite` scope
|
|
153
|
+
|
|
154
|
+
Paste the refresh token directly into the config node. The nodes do not implement a UI for the initial OAuth flow.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Design
|
|
159
|
+
|
|
160
|
+
- One node per operation
|
|
161
|
+
- Config node as single source of truth for credentials
|
|
162
|
+
- `msg` overrides always win over node config
|
|
163
|
+
- Streams over buffering — download nodes return streams, not Buffers
|
|
164
|
+
- Manual OAuth bootstrap — no hidden auth flows
|
|
165
|
+
- No generalized abstraction layer — Google Drive and OneDrive nodes are independent
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
Apache-2.0
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
<script type="text/html" data-template-name="drive-config">
|
|
2
|
+
<div class="form-row">
|
|
3
|
+
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
4
|
+
<input type="text" id="node-config-input-name" placeholder="Name">
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<hr>
|
|
8
|
+
|
|
9
|
+
<div class="form-row">
|
|
10
|
+
<label for="node-config-input-provider"><i class="fa fa-cloud"></i> Provider</label>
|
|
11
|
+
<select id="node-config-input-provider" style="width:70%">
|
|
12
|
+
<option value="google">Google Drive</option>
|
|
13
|
+
<option value="onedrive">OneDrive</option>
|
|
14
|
+
</select>
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
<hr>
|
|
18
|
+
|
|
19
|
+
<div class="form-row" id="drive-config-row-tenantId">
|
|
20
|
+
<label for="node-config-input-tenantId"><i class="fa fa-building"></i> Tenant ID</label>
|
|
21
|
+
<input type="text" id="node-config-input-tenantId" placeholder="Azure tenant ID" style="width:70%">
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
<div class="form-row">
|
|
25
|
+
<label for="node-config-input-clientId"><i class="fa fa-key"></i> Client ID</label>
|
|
26
|
+
<input type="text" id="node-config-input-clientId" placeholder="OAuth2 client ID" style="width:70%">
|
|
27
|
+
</div>
|
|
28
|
+
<div class="form-row">
|
|
29
|
+
<label for="node-config-input-clientSecret"><i class="fa fa-user-secret"></i> Client Secret</label>
|
|
30
|
+
<input type="password" id="node-config-input-clientSecret" placeholder="OAuth2 client secret" style="width:70%">
|
|
31
|
+
</div>
|
|
32
|
+
<div class="form-row">
|
|
33
|
+
<label for="node-config-input-refreshToken"><i class="fa fa-refresh"></i> Refresh Token</label>
|
|
34
|
+
<input type="password" id="node-config-input-refreshToken" placeholder="OAuth2 refresh token" style="width:70%">
|
|
35
|
+
</div>
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<script type="text/html" data-help-name="drive-config">
|
|
39
|
+
<p>Shared cloud drive credentials. Select a provider and configure OAuth2.</p>
|
|
40
|
+
|
|
41
|
+
<h3>Providers</h3>
|
|
42
|
+
<ul>
|
|
43
|
+
<li><b>Google Drive</b> — Client ID, Client Secret, Refresh Token</li>
|
|
44
|
+
<li><b>OneDrive</b> — Tenant ID, Client ID, Client Secret, Refresh Token</li>
|
|
45
|
+
</ul>
|
|
46
|
+
|
|
47
|
+
<h3>Bootstrap</h3>
|
|
48
|
+
<p>Obtain a refresh token externally and paste it here. The access token is refreshed automatically in memory and is never persisted.</p>
|
|
49
|
+
<ul>
|
|
50
|
+
<li><b>Google Drive:</b> OAuth2 Playground with scope <code>https://www.googleapis.com/auth/drive</code></li>
|
|
51
|
+
<li><b>OneDrive:</b> Azure app registration with scope <code>Files.ReadWrite</code></li>
|
|
52
|
+
</ul>
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<script type="text/javascript">
|
|
56
|
+
RED.nodes.registerType('drive-config', {
|
|
57
|
+
category: 'config',
|
|
58
|
+
defaults: {
|
|
59
|
+
name: { value: '' },
|
|
60
|
+
provider: { value: 'google' },
|
|
61
|
+
tenantId: { value: '' }
|
|
62
|
+
},
|
|
63
|
+
credentials: {
|
|
64
|
+
clientId: { type: 'text' },
|
|
65
|
+
clientSecret: { type: 'password' },
|
|
66
|
+
refreshToken: { type: 'password' }
|
|
67
|
+
},
|
|
68
|
+
label: function() {
|
|
69
|
+
const label = this.provider === 'onedrive' ? 'OneDrive' : 'Google Drive';
|
|
70
|
+
return this.name || label;
|
|
71
|
+
},
|
|
72
|
+
oneditprepare: function() {
|
|
73
|
+
function updateFields() {
|
|
74
|
+
const isOneDrive = $('#node-config-input-provider').val() === 'onedrive';
|
|
75
|
+
$('#drive-config-row-tenantId').toggle(isOneDrive);
|
|
76
|
+
}
|
|
77
|
+
$('#node-config-input-provider').on('change', updateFields);
|
|
78
|
+
updateFields();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
</script>
|
package/drive-config.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const GoogleDriveProvider = require("./shared/providers/google-drive");
|
|
2
|
+
const OneDriveProvider = require("./shared/providers/onedrive");
|
|
3
|
+
|
|
4
|
+
const PROVIDERS = {
|
|
5
|
+
google: GoogleDriveProvider,
|
|
6
|
+
onedrive: OneDriveProvider
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
module.exports = function(RED) {
|
|
10
|
+
function DriveConfigNode(n) {
|
|
11
|
+
RED.nodes.createNode(this, n);
|
|
12
|
+
|
|
13
|
+
this.provider = n.provider;
|
|
14
|
+
this.tenantId = n.tenantId; // OneDrive only — not a credential
|
|
15
|
+
|
|
16
|
+
const ProviderClass = PROVIDERS[this.provider];
|
|
17
|
+
|
|
18
|
+
if (!ProviderClass) {
|
|
19
|
+
this.error(`Unknown provider: ${this.provider}`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.providerInstance = new ProviderClass();
|
|
24
|
+
this.providerInstance.init(this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
RED.nodes.registerType("drive-config", DriveConfigNode, {
|
|
28
|
+
credentials: {
|
|
29
|
+
clientId: { type: "text" },
|
|
30
|
+
clientSecret: { type: "password" },
|
|
31
|
+
refreshToken: { type: "password" }
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script type="text/html" data-template-name="drive-download">
|
|
2
|
+
<div class="form-row">
|
|
3
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
4
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
5
|
+
</div>
|
|
6
|
+
<div class="form-row">
|
|
7
|
+
<label for="node-input-config"><i class="fa fa-cloud"></i> Config</label>
|
|
8
|
+
<input type="text" id="node-input-config">
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<hr>
|
|
12
|
+
|
|
13
|
+
<div class="form-row">
|
|
14
|
+
<label for="node-input-fileId"><i class="fa fa-file"></i> File ID</label>
|
|
15
|
+
<input type="text" id="node-input-fileId" placeholder="File ID (or msg.fileId)" style="width:70%">
|
|
16
|
+
</div>
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<script type="text/html" data-help-name="drive-download">
|
|
20
|
+
<p>Download a file as a readable stream. Provider is determined by the config node.</p>
|
|
21
|
+
|
|
22
|
+
<h3>Input</h3>
|
|
23
|
+
<ul>
|
|
24
|
+
<li><code>msg.fileId</code> — file ID to download, overrides node config</li>
|
|
25
|
+
</ul>
|
|
26
|
+
|
|
27
|
+
<h3>Output</h3>
|
|
28
|
+
<ul>
|
|
29
|
+
<li><code>msg.payload</code> — readable stream of file content</li>
|
|
30
|
+
</ul>
|
|
31
|
+
|
|
32
|
+
<h3>Notes</h3>
|
|
33
|
+
<ul>
|
|
34
|
+
<li>Output is a stream, not a Buffer. Pipe it to a file writer or another node that consumes streams.</li>
|
|
35
|
+
</ul>
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<script type="text/javascript">
|
|
39
|
+
RED.nodes.registerType('drive-download', {
|
|
40
|
+
category: 'storage',
|
|
41
|
+
color: '#E89B2D',
|
|
42
|
+
defaults: {
|
|
43
|
+
name: { value: '' },
|
|
44
|
+
config: { value: '', type: 'drive-config', required: true },
|
|
45
|
+
fileId: { value: '' }
|
|
46
|
+
},
|
|
47
|
+
inputs: 1,
|
|
48
|
+
outputs: 1,
|
|
49
|
+
icon: 'file-out.svg',
|
|
50
|
+
paletteLabel: 'drive.download',
|
|
51
|
+
label: function() {
|
|
52
|
+
return this.name || 'drive.download';
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
</script>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const createDriveHandler = require("./shared/drive-base");
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
function Node(n) {
|
|
5
|
+
RED.nodes.createNode(this, n);
|
|
6
|
+
|
|
7
|
+
this.config = n.config;
|
|
8
|
+
this.fileId = n.fileId;
|
|
9
|
+
|
|
10
|
+
const handler = createDriveHandler({ operation: "download", RED });
|
|
11
|
+
|
|
12
|
+
this.on("input", function(msg, send, done) {
|
|
13
|
+
handler(msg, this, send, done);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
RED.nodes.registerType("drive-download", Node);
|
|
18
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script type="text/html" data-template-name="drive-upload">
|
|
2
|
+
<div class="form-row">
|
|
3
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
4
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
5
|
+
</div>
|
|
6
|
+
<div class="form-row">
|
|
7
|
+
<label for="node-input-config"><i class="fa fa-cloud"></i> Config</label>
|
|
8
|
+
<input type="text" id="node-input-config">
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<hr>
|
|
12
|
+
|
|
13
|
+
<div class="form-row">
|
|
14
|
+
<label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
|
|
15
|
+
<input type="text" id="node-input-filename" placeholder="file.txt (or msg.filename)" style="width:70%">
|
|
16
|
+
</div>
|
|
17
|
+
<div class="form-row">
|
|
18
|
+
<label for="node-input-folderId"><i class="fa fa-folder"></i> Folder ID</label>
|
|
19
|
+
<input type="text" id="node-input-folderId" placeholder="Google Drive folder ID (or msg.folderId)" style="width:70%">
|
|
20
|
+
</div>
|
|
21
|
+
<div class="form-row">
|
|
22
|
+
<label for="node-input-path"><i class="fa fa-folder-open"></i> Path</label>
|
|
23
|
+
<input type="text" id="node-input-path" placeholder="OneDrive path: Documents/file.txt (or msg.path)" style="width:70%">
|
|
24
|
+
</div>
|
|
25
|
+
<div class="form-row">
|
|
26
|
+
<label for="node-input-mimeType"><i class="fa fa-file-code-o"></i> MIME Type</label>
|
|
27
|
+
<input type="text" id="node-input-mimeType" placeholder="auto-detect (or msg.mimeType)" style="width:70%">
|
|
28
|
+
</div>
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<script type="text/html" data-help-name="drive-upload">
|
|
32
|
+
<p>Upload a file to Google Drive or OneDrive. Provider is determined by the config node.</p>
|
|
33
|
+
|
|
34
|
+
<h3>Input</h3>
|
|
35
|
+
<ul>
|
|
36
|
+
<li><code>msg.payload</code> — Buffer or readable stream (required)</li>
|
|
37
|
+
<li><code>msg.filename</code> — overrides node filename (default: timestamp)</li>
|
|
38
|
+
<li><code>msg.folderId</code> — Google Drive folder ID, overrides node config</li>
|
|
39
|
+
<li><code>msg.path</code> — OneDrive path (e.g. <code>Documents/file.txt</code>), overrides node config</li>
|
|
40
|
+
<li><code>msg.mimeType</code> — overrides node MIME type (default: <code>application/octet-stream</code>)</li>
|
|
41
|
+
</ul>
|
|
42
|
+
|
|
43
|
+
<h3>Output</h3>
|
|
44
|
+
<ul>
|
|
45
|
+
<li><code>msg.payload</code> — file metadata returned by the provider</li>
|
|
46
|
+
<li><code>msg.fileId</code> — ID of the uploaded file</li>
|
|
47
|
+
</ul>
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<script type="text/javascript">
|
|
51
|
+
RED.nodes.registerType('drive-upload', {
|
|
52
|
+
category: 'storage',
|
|
53
|
+
color: '#E89B2D',
|
|
54
|
+
defaults: {
|
|
55
|
+
name: { value: '' },
|
|
56
|
+
config: { value: '', type: 'drive-config', required: true },
|
|
57
|
+
filename: { value: '' },
|
|
58
|
+
folderId: { value: '' },
|
|
59
|
+
path: { value: '' },
|
|
60
|
+
mimeType: { value: '' }
|
|
61
|
+
},
|
|
62
|
+
inputs: 1,
|
|
63
|
+
outputs: 1,
|
|
64
|
+
icon: 'file-in.svg',
|
|
65
|
+
paletteLabel: 'drive.upload',
|
|
66
|
+
label: function() {
|
|
67
|
+
return this.name || 'drive.upload';
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
</script>
|
package/drive-upload.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const createDriveHandler = require("./shared/drive-base");
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
function Node(n) {
|
|
5
|
+
RED.nodes.createNode(this, n);
|
|
6
|
+
|
|
7
|
+
this.config = n.config;
|
|
8
|
+
this.filename = n.filename;
|
|
9
|
+
this.folderId = n.folderId;
|
|
10
|
+
this.path = n.path;
|
|
11
|
+
this.mimeType = n.mimeType;
|
|
12
|
+
|
|
13
|
+
const handler = createDriveHandler({ operation: "upload", RED });
|
|
14
|
+
|
|
15
|
+
this.on("input", function(msg, send, done) {
|
|
16
|
+
handler(msg, this, send, done);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
RED.nodes.registerType("drive-upload", Node);
|
|
21
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-drive-collection",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Node-RED nodes for Google Drive and OneDrive file operations",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"node-red",
|
|
7
|
+
"google-drive",
|
|
8
|
+
"onedrive",
|
|
9
|
+
"storage",
|
|
10
|
+
"oauth2"
|
|
11
|
+
],
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"axios": "^1.6.0",
|
|
15
|
+
"googleapis": "^140.0.0"
|
|
16
|
+
},
|
|
17
|
+
"node-red": {
|
|
18
|
+
"version": ">=2.0.0",
|
|
19
|
+
"nodes": {
|
|
20
|
+
"drive-config": "drive-config.js",
|
|
21
|
+
"drive-upload": "drive-upload.js",
|
|
22
|
+
"drive-download": "drive-download.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module.exports = function createDriveHandler({ operation, RED }) {
|
|
2
|
+
return async function(msg, node, send, done) {
|
|
3
|
+
const configNode = RED.nodes.getNode(node.config);
|
|
4
|
+
|
|
5
|
+
if (!configNode) {
|
|
6
|
+
node.error("Missing drive config", msg);
|
|
7
|
+
done();
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const provider = configNode.providerInstance;
|
|
12
|
+
|
|
13
|
+
if (!provider) {
|
|
14
|
+
node.error("Provider not initialized on config node", msg);
|
|
15
|
+
done();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
node.status({ fill: "yellow", shape: "dot", text: operation });
|
|
21
|
+
|
|
22
|
+
// Normalized input — providers pick what they need
|
|
23
|
+
const input = {
|
|
24
|
+
payload: msg.payload,
|
|
25
|
+
filename: msg.filename || node.filename,
|
|
26
|
+
folderId: msg.folderId || node.folderId,
|
|
27
|
+
path: msg.path || node.path,
|
|
28
|
+
mimeType: msg.mimeType || node.mimeType,
|
|
29
|
+
fileId: msg.fileId || node.fileId
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
if (operation === "upload") {
|
|
33
|
+
const { fileId, metadata } = await provider.upload(input);
|
|
34
|
+
msg.payload = metadata;
|
|
35
|
+
msg.fileId = fileId;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (operation === "download") {
|
|
39
|
+
msg.payload = await provider.download(input);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
node.status({ fill: "green", shape: "dot", text: "ok" });
|
|
43
|
+
send(msg);
|
|
44
|
+
done();
|
|
45
|
+
|
|
46
|
+
} catch (err) {
|
|
47
|
+
node.status({ fill: "red", shape: "ring", text: err.code || "error" });
|
|
48
|
+
node.error(err.message, msg);
|
|
49
|
+
done(err);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const { google } = require("googleapis");
|
|
2
|
+
const { Readable } = require("stream");
|
|
3
|
+
|
|
4
|
+
function toReadable(payload) {
|
|
5
|
+
if (Buffer.isBuffer(payload)) {
|
|
6
|
+
const r = new Readable();
|
|
7
|
+
r.push(payload);
|
|
8
|
+
r.push(null);
|
|
9
|
+
return r;
|
|
10
|
+
}
|
|
11
|
+
return payload;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class GoogleDriveProvider {
|
|
15
|
+
init(configNode) {
|
|
16
|
+
this._oauth2 = new google.auth.OAuth2(
|
|
17
|
+
configNode.credentials.clientId,
|
|
18
|
+
configNode.credentials.clientSecret
|
|
19
|
+
);
|
|
20
|
+
this._oauth2.setCredentials({ refresh_token: configNode.credentials.refreshToken });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async upload({ payload, filename, folderId, mimeType }) {
|
|
24
|
+
const drive = google.drive({ version: "v3", auth: this._oauth2 });
|
|
25
|
+
|
|
26
|
+
const res = await drive.files.create({
|
|
27
|
+
requestBody: {
|
|
28
|
+
name: filename || `upload-${Date.now()}`,
|
|
29
|
+
...(folderId && { parents: [folderId] })
|
|
30
|
+
},
|
|
31
|
+
media: {
|
|
32
|
+
mimeType: mimeType || "application/octet-stream",
|
|
33
|
+
body: toReadable(payload)
|
|
34
|
+
},
|
|
35
|
+
fields: "id, name"
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return { fileId: res.data.id, metadata: res.data };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async download({ fileId }) {
|
|
42
|
+
if (!fileId) {
|
|
43
|
+
const err = new Error("fileId is required for Google Drive download");
|
|
44
|
+
err.code = "INVALID_INPUT";
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const drive = google.drive({ version: "v3", auth: this._oauth2 });
|
|
49
|
+
|
|
50
|
+
const res = await drive.files.get(
|
|
51
|
+
{ fileId, alt: "media" },
|
|
52
|
+
{ responseType: "stream" }
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return res.data;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = GoogleDriveProvider;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
|
|
3
|
+
const GRAPH_BASE = "https://graph.microsoft.com/v1.0";
|
|
4
|
+
|
|
5
|
+
class OneDriveProvider {
|
|
6
|
+
init(configNode) {
|
|
7
|
+
this._tenantId = configNode.tenantId;
|
|
8
|
+
this._clientId = configNode.credentials.clientId;
|
|
9
|
+
this._clientSecret = configNode.credentials.clientSecret;
|
|
10
|
+
this._refreshToken = configNode.credentials.refreshToken;
|
|
11
|
+
|
|
12
|
+
// In-memory only — never persisted
|
|
13
|
+
this._accessToken = null;
|
|
14
|
+
this._expiry = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async _getValidAccessToken() {
|
|
18
|
+
if (this._accessToken && Date.now() < this._expiry - 60_000) {
|
|
19
|
+
return this._accessToken;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const params = new URLSearchParams({
|
|
23
|
+
grant_type: "refresh_token",
|
|
24
|
+
client_id: this._clientId,
|
|
25
|
+
client_secret: this._clientSecret,
|
|
26
|
+
refresh_token: this._refreshToken,
|
|
27
|
+
scope: "https://graph.microsoft.com/.default"
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const res = await axios.post(
|
|
31
|
+
`https://login.microsoftonline.com/${this._tenantId}/oauth2/v2.0/token`,
|
|
32
|
+
params.toString(),
|
|
33
|
+
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
this._accessToken = res.data.access_token;
|
|
37
|
+
this._expiry = Date.now() + res.data.expires_in * 1000;
|
|
38
|
+
|
|
39
|
+
return this._accessToken;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async upload({ payload, path }) {
|
|
43
|
+
if (!path) {
|
|
44
|
+
const err = new Error("path is required for OneDrive upload");
|
|
45
|
+
err.code = "INVALID_INPUT";
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const accessToken = await this._getValidAccessToken();
|
|
50
|
+
const url = `${GRAPH_BASE}/me/drive/root:/${path}:/content`;
|
|
51
|
+
|
|
52
|
+
const res = await axios.put(url, payload, {
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${accessToken}`,
|
|
55
|
+
"Content-Type": "application/octet-stream"
|
|
56
|
+
},
|
|
57
|
+
maxContentLength: Infinity,
|
|
58
|
+
maxBodyLength: Infinity
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return { fileId: res.data.id, metadata: res.data };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async download({ fileId }) {
|
|
65
|
+
if (!fileId) {
|
|
66
|
+
const err = new Error("fileId is required for OneDrive download");
|
|
67
|
+
err.code = "INVALID_INPUT";
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const accessToken = await this._getValidAccessToken();
|
|
72
|
+
const url = `${GRAPH_BASE}/me/drive/items/${fileId}/content`;
|
|
73
|
+
|
|
74
|
+
const res = await axios.get(url, {
|
|
75
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
76
|
+
responseType: "stream"
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return res.data;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = OneDriveProvider;
|