Skip to content

Commit ac79b00

Browse files
authored
New Components - egnyte (#15301)
* egnyte init * new components * updates * add bottleneck dependency * pnpm-lock.yaml * pnpm-lock.yaml
1 parent 781d6ec commit ac79b00

File tree

8 files changed

+301
-7
lines changed

8 files changed

+301
-7
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import egnyte from "../../egnyte.app.mjs";
2+
3+
export default {
4+
key: "egnyte-create-folder",
5+
name: "Create Folder",
6+
description: "Creates a new folder in your Egnyte workspace. [See the documentation](https://developers.egnyte.com/docs/File_System_Management_API_Documentation#Create-a-Folder)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
egnyte,
11+
folderPath: {
12+
type: "string",
13+
label: "Folder Path",
14+
description: "The full path to the new folder. Example: `/Shared/test`",
15+
},
16+
},
17+
async run({ $ }) {
18+
const folderPath = this.folderPath[0] === "/"
19+
? this.folderPath.slice(1)
20+
: this.folderPath;
21+
const response = await this.egnyte.createFolder({
22+
$,
23+
folderPath,
24+
});
25+
$.export("$summary", `Created folder "${this.folderPath}"`);
26+
return response;
27+
},
28+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import egnyte from "../../egnyte.app.mjs";
2+
import FormData from "form-data";
3+
import fs from "fs";
4+
import path from "path";
5+
import mime from "mime";
6+
7+
export default {
8+
key: "egnyte-upload-file",
9+
name: "Upload File",
10+
description: "Uploads a file to a specified folder in Egnyte. [See the documentation](https://developers.egnyte.com/docs/File_System_Management_API_Documentation#Upload-a-File)",
11+
version: "0.0.1",
12+
type: "action",
13+
props: {
14+
egnyte,
15+
filePath: {
16+
type: "string",
17+
label: "File Path",
18+
description: "The path to a file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)",
19+
},
20+
folderPath: {
21+
type: "string",
22+
label: "Folder Path",
23+
description: "The full path to the folder where the file should be uploaded. Example: `/Shared/Documents",
24+
},
25+
},
26+
async run({ $ }) {
27+
const form = new FormData();
28+
29+
const filePath = this.filePath.includes("tmp/")
30+
? this.filePath
31+
: `/tmp/${this.filePath}`;
32+
33+
const filename = path.basename(filePath);
34+
const contentType = mime.getType(filePath) || "application/octet-stream";
35+
36+
form.append("file", fs.createReadStream(filePath), {
37+
filename,
38+
contentType,
39+
});
40+
41+
let folderPath = this.folderPath;
42+
if (folderPath.startsWith("/")) {
43+
folderPath = folderPath.slice(1);
44+
}
45+
if (folderPath.endsWith("/")) {
46+
folderPath = folderPath.slice(0, -1);
47+
}
48+
49+
const response = await this.egnyte.uploadFile({
50+
$,
51+
folderPath,
52+
filename,
53+
data: form,
54+
headers: form.getHeaders(),
55+
});
56+
57+
$.export("$summary", `Successfully uploaded file ${filename}`);
58+
return response;
59+
},
60+
};

components/egnyte/egnyte.app.mjs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
1+
import { axios } from "@pipedream/platform";
2+
import Bottleneck from "bottleneck";
3+
const limiter = new Bottleneck({
4+
minTime: 500, // 2 requests per second
5+
maxConcurrent: 1,
6+
});
7+
const axiosRateLimiter = limiter.wrap(axios);
8+
19
export default {
210
type: "app",
311
app: "egnyte",
4-
propDefinitions: {},
512
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
13+
_baseUrl() {
14+
return `https://${this.$auth.subdomain}.egnyte.com/pubapi/v1`;
15+
},
16+
_makeRequest({
17+
$ = this,
18+
path,
19+
headers,
20+
...otherOpts
21+
}) {
22+
const config = {
23+
url: `${this._baseUrl()}${path}`,
24+
headers: {
25+
...headers,
26+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
27+
},
28+
...otherOpts,
29+
};
30+
return axiosRateLimiter($, config);
31+
},
32+
getFolder({
33+
folderPath, ...opts
34+
}) {
35+
return this._makeRequest({
36+
path: `/fs/${folderPath}`,
37+
...opts,
38+
});
39+
},
40+
createFolder({ folderPath }) {
41+
return this._makeRequest({
42+
method: "POST",
43+
path: `/fs/${folderPath}`,
44+
data: {
45+
action: "add_folder",
46+
},
47+
});
48+
},
49+
uploadFile({
50+
folderPath, filename, ...opts
51+
}) {
52+
return this._makeRequest({
53+
method: "POST",
54+
path: `/fs-content/${folderPath}/${filename}`,
55+
...opts,
56+
});
957
},
1058
},
1159
};

