diff --git a/src/taskRunner.ts b/src/taskRunner.ts index fbc2229..eefa8b9 100644 --- a/src/taskRunner.ts +++ b/src/taskRunner.ts @@ -130,7 +130,8 @@ function uploadTest(fileName: string, folderName: string) { console.log( '\x1b[93mtaskRunner.js::uploader:', '\x1b[0mAttempting upload...' -); uploader.uploadFile(fileName, folderName, credentials); +); + uploader.uploadFile(fileName, folderName, credentials); } getCredentials(); @@ -139,4 +140,4 @@ getCredentials(); // and how often it runs. We may need to test this out. // setInterval(dispatchWatcher, 5000, 7000); -setInterval(uploadTest, 5000, process.argv[2], folderID); +setInterval(uploadTest, 5000, process.argv[2], process.argv[3]); diff --git a/src/uploadfile.ts b/src/uploadfile.ts index ab3d2a2..af85a82 100644 --- a/src/uploadfile.ts +++ b/src/uploadfile.ts @@ -1,9 +1,10 @@ import * as ForgeSDK from 'forge-apis'; -import { authClient, projectID } from './config'; +import * as config from './config'; import * as fs from 'fs'; const projectsAPI = new ForgeSDK.ProjectsApi(); const objectsAPI = new ForgeSDK.ObjectsApi(); +const itemsAPI = new ForgeSDK.ItemsApi(); const jsonVersion: ForgeSDK.JsonApiVersionJsonapi = {version: "1.0"} function getCreateStorageDataObject(fileName: string, folderID: string): ForgeSDK.CreateStorageData { @@ -24,13 +25,109 @@ function getCreateStorageDataObject(fileName: string, folderID: string): ForgeSD return content; } -function uploadFileObject(bucketKey: string, objectName: string, credentials: ForgeSDK.AuthToken) { - // objectsAPI.uploadObject(bucketKey, objectName, ) +function getCreateItemDataObject(fileName: string, folderID: string): ForgeSDK.CreateItemData { + const content: ForgeSDK.CreateItemData = { + type: "items", + attributes: { + name: fileName, + extension: { + type: "items:autodesk.core:File", + version: "1.0", + schema: { + href: "" + } + } + }, + relationships: { + tip: { + data: { + type: "versions", id: "1" + } + }, + parent: { + data: { + type: "folders", + id: folderID + } + } + } + } + return content; } -export function uploadFile(fileName: string, folderID: string, credentials: ForgeSDK.AuthToken) { +function getCreateItemIncludedObject(fileName: string, objectID: string): ForgeSDK.CreateItemIncluded[] { + const content: ForgeSDK.CreateItemIncluded[] = [{ + type: "versions", + id: "1", + attributes: { + name: fileName, + extension: { + type: "items:autodesk.core:File", + version: "1.0", + schema: { + href: "" + } + } + }, + relationships: { + storage: { + data: { + type: "objects", + id: objectID + } + } + } + }] + return content; +} + +function createItem(fileName: string, folderID: string, objectID: string, credentials: ForgeSDK.AuthToken) { + const body: ForgeSDK.CreateItem = { + jsonapi: jsonVersion, + data: getCreateItemDataObject(fileName, folderID), + included: getCreateItemIncludedObject(fileName, objectID) + } + + // itemsAPI.postItem(config.projectID); +} + +function uploadFileObject(fileName: string, folderName: string, objectID: string, credentials: ForgeSDK.AuthToken) { + // TODO: use FS to upload file + const fileBuffer: Buffer = fs.readFileSync(`${__dirname + '/FileShare' + config.folderMap[folderName].local}/${fileName}`); + + var objIDTokens: string[] = objectID.split(":"); + objIDTokens = objIDTokens[objIDTokens.length-1].split("/"); + + const bucketKey: string = objIDTokens[0].trim(); + const objectName:string = objIDTokens[1].trim(); + + objectsAPI.uploadObject( + bucketKey, + objectName, + fileBuffer.byteLength, + fileBuffer, + {contentDisposition: fileName}, + config.authClient, + credentials).then( + (resp: ForgeSDK.ApiResponse) => { + if (resp.statusCode != 200) { + console.log("Response Error Code: ", resp.statusCode); + } + console.log("Success!\nResponse:\n", resp.body); + // createItem(objectID, credentials); + }, + (err: ForgeSDK.ApiError) => { + console.log("Upload File Error: " + err.statusCode); + return; + } + ); +} + +export function uploadFile(fileName: string, folderName: string, credentials: ForgeSDK.AuthToken) { + const folderID: string = config.folderMap[folderName].fusionID; console.log(folderID); + var objectID: string; var bucketKey: string; var objectName: string; @@ -39,21 +136,22 @@ export function uploadFile(fileName: string, folderID: string, credentials: Forg data: getCreateStorageDataObject(fileName, folderID) } - projectsAPI.postStorage(projectID, body, authClient, credentials).then( + projectsAPI.postStorage(config.projectID, body, config.authClient, credentials).then( (resp: ForgeSDK.ApiResponse) => { if (resp.statusCode != 201) { - console.log("Resp Error Code: ", resp.statusCode); + console.log("Response Error Code: ", resp.statusCode); return; } - const storageID: string = resp.body.data.id; - var strarray: string[] = storageID.split(":"); + objectID = resp.body.data.id; + + var strarray: string[] = objectID.split(":"); strarray = strarray[strarray.length-1].split("/"); - bucketKey = strarray[0]; - objectName = strarray[1]; + bucketKey = strarray[0].trim(); + objectName = strarray[1].trim(); - console.log("Created ID:\n", storageID, "\nBucket: ", bucketKey, "\nFile: ", objectName); - uploadFileObject(bucketKey, objectName, credentials); + console.log("Created ID:\n", objectID, "\nBucket: ", bucketKey, "\nFile: ", objectName); + uploadFileObject(fileName, folderName, objectID, credentials); }, (err: ForgeSDK.ApiError) => { console.log("Post Storage Error: " + err.statusCode);