Skip to content

Commit

Permalink
Data Upload to Bucket Added
Browse files Browse the repository at this point in the history
  • Loading branch information
will1742 committed Jan 25, 2022
1 parent 9ef2fb4 commit d1e78c0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/taskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]);
122 changes: 110 additions & 12 deletions src/uploadfile.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit d1e78c0

Please sign in to comment.