components/egnyte/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/egnyte",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Egnyte Components",
55
"main": "egnyte.app.mjs",
66
"keywords": [
@@ -11,5 +11,12 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"bottleneck": "^2.19.5",
18+
"form-data": "^4.0.1",
19+
"mime": "^4.0.6",
20+
"path": "^0.12.7"
1421
}
15-
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import egnyte from "../../egnyte.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
egnyte,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
folderPath: {
15+
type: "string",
16+
label: "Folder Path",
17+
description: "The folder path (example: `/Shared/Documents`) to watch for updates.",
18+
},
19+
},
20+
methods: {
21+
_getLastTs() {
22+
return this.db.get("lastTs") || 0;
23+
},
24+
_setLastTs(lastTs) {
25+
this.db.set("lastTs", lastTs);
26+
},
27+
getResourceType() {
28+
throw new Error("getResourceType is not implemented");
29+
},
30+
generateMeta() {
31+
throw new Error("generateMeta is not implemented");
32+
},
33+
},
34+
async run() {
35+
const lastTs = this._getLastTs();
36+
let maxTs = lastTs;
37+
const resourceType = this.getResourceType();
38+
39+
// Recursively process folder and subfolders
40+
const processFolder = async (folderPath) => {
41+
const results = await this.egnyte.getFolder({
42+
folderPath,
43+
params: {
44+
sort_by: "last_modified",
45+
sort_direction: "descending",
46+
},
47+
});
48+
49+
const items = results[resourceType];
50+
if (!items?.length) {
51+
return;
52+
}
53+
const newItems = [];
54+
55+
for (const item of items) {
56+
const ts = item.uploaded;
57+
if (ts >= lastTs) {
58+
newItems.push(item);
59+
maxTs = Math.max(ts, maxTs);
60+
}
61+
}
62+
63+
const folders = results.folders;
64+
if (folders?.length) {
65+
for (const folder of folders) {
66+
await processFolder(folder.path);
67+
}
68+
}
69+
70+
newItems.reverse().forEach((item) => {
71+
const meta = this.generateMeta(item);
72+
this.$emit(item, meta);
73+
});
74+
};
75+
76+
await processFolder(this.folderPath);
77+
78+
this._setLastTs(maxTs);
79+
},
80+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "egnyte-new-file-in-folder",
6+
name: "New File in Folder",
7+
description: "Emit new event when a file is added within the specified folder in Egnyte. [See the documentation](https://developers.egnyte.com/docs/read/File_System_Management_API_Documentation#List-File-or-Folder)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceType() {
14+
return "files";
15+
},
16+
generateMeta(file) {
17+
return {
18+
id: file.entry_id,
19+
summary: `New file: ${file.name}`,
20+
ts: file.uploaded,
21+
};
22+
},
23+
},
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "egnyte-new-folder-added",
6+
name: "New Folder",
7+
description: "Emit new event when a folder is added within the specified folder in Egnyte. [See the documentation](https://developers.egnyte.com/docs/read/File_System_Management_API_Documentation#List-File-or-Folder).",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getResourceType() {
14+
return "folders";
15+
},
16+
generateMeta(folder) {
17+
return {
18+
id: folder.folder_id,
19+
summary: `New folder: ${folder.name}`,
20+
ts: folder.uploaded,
21+
};
22+
},
23+
},
24+
};

pnpm-lock.yaml

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)