Skip to content

Commit

Permalink
template for watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 16, 2021
1 parent ec77a5a commit 4072568
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 37 deletions.
95 changes: 58 additions & 37 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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(() => {
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
52 changes: 52 additions & 0 deletions watcher.js
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit 4072568

Please sign in to comment.