Skip to content

Commit

Permalink
Created Basic File Upload Class
Browse files Browse the repository at this point in the history
  • Loading branch information
will1742 committed Jan 24, 2022
1 parent 1e1e612 commit 2e03b28
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/localhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ let [file, folder]: string = "";
* depth: 0 -> only track immediate directories passed in
* cwd: '.' -> use relative paths
*/
const watcher = chokidar.watch(['./mills/', './lathes/'], {
const watcher = chokidar.watch(['./Mill/', './Lathe/', './Gantry', './Waterjet'], {
persistent: true,
usePolling: true,
ignoreInitial: true,
interval: polltime,
useFsEvents: false,
depth: 0,
cwd: '.'
cwd: './FileShare'
})

/* Gavin Williams | will1742 | 01/19/22
Expand Down
18 changes: 14 additions & 4 deletions src/taskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const axios = require('axios');
const watcher = require('./watcher');
const downloader = require('./downloader');
const fs = require('fs');
import { AuthToken } from 'forge-apis';
import * as config from './config';
import * as uploader from './uploadfile';


/**
Expand All @@ -21,8 +23,8 @@ const projectID = ""

// parse command line arguments from docker-compose to get folder ID

const folderID = config.folderMap[process.argv[2]].fusionID;
const localPath = config.folderMap[process.argv[2]].local;
const folderID = config.folderMap[process.argv[3]].fusionID;
const localPath = config.folderMap[process.argv[3]].local;
const localDir = __dirname + '/FileShare' + localPath;

console.log(folderID, localDir);
Expand All @@ -43,7 +45,7 @@ if (!fs.existsSync(localDir)) {
// const adminProjectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng';
// const adminProjectTopFolder = 'urn:adsk.wipprod:fs.folder:co.ltuYad4ZTtieJLm6j3MGuA';

let credentials: any = null;
let credentials: AuthToken;
let initInterval: any = null;
let updateInterval: any = null;

Expand Down Expand Up @@ -124,9 +126,17 @@ const dispatchWatcher = async (interval: any) => {
}
};

function uploadTest(fileName: string, folderName: string) {
console.log(
'\x1b[93mtaskRunner.js::uploader:',
'\x1b[0mAttempting upload...'
); uploader.uploadFile(fileName, folderName, credentials);
}

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, 7000);
// setInterval(dispatchWatcher, 5000, 7000);

setInterval(uploadTest, 5000, process.argv[2], process.argv[3]);
45 changes: 45 additions & 0 deletions src/uploadfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as ForgeSDK from 'forge-apis';
import { authClient, folderMap, ForgeAuthClient, projectID } from './config';

const projectsAPI = new ForgeSDK.ProjectsApi();
const bucketsAPI = new ForgeSDK.BucketsApi();
const jsonVersion: ForgeSDK.JsonApiVersionJsonapi = {version: "1.0"}

function getCreateStorageDataObject(fileName: string, foldername: string): ForgeSDK.CreateStorageData {
const content: ForgeSDK.CreateStorageData = {
type: "objects",
attributes: {
name: fileName
},
relationships: {
target: {
data: {
type: "folders", id: folderMap[foldername].folderID
}
}
}
}
return content;
}

export function uploadFile(fileName: string, folderName: string, credentials: ForgeSDK.AuthToken) {
const body: ForgeSDK.CreateStorage = {
jsonapi: jsonVersion,
data: getCreateStorageDataObject(fileName, folderName)
}

projectsAPI.postStorage(projectID, body, authClient, credentials).then(
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 201) {
console.log("Resp Error Code: ", resp.statusCode);
return;
}
const storageID: string = resp.body.id;
console.log("Created ID:\n", storageID);
},
(err: ForgeSDK.ApiError) => {
console.log("Post Storage Error: " + err.statusCode);
return;
}
);
}

0 comments on commit 2e03b28

Please sign in to comment.