Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/pcloud/actions/copy-file/copy-file.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
name: "Copy File",
description:
"Copy a file to the specified destination. [See the docs here](https://docs.pcloud.com/methods/file/copyfile.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/pcloud/actions/copy-folder/copy-folder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "pcloud-copy-folder",
name: "Copy Folder",
description: "Copy a folder to the specified folder. [See the docs here](https://docs.pcloud.com/methods/folder/copyfolder.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/pcloud/actions/create-folder/create-folder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
name: "Create Folder",
description:
"Create a folder in the specified folder. [See the docs here](https://docs.pcloud.com/methods/folder/createfolder.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "pcloud-download-files",
name: "Download File(s)",
description: "Download one or more files to a folder. [See the docs here](https://docs.pcloud.com/methods/file/downloadfile.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/pcloud/actions/list-contents/list-contents.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "pcloud-list-contents",
name: "List Contents",
description: "Get the contents of the specified folder. [See the docs here](https://docs.pcloud.com/methods/folder/listfolder.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/pcloud/actions/upload-file/upload-file.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
name: "Upload File",
description:
"Upload a file to the specified folder. [See the docs here](https://docs.pcloud.com/methods/file/uploadfile.html)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 2 additions & 0 deletions components/pcloud/pcloud.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export default {
const retryOpts = {
retries: 3,
factor: 2,
minTimeout: 500,
maxTimeout: 1500,
};
return retry(async (bail) => {
try {
Expand Down
91 changes: 47 additions & 44 deletions components/pcloud/sources/watch-folder/watch-folder.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import pcloud from "../../pcloud.app.mjs";
import get from "lodash/get.js";

export default {
key: "pcloud-watch-folder",
name: "Watch Folder",
description:
"Emit new event when a file is created or modified in the specified folder.",
version: "0.0.1",
description: "Emit new event when a file is created or modified in the specified folder.",
version: "0.0.2",
type: "source",
dedupe: "last",
dedupe: "unique",
props: {
pcloud,
db: "$.service.db",
Expand All @@ -25,9 +23,7 @@ export default {
pcloud,
"folderId",
],
description: `Select a **Folder** to watch for changes.
\\
Alternatively, you can provide a custom *Folder ID*.`,
description: "Select a **Folder** to watch for changes. Alternatively, you can provide a custom *Folder ID*.",
},
event: {
type: "string",
Expand All @@ -36,8 +32,7 @@ export default {
"Created",
"Modified",
],
description:
"Specify when to emit an event related to a given folder. Note that pCloud preserves files' `created` and `modified` timestamps on upload. If manually uploading via pCloud's `uploadfile` API, these timestamps can be set by specifying the `mtime` and `ctime` parameters, respectively.",
description: "Specify when to emit an event related to a given folder.",
default: "Created",
},
showDeleted: {
Expand All @@ -50,27 +45,38 @@ export default {
hooks: {
async deploy() {
const files = [];
const pCloudContentsData = await this.getContents();
const hasContents = get(pCloudContentsData, [
"contents",
]);
if (hasContents) {
for (const folderItem of pCloudContentsData.contents) {
if (!folderItem.isfolder) {
files.push(folderItem);
if (files.length == 10) {
break;
}
const { contents } = await this.getContents();
if (!contents) {
return;
}
for (const folderItem of contents) {
if (this.isRelevant(folderItem)) {
files.push(folderItem);
if (files.length == 10) {
break;
}
}
} else {
console.log("No data available, skipping iteration");
}
files.forEach(this.emitpCloudEvent);
this.db.set("lastPolledTime", +Date.now());
this._setPreviousFiles(contents);
},
},
methods: {
_getPreviousFiles() {
return this.db.get("previousFiles");
},
_setPreviousFiles(files) {
const key = this.getFileKey();
const previousFiles = files.filter((file) => file[key]).map((file) => ({
[file[key]]: true,
}));
this.db.set("previousFiles", previousFiles);
},
getFileKey() {
return this.event === "Created"
? "fileid"
: "hash";
},
async getContents() {
return this.pcloud._withRetries(() =>
this.pcloud.listContents(
Expand All @@ -86,35 +92,32 @@ export default {
this.$emit(pCloudEvent, metadata);
},
getEventData(pcloudEvent) {
const eventDate = pcloudEvent[this.event.toLowerCase()];
const ts = +new Date(eventDate);
const key = this.getFileKey();
return {
id: ts,
id: pcloudEvent[key],
summary: `${this.event} file "${pcloudEvent.name}"`,
ts,
ts: Date.now(),
};
},
isRelevant(folderItem, previousFiles = []) {
const key = this.getFileKey();
return !folderItem.isFolder && !previousFiles[folderItem[key]];
},
},
async run() {
const lastPolledTime = this.db.get("lastPolledTime");
const previousFiles = this._getPreviousFiles();
const files = [];
const pCloudContentsData = await this.getContents();
const hasContents = get(pCloudContentsData, [
"contents",
]);
if (hasContents) {
for (const folderItem of pCloudContentsData.contents) {
if (!folderItem.isfolder) {
let fileTime = +new Date(folderItem[this.event.toLowerCase()]);
if (fileTime > lastPolledTime) {
files.push(folderItem);
}
}
}
} else {
const { contents } = await this.getContents();
if (!contents) {
console.log("No data available, skipping iteration");
return;
}
for (const folderItem of contents) {
if (this.isRelevant(folderItem, previousFiles)) {
files.push(folderItem);
}
}
files.forEach(this.emitpCloudEvent);
this.db.set("lastPolledTime", +Date.now());
this._setPreviousFiles(contents);
},
};