Skip to content

Commit

Permalink
downloading fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 29, 2021
1 parent 0c30213 commit 79da169
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
61 changes: 46 additions & 15 deletions downloader.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
});
}

6 changes: 4 additions & 2 deletions watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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
Expand All @@ -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);
}

Expand Down

0 comments on commit 79da169

Please sign in to comment.