Skip to content

Commit

Permalink
Commented Wrapper Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
will1742 committed Jan 27, 2022
1 parent 8a5bb1f commit be90079
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions src/uploadfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function getCreateItemIncludedObject(fileName: string, objectID: string): ForgeS
return content;
}

/**
* Wrapper for: @function ItemsApi.postItem()
* Builds JSON to link bucket data to fusion folders
* On Success: returns
* On Fail: returns
* @param fileName File name plus extension
* @param folderID Fusion ID of folder
* @param objectID fusion ID of uploaded bucket object
* @param credentials Forge Authtoken
* Calls:
* @function getCreateItemDataObject
* @function getCreateItemIncludedObject
*/
function createItem(fileName: string, folderID: string, objectID: string, credentials: ForgeSDK.AuthToken) {
console.log("Name: ", fileName)
const body: ForgeSDK.CreateItem = {
Expand All @@ -95,17 +108,27 @@ function createItem(fileName: string, folderID: string, objectID: string, creden
console.log("Create Version Error: ", resp.statusCode);
return;
}
console.log("Success!\nResponse:\n", resp.body);
return;

return true;
},
(err: ForgeSDK.ApiError) => {
console.log("API ERROR CODE: ", err.statusCode, "\nMESSAGE: ", err.statusMessage, "\nBODY: ", err.statusBody);
return false;
}
);
}

/**
* Wrapper for: @function ObjectsApi.uploadObject()
* Builds json to upload file to bucket
* On success: Maps fusion version to bucket, allows data to be accessed
* On fail: returns
* @param fileName File name plus extension
* @param folderName Name of folder to access ID and local directory
* @param objectID ID of object from successful bucket allocation
* @param credentials Forge Authtoken
*/
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(":");
Expand All @@ -125,17 +148,29 @@ function uploadFileObject(fileName: string, folderName: string, objectID: string
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 200) {
console.log("Upload File Error: ", resp.statusCode);
return;
return false;
}
createItem(fileName, config.folderMap[folderName].fusionID, objectID, credentials);

return createItem(fileName, config.folderMap[folderName].fusionID, objectID, credentials);
},
(err: ForgeSDK.ApiError) => {
console.log("Upload File Error: " + err.statusCode);
return;
return false;
}
);
}

/**
* Wrapper for: @function ProjectsApi.postStorage()
* Extracts necessary information from arguments.
* Requests bucket storage for file name in specified project and folder.
* On success: Uploads file object to bucket
* On fail: Returns
* @param fileName name of the file with extension
* @param folderName name of the folder
* @param credentials forge authtoken
*/

export function uploadFile(fileName: string, folderName: string, credentials: ForgeSDK.AuthToken) {
const folderID: string = config.folderMap[folderName].fusionID;
console.log(folderID);
Expand All @@ -153,7 +188,7 @@ export function uploadFile(fileName: string, folderName: string, credentials: Fo
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 201) {
console.log("Allocate Storage Error: ", resp.statusCode);
return;
return true;
}
objectID = resp.body.data.id;

Expand All @@ -163,12 +198,11 @@ export function uploadFile(fileName: string, folderName: string, credentials: Fo
bucketKey = strarray[0].trim();
objectName = strarray[1].trim();

console.log("Created ID:\n", objectID, "\nBucket: ", bucketKey, "\nFile: ", objectName);
uploadFileObject(fileName, folderName, objectID, credentials);
return uploadFileObject(fileName, folderName, objectID, credentials);
},
(err: ForgeSDK.ApiError) => {
console.log("Post Storage Error: " + err.statusCode);
return;
return false;
}
);
}

0 comments on commit be90079

Please sign in to comment.