diff --git a/downloader.js b/downloader.js index 2727216..779601e 100644 --- a/downloader.js +++ b/downloader.js @@ -1,5 +1,21 @@ const axios = require('axios'); const fs = require('fs').promises; +const ForgeSDK = require('forge-apis'); +require('dotenv').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 @@ -11,22 +27,37 @@ const fs = require('fs').promises; * @param {String} destination * @param {Object} credentials */ -exports.download = async (storageUrl, fileName, destination, credentials) => { - await axios({ - method: 'GET', - url: storageUrl, - headers: { - Authorization: `Bearer ${credentials.access_token}` - } - }) - .then(async res => { - fs.writeFile(`./${destination}/${fileName}`, res.data).then(() => { - console.log("finished downloading", fileName); - }); +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); + // }); + + ObjectsApi.getObject('wip.dm.prod', storageUrl, {}, authClient, credentials) + .then((res) => { + console.log(res) + fs.writeFile(`./${destination}/${fileName}`, res.body) }) - .catch(err => { - console.log("error while processing " + fileName); - // console.log(err); + .catch((err) => { + console.log("error while processing " + storageUrl); + console.log(err); }); } diff --git a/watcher.js b/watcher.js index 1cdae5b..9848227 100644 --- a/watcher.js +++ b/watcher.js @@ -42,7 +42,7 @@ exports.watch = (projectID, folderID, credentials, interval) => { console.log( '\x1b[96mwatcher.js::watch:', '\x1b[0mNo new files modified since ' + dateXSecondsAgo.toISOString()); - return; + return {}; } let changedItems = {}; @@ -59,6 +59,8 @@ exports.watch = (projectID, folderID, credentials, interval) => { const lastModifiedTime = data[index].attributes.lastModifiedTime; const storageUrl = data[index].relationships.storage.meta.link.href; + var storageID = data[index].relationships.storage.data.id; + storageID = storageID.substring(storageID.indexOf('/') + 1); console.log('\x1b[92mwatcher.js::watch: \x1b[0m' + displayName + ' was modified at ' + lastModifiedTime); // TODO: if item is not in db, add to db and then download @@ -67,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: storageUrl}; + changedItems[index] = {name: saveName, url: storageID}; // console.log(changedItems); }