Skip to content

Commit

Permalink
renaming files
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Feb 8, 2022
1 parent 4b2d285 commit fc3ceba
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 36 deletions.
86 changes: 86 additions & 0 deletions src/forge-delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as ForgeSDK from "forge-apis";
import * as axios from "axios";

const jsonVersion: ForgeSDK.JsonApiVersionJsonapi = {version: "1.0"}
const versionsAPI = new ForgeSDK.VersionsApi();

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 2/7/2021
*
* Deletes a file using the versions API
* @param {string} projectID
* @param {string} itemID
* @param {ForgeSDK.AuthClient} credentials
*/

exports.delete = (projectID: string, itemID: string, fileName: string, credentials: ForgeSDK.AuthToken) => {
const data:ForgeSDK.CreateVersionData = {
type: "versions",
attributes: {
name: fileName,
extension: {
type: "versions:autodesk.core:Deleted",
version: "1.0",
schema: {
href: ""
}
}
},
relationships: {
item: {
data: {
type: "items",
id: itemID
}
}
}
}

const body: ForgeSDK.CreateVersion = {
jsonapi: jsonVersion,
data: data
}

axios.default({
method: 'POST',
url: `https://developer.api.autodesk.com/data/v1/projects/${projectID}/versions`,
headers: {
"Content-Type": "application/vnd.api+json",
Authorization: `Bearer ${credentials.access_token}`,
},
data: {
jsonapi: {
version: "1.0"
},
data: {
type: "versions",
attributes: {
extension: {
type: "versions:autodesk.core:Deleted",
version: "1.0",
}
},
relationships: {
item: {
data: {
type: "items",
id: itemID
}
}
}
}
}
}).then(res => {
console.log("Successfully deleted item with id: " + itemID);
}).catch(err => {
console.log("Error deleting item with id: " + itemID);
});

// versionsAPI.postVersion(projectID, body, config.authClient, credentials)
// .then((res: ForgeSDK.ApiResponse) => {
// console.log("Successfully deleted item with " + itemID);
// }).catch((err: ForgeSDK.ApiError) => {
// console.log("Error deleting file: " + itemID + ", Error code: " + err.statusCode);
// console.log(JSON.stringify(err));
// });
}
9 changes: 4 additions & 5 deletions src/downloader.ts → src/forge-download.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import * as axios from "axios";
import * as ForgeSDK from "forge-apis";

const fs = require("fs").promises;
// const axios = require('axios');
import * as config from "./config";

