@pipedream/google_drive 1.6.3 → 1.7.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.
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import googleDrive from "../../google_drive.app.mjs";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
key: "google_drive-is-folder-ancestor",
|
|
5
|
+
name: "Is Folder Ancestor",
|
|
6
|
+
description: "Check if a specific folder is anywhere in the parent hierarchy of a file or folder. [See the documentation](https://developers.google.com/workspace/drive/api/reference/rest/v3/files/get)",
|
|
7
|
+
version: "0.0.1",
|
|
8
|
+
type: "action",
|
|
9
|
+
annotations: {
|
|
10
|
+
destructiveHint: false,
|
|
11
|
+
openWorldHint: true,
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
},
|
|
14
|
+
props: {
|
|
15
|
+
googleDrive,
|
|
16
|
+
fileId: {
|
|
17
|
+
propDefinition: [
|
|
18
|
+
googleDrive,
|
|
19
|
+
"fileOrFolderId",
|
|
20
|
+
],
|
|
21
|
+
description: "The ID of the file or folder to check",
|
|
22
|
+
},
|
|
23
|
+
ancestorFolderId: {
|
|
24
|
+
propDefinition: [
|
|
25
|
+
googleDrive,
|
|
26
|
+
"folderId",
|
|
27
|
+
],
|
|
28
|
+
label: "Ancestor Folder ID",
|
|
29
|
+
description: "The folder ID to check for in the parent hierarchy",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
async run({ $ }) {
|
|
33
|
+
const {
|
|
34
|
+
fileId, ancestorFolderId,
|
|
35
|
+
} = this;
|
|
36
|
+
|
|
37
|
+
// Get the initial file's parents to start traversal from parent (not the file itself)
|
|
38
|
+
let file;
|
|
39
|
+
try {
|
|
40
|
+
file = await this.googleDrive.getFile(fileId, {
|
|
41
|
+
fields: "id,name,parents",
|
|
42
|
+
});
|
|
43
|
+
} catch (err) {
|
|
44
|
+
throw new Error(`Failed to retrieve file "${fileId}": ${err.message}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const initialParents = file.parents || [];
|
|
48
|
+
if (initialParents.length === 0) {
|
|
49
|
+
$.export("$summary", `Folder "${ancestorFolderId}" is NOT an ancestor of file "${fileId}"`);
|
|
50
|
+
return {
|
|
51
|
+
isAncestor: false,
|
|
52
|
+
fileId,
|
|
53
|
+
ancestorFolderId,
|
|
54
|
+
reason: "not_in_hierarchy",
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// BFS traversal to check all parent paths (files can have multiple parents in Drive)
|
|
59
|
+
const queue = [
|
|
60
|
+
...initialParents,
|
|
61
|
+
];
|
|
62
|
+
const visitedIds = new Set([
|
|
63
|
+
fileId,
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
while (queue.length > 0) {
|
|
67
|
+
const currentId = queue.shift();
|
|
68
|
+
|
|
69
|
+
// Skip if already visited (prevents cycles)
|
|
70
|
+
if (visitedIds.has(currentId)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
visitedIds.add(currentId);
|
|
74
|
+
|
|
75
|
+
// Check if current parent matches the ancestor folder ID
|
|
76
|
+
if (currentId === ancestorFolderId) {
|
|
77
|
+
let ancestorFile;
|
|
78
|
+
try {
|
|
79
|
+
ancestorFile = await this.googleDrive.getFile(currentId, {
|
|
80
|
+
fields: "id,name",
|
|
81
|
+
});
|
|
82
|
+
} catch (err) {
|
|
83
|
+
throw new Error(`Failed to retrieve ancestor folder "${currentId}": ${err.message}`);
|
|
84
|
+
}
|
|
85
|
+
$.export("$summary", `Folder "${ancestorFolderId}" is an ancestor of file "${fileId}"`);
|
|
86
|
+
return {
|
|
87
|
+
isAncestor: true,
|
|
88
|
+
fileId,
|
|
89
|
+
ancestorFolderId,
|
|
90
|
+
ancestorName: ancestorFile.name,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Get file metadata including parents to continue traversal
|
|
95
|
+
let parentFile;
|
|
96
|
+
try {
|
|
97
|
+
parentFile = await this.googleDrive.getFile(currentId, {
|
|
98
|
+
fields: "id,name,parents",
|
|
99
|
+
});
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw new Error(`Failed to retrieve file "${currentId}": ${err.message}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Enqueue all parents for traversal
|
|
105
|
+
const parents = parentFile.parents || [];
|
|
106
|
+
for (const parentId of parents) {
|
|
107
|
+
if (!visitedIds.has(parentId)) {
|
|
108
|
+
queue.push(parentId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
$.export("$summary", `Folder "${ancestorFolderId}" is NOT an ancestor of file "${fileId}"`);
|
|
114
|
+
return {
|
|
115
|
+
isAncestor: false,
|
|
116
|
+
fileId,
|
|
117
|
+
ancestorFolderId,
|
|
118
|
+
reason: "not_in_hierarchy",
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
};
|