From 4072568de8521ee76c13bdf668048bdd305169bd Mon Sep 17 00:00:00 2001 From: pan261 Date: Tue, 16 Nov 2021 15:41:43 -0500 Subject: [PATCH] template for watcher --- test.js | 95 +++++++++++++++++++++++++++++++++--------------------- watcher.js | 52 ++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 watcher.js diff --git a/test.js b/test.js index 52b3765..b39fb40 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,8 @@ const authServer = require('./server'); const fs = require("fs").promises; const ForgeSDK = require('forge-apis'); const fileCreds = require("./credentials"); - +const axios = require('axios'); +const watcher = require('./watcher'); /* Look at this for fetching new auth tokens in a loop: https://nodejs.org/en/docs/guides/timers-in-node/ @@ -19,6 +20,10 @@ var BucketsApi = new ForgeSDK.BucketsApi(); //Buckets Client var FoldersApi = new ForgeSDK.FoldersApi(); var ObjectsApi = new ForgeSDK.ObjectsApi(); +// Define constants for file sync folders/project +const projectID = "a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng"; + + async function waitUntil(hasCredentials) { return await new Promise(resolve => { const interval = setInterval(() => { @@ -41,6 +46,20 @@ async function waitUntil(hasCredentials) { } +const donwload = (storageLocation, fileName, destination) => { + ObjectsApi.getObject('wip.dm.prod', loc, {}, authClient, credentials) + .then((res) => { + + // For now writing to project (node project) directory, eventually this + // will be something else + fs.writeFile(`./${destination}/${fileName}`, res.body); + }) + .catch((err) => { + console.log("error while processing " + loc); + console.log(err); + }); +} + (async () => { var content = null; @@ -61,7 +80,7 @@ async function waitUntil(hasCredentials) { // authServer.shutdown(server); } - credentials = fileCreds; + Object.keys(fileCreds).length == 0 ? credentials = hasCredentials : credentials = fileCreds; console.log("Authenticated"); // console.log(credentials); @@ -98,48 +117,50 @@ async function waitUntil(hasCredentials) { var obj; - FoldersApi.getFolderContents(adminProjectID, adminProjectTopFolder, {}, authClient, credentials).then(async function(res) { + watcher.watch(projectID, adminProjectTopFolder, credentials); + + // FoldersApi.getFolderContents(adminProjectID, adminProjectTopFolder, {}, authClient, credentials).then(async function(res) { - // res.body.data includes items, res.body.included has the versions - // we will use versions to download + // // res.body.data includes items, res.body.included has the versions + // // we will use versions to download - // items will be downloaded to /download_tests + // // items will be downloaded to /download_tests - content = res.body.included; - console.log(content); - for (var i = 0; i < content.length; i++) { - // console.log(content[i].type) - if (content[i].type == "versions") { - var loc = content[i].relationships.storage.data.id - loc = loc.substring(loc.indexOf("/") + 1) - console.log(loc) // bucket location + // content = res.body.included; + // console.log(content); + // for (var i = 0; i < content.length; i++) { + // // console.log(content[i].type) + // if (content[i].type == "versions") { + // var loc = content[i].relationships.storage.data.id + // loc = loc.substring(loc.indexOf("/") + 1) + // console.log(loc) // bucket location - console.log(content[i].relationships.storage) - - var fileName = content[i].attributes.displayName; - var extension = content[i].attributes.name.substring(content[i].attributes.name.indexOf(".")); - - if (fileName.indexOf(".") == -1) { - fileName = fileName + extension; - } - - await ObjectsApi.getObject('wip.dm.prod', loc, {}, authClient, credentials) - .then(async (res) => { - // console.log(res) - await fs.writeFile(`./download_tests/${fileName}`, res.body) - }) - .catch((err) => { - console.log("error while processing " + loc); - console.log(err); - }); + // console.log(content[i].relationships.storage) + + // var fileName = content[i].attributes.displayName; + // var extension = content[i].attributes.name.substring(content[i].attributes.name.indexOf(".")); + + // if (fileName.indexOf(".") == -1) { + // fileName = fileName + extension; + // } + + // ObjectsApi.getObject('wip.dm.prod', loc, {}, authClient, credentials) + // .then((res) => { + // // console.log(res) + // fs.writeFile(`./download_tests/${fileName}`, res.body) + // }) + // .catch((err) => { + // console.log("error while processing " + loc); + // console.log(err); + // }); - } - } + // } + // } - }, function(err) { - console.log(err) - }) + // }, function(err) { + // console.log(err) + // }) // var file = await obj; diff --git a/watcher.js b/watcher.js new file mode 100644 index 0000000..d0bbd75 --- /dev/null +++ b/watcher.js @@ -0,0 +1,52 @@ + +const axios = require('axios'); + +/** + * Watches given folder id in the project, dispatches downloader for any files needing to be updated + * @param {String} projectId the project id + * @param {String} folderId the folder id + * @param {Object} credentials credentials for the call + * @param {Integer} interval number of seconds used to filter the date + * if an interval is not passed, watcher will loop through all files in the folder + */ +exports.watch = (projectID, folderID, credentials, interval) => { + + var timeFilter = {}; + + if (interval) { + var dateNow = new Date(); + var dateXSecondsAgo = new Date(dateNow.getTime() - interval * 1000); + timeFilter = {'filter[lastModifiedTime]-ge': dateXSecondsAgo.toISOString()} + } + + axios({ + method: 'GET', + url: `https://developer.api.autodesk.com//data/v1/projects/${projectID}/folders/${folderID}/contents`, + headers: { + 'Authorization': `Bearer ${credentials.access_token}` + }, + params: timeFilter, + }) + .then(res => { + return res.data.included; + }) + .then(data => { + if (data === undefined) { // no new files changed, return + return; + } + + for (var index in data) { + // console.log(data[index]); + var lastModifiedTime = data[index].attributes.lastModifiedTime; + console.log(lastModifiedTime); + + // TODO: if item is not in db, add to db and then download + + // TODO: if item is in db, check and compare lastModifiedTime + + } + }) + .catch(err => { + console.error(err); + }); +} \ No newline at end of file