const ObjectsApi = new ForgeSDK.ObjectsApi();
const ItemsApi = new ForgeSDK.ItemsApi();

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 12/2/2021
* Kevin Pan | pan261@purdue.edu | Last Modified: 2/7/2021
*
* Downloads a file by using the Forge SDK to make a GET request
* on the storageLocation and then saves the file as `filename` under the `destination` directory
* @param {string} storageLocation
* @param {string} fileName
* @param {string} destination
* @param {Object} credentials
* @param {ForgeSDK.AuthClient} credentials
*/
exports.download = async (projectID: string, itemID: string, fileName: string, destination: string, credentials: ForgeSDK.AuthToken) => {
const storageLocation = await getStorageLocation(projectID, itemID, credentials);

console.log("Downloading file " + fileName);

ObjectsApi.getObject("wip.dm.prod", storageLocation, {}, config.authClient, credentials)
return ObjectsApi.getObject("wip.dm.prod", storageLocation, {}, config.authClient, credentials)
.then((res: ForgeSDK.ApiResponse) => {
console.log("Downloaded file " + fileName + " to " + destination);
fs.writeFile(`${destination}/${fileName}`, res.body);
return itemID;
})
.catch((err: ForgeSDK.ApiError) => {
console.log("error while processing " + storageLocation);
Expand Down
26 changes: 13 additions & 13 deletions src/uploadfile.ts → src/forge-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getCreateStorageDataObject(fileName: string, folderID: string): ForgeSD
relationships: {
target: {
data: {
type: "folders",
type: "folders",
id: folderID
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ function getCreateItemIncludedObject(fileName: string, objectID: string): ForgeS
href: ""
}
}
},
},
relationships: {
storage: {
data: {
Expand Down Expand Up @@ -116,7 +116,7 @@ function getCreateVersionData(fileName: string, itemID: string):ForgeSDK.CreateV
* @param folderID Fusion ID of folder
* @param objectID fusion ID of uploaded bucket object
* @param credentials Forge Authtoken
* Calls:
* Calls:
* @function getCreateItemDataObject
* @function getCreateItemIncludedObject
*/
Expand All @@ -136,7 +136,7 @@ function createItem(fileName: string, folderID: string, objectID: string, creden
}

return true;
},
},
(err: ForgeSDK.ApiError) => {
console.log("API ERROR CODE: ", err.statusCode, "\nMESSAGE: ", err.statusMessage, "\nBODY: ", err.statusBody);
return false;
Expand Down Expand Up @@ -164,12 +164,12 @@ function uploadFileObject(fileName: string, folderName: string, objectID: string
const objectName:string = objIDTokens[1].trim();

objectsAPI.uploadObject(
bucketKey,
bucketKey,
objectName,
fileBuffer.byteLength,
fileBuffer,
{contentDisposition: fileName},
config.authClient,
fileBuffer.byteLength,
fileBuffer,
{contentDisposition: fileName},
config.authClient,
credentials).then(
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 200) {
Expand All @@ -188,7 +188,7 @@ function uploadFileObject(fileName: string, folderName: string, objectID: string

/**
* Wrapper for: @function ProjectsApi.postStorage()
* Extracts necessary information from arguments.
* 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
Expand All @@ -209,7 +209,7 @@ export function uploadFile(fileName: string, folderName: string, credentials: Fo
jsonapi: jsonVersion,
data: getCreateStorageDataObject(fileName, folderID)
}

projectsAPI.postStorage(config.projectID, body, config.authClient, credentials).then(
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 201) {
Expand All @@ -236,12 +236,12 @@ export function uploadFile(fileName: string, folderName: string, credentials: Fo


export function newVersion(fileName: string, folderName: string, credentials: ForgeSDK.AuthToken) {
const folderID: string = config.folderMap[folderName].fusionID;
const body: ForgeSDK.CreateVersion = {
jsonapi: jsonVersion,
data: getCreateVersionData(fileName, "")
}
versionsAPI.postVersion(folderID, body, config.authClient, credentials).then(

versionsAPI.postVersion(config.projectID, body, config.authClient, credentials).then(
(resp: ForgeSDK.ApiResponse) => {
if (resp.statusCode != 201) {
console.log("Allocate Storage Error: ", resp.statusCode);
Expand Down
5 changes: 3 additions & 2 deletions src/webhooks.ts → src/forge-webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ForgeSDK from "forge-apis";
import * as config from "./config";

/**
* Kevin Pan | pan261@purdue.edu | Last Modified: 1/24/2022
* Kevin Pan | pan261@purdue.edu | Last Modified: 2/7/2022
*
* Functions to manage hook lifecycle
* setup() checks if hooks are valid and active, if not it will reactivated
Expand All @@ -12,7 +12,7 @@ import * as config from "./config";
*
* - 'dm.version.added' (file added)
* - 'dm.version.modified' (file modified)
* - dm.version.deleted (file deleted)
* - 'dm.version.deleted' (file deleted)
*
*/

Expand Down Expand Up @@ -131,6 +131,7 @@ const deleteHook = (event: string, hookID: string, credentials: ForgeSDK.AuthTok

exports.setupToken = (credentials: ForgeSDK.AuthToken) => {
console.log("Checking webhook token");

axios.default({
method: "POST",
url: "https://developer.api.autodesk.com/webhooks/v1/tokens",
Expand Down
2 changes: 1 addition & 1 deletion src/localhook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chokidar from 'chokidar';
import { AuthToken } from 'forge-apis';
import * as uploader from './uploadfile';
import * as uploader from './forge-upload';

const polltime: number = 100;

Expand Down
22 changes: 7 additions & 15 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import * as db from "./db";
import * as ForgeSDK from "forge-apis";
import { Request, Response, ErrorRequestHandler } from "express";

const webhooks = require("./webhooks");
const downloader = require("./downloader");
const webhooks = require("./forge-webhooks");
const downloader = require("./forge-download");
const deleter = require("./forge-delete");
const localhook = require("./localhook");

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ const createServer = () => {
}
});

app.post("/hook", function (req: any, res: Response) {
app.post("/hook", async function (req: any, res: Response) {
if (!req.signature_match) {
console.log("Request received from outside webhooks service");
return res.status(403).send("Not called from webhooks service");
Expand All @@ -159,18 +160,9 @@ const createServer = () => {
const itemID = body.payload.lineageUrn;
//TODO: check if extension is in the name
const fileName = body.payload.name;
const destination =
__dirname +
"/file_share" +
config.folderIDtoLocal[body.payload.parentFolderUrn];

downloader.download(
config.projectID,
itemID,
fileName,
destination,
credentials
);
const destination = __dirname + "/file_share" + config.folderIDtoLocal[body.payload.parentFolderUrn];
await downloader.download(config.projectID, itemID, fileName, destination, credentials);
await deleter.delete(config.projectID, itemID, fileName, credentials);
}
});

Expand Down

0 comments on commit fc3ceba

Please sign in to comment.