diff --git a/config.js b/config.js new file mode 100644 index 0000000..aad2b35 --- /dev/null +++ b/config.js @@ -0,0 +1,33 @@ +/** + * Configures constants used in the other parts of this app + */ + +require('dotenv').config(); +const ForgeAuthClient = require('forge-apis').AuthClientThreeLegged; + +const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write']; + +const authClient = new ForgeAuthClient( + process.env.FORGE_CLIENT_ID, + process.env.FORGE_CLIENT_SECRET, + process.env.FORGE_CALLBACK_URL, + scopes, + true +); + +const folderIdMap = { + gantry: "GANTRY_FOLDER_ID", + lathe: "LATHE_FOLDER_ID", + mill: "MILL_FOLDER_ID", + waterjet: "WATERJET_FOLDER_ID" +}; + +module.exports = { + authClient: authClient, + scopes: scopes, + folderIdMap: folderIdMap, + port: process.env.PORT, + forgeClientId: process.env.FORGE_CLIENT_ID, + forgeClientSecret: process.env.FORGE_CLIENT_SECRET, + forgeCallbackURL: process.env.FORGE_CALLBACK_URL +} \ No newline at end of file diff --git a/downloader.js b/downloader.js index 779601e..35b3e2b 100644 --- a/downloader.js +++ b/downloader.js @@ -1,62 +1,28 @@ -const axios = require('axios'); const fs = require('fs').promises; const ForgeSDK = require('forge-apis'); -require('dotenv').config(); - +const config = require('./config'); const ObjectsApi = new ForgeSDK.ObjectsApi(); -const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write']; - -const authClient = new ForgeSDK.AuthClientThreeLegged( - process.env.FORGE_CLIENT_ID, - process.env.FORGE_CLIENT_SECRET, - process.env.FORGE_CALLBACK_URL, - scopes, - true -); - - /** - * Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021 + * Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021 * - * Downloads a file by making a GET request on the storageUrl and then - * saves the file as `filename` under the `destination` directory - * @param {String} storageUrl + * Downloads a file by using the Forge SDK to make a GET request + * on the storageLocation and then saves the file as `filename` under the `destination` directory + * @param {String} storageLocation * @param {String} fileName * @param {String} destination * @param {Object} credentials */ -exports.download = (storageUrl, fileName, destination, credentials) => { - // var file = fs.createWriteStream(`./${destination}/${fileName}`); - - // await axios({ - // method: 'GET', - // url: storageUrl, - // headers: { - // Authorization: `Bearer ${credentials.access_token}` - // } - // }) - // .then(res => {return res.data}) - // .then(async res => { - // // console.log(res.data); - // // fs.writeFile(`./${destination}/${fileName}`, res.data, 'binary').then(() => { - // // console.log("finished downloading", fileName); - // // }); - // fs.WriteStream(`./${destination}/${fileName}`).write(res); - // }) - // .catch(err => { - // console.log("error while processing " + fileName); - // console.log(err); - // }); +exports.download = (storageLocation, fileName, destination, credentials) => { - ObjectsApi.getObject('wip.dm.prod', storageUrl, {}, authClient, credentials) + ObjectsApi.getObject('wip.dm.prod', storageLocation, {}, config.authClient, credentials) .then((res) => { console.log(res) fs.writeFile(`./${destination}/${fileName}`, res.body) }) .catch((err) => { - console.log("error while processing " + storageUrl); + console.log("error while processing " + storageLocation); console.log(err); }); } diff --git a/server.js b/server.js index bf079d6..48d5d4c 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,5 @@ const express = require('express'); -require('dotenv').config(); - -const ForgeSDK = require('forge-apis'); +const config = require('./config'); /** * Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021 @@ -14,18 +12,10 @@ const ForgeSDK = require('forge-apis'); * - FORGE_CALLBACK_URL * - PORT (defaults to 3000) * + * These are all configured in config.js + * */ -const scopes = ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write']; - -const oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged( - process.env.FORGE_CLIENT_ID, - process.env.FORGE_CLIENT_SECRET, - process.env.FORGE_CALLBACK_URL, - scopes, - true -); - let credentials = null; let refreshTime = null; let intervalID = null; @@ -42,9 +32,9 @@ let intervalID = null; */ const createServer = () => { const app = express(); - const PORT = process.env.PORT || 3000; + const PORT = config.port || 3000; - if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) { + if (config.forgeClientId == null || config.forgeClientSecret == null) { console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.'); return; } @@ -55,12 +45,12 @@ const createServer = () => { '\x1b[96mserver.js::createServer:', '\x1b[0m/auth endpoint called' ); - res.redirect(oAuth2ThreeLegged.generateAuthUrl()); + res.redirect(config.authClient.generateAuthUrl()); }); // Endpoint Forge redirects to after consent screen app.get('/callback', function (req, res) { - oAuth2ThreeLegged.getToken(req.query.code) + config.authClient.getToken(req.query.code) .then((creds) => { credentials = creds; refreshTime = creds.expires_in - 300; @@ -113,7 +103,7 @@ const createServer = () => { res.status(err.statusCode).json(err); }); - const server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); }); + const server = app.listen(config.port, () => { console.log(`Server listening on port ${PORT}`); }); return server; } @@ -122,7 +112,7 @@ const createServer = () => { * Used internally to refresh tokens automatically */ const refresh = () => { - oAuth2ThreeLegged.refreshToken(credentials, scopes) + config.authClient.refreshToken(credentials, scopes) .then(creds => { credentials = creds; console.log( diff --git a/taskRunner.js b/taskRunner.js index ac29f2b..acb9ecb 100644 --- a/taskRunner.js +++ b/taskRunner.js @@ -1,10 +1,10 @@ const axios = require('axios'); const watcher = require('./watcher'); const downloader = require('./downloader'); -require('dotenv').config(); +const config = require('./config'); /** - * Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021 + * Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021 * * This file will be responsible for managing the watcher * as well as fetching the credentials from the server @@ -18,14 +18,8 @@ const projectID = "" // TODO: create map from cloud directories to local // parse command line arguments from docker-compose to get folder ID -const folderIdMap = { - gantry: "GANTRY_FOLDER_ID", - lathe: "LATHE_FOLDER_ID", - mill: "MILL_FOLDER_ID", - waterjet: "WATERJET_FOLDER_ID" -}; -const folderID = folderIdMap[process.argv[2]]; +const folderID = config.folderIdMap[process.argv[2]]; console.log(folderID); const adminProjectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng'; @@ -108,8 +102,7 @@ const dispatchWatcher = async (interval) => { for (index in items) { // dispatch the downloader here - console.log(items[index].name); - downloader.download(items[index].url, items[index].name, 'download_tests', credentials); + downloader.download(items[index].location, items[index].name, 'download_tests', credentials); } }; @@ -117,5 +110,5 @@ getCredentials(); // There should be a buffer between how far back the watcher checks // and how often it runs. We may need to test this out. -setInterval(dispatchWatcher, 5000); +setInterval(dispatchWatcher, 5000, 7000); diff --git a/watcher.js b/watcher.js index 9848227..a321e97 100644 --- a/watcher.js +++ b/watcher.js @@ -1,7 +1,7 @@ const axios = require('axios'); /** - * Kevin Pan | pan261@purdue.edu | Last Modified: 11/22/2021 + * Kevin Pan | pan261@purdue.edu | Last Modified: 11/30/2021 * * Watches given folder id in the project, logs any files changed since * `interval` ms ago @@ -69,7 +69,7 @@ exports.watch = (projectID, folderID, credentials, interval) => { // where time does not match with db time // index will have to change because not all items may need to be added to this list - changedItems[index] = {name: saveName, url: storageID}; + changedItems[index] = {name: saveName, location: storageID}; // console.log(changedItems); } @@ -80,6 +80,7 @@ exports.watch = (projectID, folderID, credentials, interval) => { '\x1b[91mwatcher.js::watch:', '\x1b[0mError accessing Forge folder contents API' ); + return {}; }); return items;