From 2e03b283e13e3924668db2417b27575cbc371b5c Mon Sep 17 00:00:00 2001 From: "Williams, Gavin J" Date: Mon, 24 Jan 2022 16:05:53 -0500 Subject: [PATCH] Created Basic File Upload Class --- src/localhook.ts | 4 ++-- src/taskRunner.ts | 18 ++++++++++++++---- src/uploadfile.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/uploadfile.ts diff --git a/src/localhook.ts b/src/localhook.ts index f2cfad8..1e5fa7c 100644 --- a/src/localhook.ts +++ b/src/localhook.ts @@ -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 diff --git a/src/taskRunner.ts b/src/taskRunner.ts index 1e4b9ec..a8718e5 100644 --- a/src/taskRunner.ts +++ b/src/taskRunner.ts @@ -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'; /** @@ -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); @@ -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; @@ -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]); diff --git a/src/uploadfile.ts b/src/uploadfile.ts new file mode 100644 index 0000000..cd102a8 --- /dev/null +++ b/src/uploadfile.ts @@ -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; + } + ); +} \ No newline at end of file