Skip to content

Commit

Permalink
downloading file on 'added' event
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Jan 25, 2022
1 parent 92c2766 commit 5da1780
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
version: "3.8"

services:
auth_server:
server:
build:
context: .
dockerfile: Dockerfile
env_file: ./.env
volumes:
- ./src/build:/ffs/
- /ffs/node_modules
- ./FileShare:/ffs/FileShare/
ports:
- ${PORT}:${PORT}
command: "node server.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "docker-compose up",
"test": "echo \"Error: no test specified\" && exit 1",
"tsc": "tsc",
"docker:build": "tsc --build && docker-compose up --build --remove-orphans"
"build": "tsc --build && docker-compose up --build --remove-orphans"
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export const folderMap: any = {
}
};

// dictionary for looking up local directory path based on folder ID
export const folderIDtoLocal: any = {
"urn:adsk.wipprod:fs.folder:co.IxiedfeBTOGg6NSIGQhXxQ" : "/Gantry",
"urn:adsk.wipprod:fs.folder:co.FDWCeyBGQX2rv8xSNWo5lg": "/Lathe",
"urn:adsk.wipprod:fs.folder:co.nEihcpHUSW-ZsVzU-__1iw": "/Mill",
"urn:adsk.wipprod:fs.folder:co.vJLXAKGbQQayeljr-nwztQ": "/Waterjet"
}

export const port = process.env.PORT;
export const forgeClientId = process.env.FORGE_CLIENT_ID;
export const forgeClientSecret = process.env.FORGE_CLIENT_SECRET;
Expand Down
8 changes: 4 additions & 4 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.download = async (projectID: string, itemID: string, fileName: string, d

ObjectsApi.getObject('wip.dm.prod', storageLocation, {}, config.authClient, credentials)
.then((res: any) => {
console.log(res)
console.log("downloaded file " + fileName + " to " + destination);
fs.writeFile(`${destination}/${fileName}`, res.body)
})
.catch((err: any) => {
Expand All @@ -30,20 +30,20 @@ exports.download = async (projectID: string, itemID: string, fileName: string, d
}

const getStorageLocation = (projectID: string, itemID: string, credentials: any) => {
axios({
return axios({
method: 'GET',
url: `https://developer.api.autodesk.com/data/v1/projects/${projectID}/items/${itemID}`,
headers: {
Authorization: `Bearer ${credentials.access_token}`
},
}).then((res: any) => {
return res.data.included;
return res.data.included[0];
}).then((data: any) => {
var storageID = data.relationships.storage.data.id;
storageID = storageID.substring(storageID.indexOf('/') + 1);
return storageID;
}).catch((err: any) => {
console.log(err)
console.log("unable to get storage location for " + itemID + ": " + err.response.status);
})
}

16 changes: 12 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express = require('express');
const webhooks = require('./webhooks');
import * as config from './config';
import fs = require('fs');

const webhooks = require('./webhooks');
const downloader = require('./downloader');

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 12/2/2021
* Kevin Pan | pan261@purdue.edu | Last Modified: 1/24/2022
*
* Authentication server used to perform 3-legged auth through browser.
*
Expand Down Expand Up @@ -111,8 +112,15 @@ const createServer = () => {
}).on('data', (chunk: any) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
console.log(body)
body = JSON.parse(Buffer.concat(body).toString());

if (credentials && body.hook.event === "dm.version.added") {
const itemID = body.payload.lineageUrn;
const fileName = body.payload.name;
const destination = __dirname + '/FileShare' + config.folderIDtoLocal[body.payload.parentFolderUrn];

downloader.download(config.projectID, itemID, fileName, destination, credentials);
}
})
res.status(204).send();
});
Expand Down
16 changes: 14 additions & 2 deletions src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ const axios = require('axios');
import * as config from './config';


// instead of using this file to constanly poll the api, it will setup and maintain the webhooks
/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 1/24/2022
*
* Functions to manage hook lifecycle
* setup() checks if hooks are valid and active, if not it will reactivated
* and/or recreate the specified hook. There is one hook for each of the
* following events:
*
* - 'dm.version.added' (file added)
* - 'dm.version.modified' (file modified)
* - dm.version.deleted (file deleted)
*
*/

const FileShareID = "urn:adsk.wipprod:fs.folder:co.T0n0mYQeS16K0lq1VuuYVQ"

Expand Down Expand Up @@ -63,7 +75,7 @@ const createHook = (event: string, credentials: any) => {
Authorization: `Bearer ${credentials.access_token}`
},
data: {
autoReactivateHook: true,
autoReactivateHook: "true",
callbackUrl: config.hookCallbackURL,
scope: {
folder: FileShareID
Expand Down

0 comments on commit 5da1780

Please sign in to comment.