Skip to content

Commit

Permalink
feat: added file upload as pda and tests related to it
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Jul 5, 2024
1 parent 674bf9f commit c88de17
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 39 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
"canonicalize": "^2.0.0",
"encoding": "^0.1.13",
"ethers": "^5.7.2",
"form-data": "^4.0.0",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0",
"mime-types": "^2.1.35",
"node-forge": "^1.3.1",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1"
Expand All @@ -56,9 +58,11 @@
"@graphql-codegen/typescript-resolvers": "^4.0.6",
"@graphql-tools/utils": "^10.1.0",
"@types/jest": "^29.5.11",
"@types/mime-types": "^2.1.4",
"@types/node": "^20.10.4",
"@types/node-forge": "^1.3.11",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"axios-mock-adapter": "^1.22.0",
"change-case": "^5.4.3",
"dts-minify": "^0.3.2",
"eslint": "^8.55.0",
Expand All @@ -70,6 +74,7 @@
"eslint-plugin-promise": "^6.1.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.7",
"json5": "^2.2.3",
"lint-staged": "^15.2.0",
"minimatch": "^9.0.3",
Expand Down
61 changes: 61 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { WalletService } from './services/wallet-service';

class SDKFactory {
static createSDK({ apiKey, token, url, logging = false }: Config): Sdk {
parameterChecker(apiKey, token, url);
// parameterChecker(apiKey, token, url);

const client = new GraphQLClient(url, {
headers: { Authorization: `Bearer ${token}`, 'x-api-key': apiKey },
Expand Down
1 change: 0 additions & 1 deletion src/modules/data-model/data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export class DataModel {
try {
return await this.sdk.dataModelsQuery(variables);
} catch (error) {
console.log(error);
throw new Error(errorHandler(error));
}
}
Expand Down
1 change: 0 additions & 1 deletion src/modules/organization/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ export class Organization {
},
});
} catch (error: any) {
console.log(error.request.variables.input);
throw new Error(errorHandler(error));
}
}
Expand Down
37 changes: 25 additions & 12 deletions src/modules/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { errorHandler } from '../../helpers/helper';
import { ValidationService } from '../../services/validator-service';
import { Config } from '../../common/types';
import { WalletService } from '../../services/wallet-service';
import FormData from 'form-data';
import { MAX_UPLOAD_FILE_SIZE } from '../../common/constants';

export class PDA {
private sdk: Sdk;
Expand Down Expand Up @@ -199,19 +201,23 @@ export class PDA {
}

/**
* The function `uploadFileAsPDA` takes a file path as input and a PDA Id
* and updates the PDA and makes it Valid
* @param {File} file - The parameter `file` is of type `File`. Maximum file size allowed is 30 MB
* @param {number} pdaId - The parameter `pdaId` is of type `number`. It should be valid pdaId
* @returns a promise of type fetch response.
* The function `uploadFileAsPDA` uploads a file to a server associated with a specific PDA after
* performing validation checks.
* @param {Buffer} file - The `file` parameter in the `uploadFileAsPDA` function is expected to be a
* `Buffer` type. It represents the file content that you want to upload.
* @param {number} pdaId - The `pdaId` parameter is the identification number of the PDA (Personal
* Digital Assistant) to which the file will be uploaded.
* @param {string} fileName - The `fileName` parameter in the `uploadFileAsPDA` function represents
* the name of the file being uploaded. It is a string value that should include the name of the file
* with its extension. For example, if you are uploading a file named "document.pdf", the `fileName`
* should be same "document.pdf"
* @returns The `uploadFileAsPDA` function returns the result of the axios POST request made to
* upload the file to the server.
*/
async uploadFileAsPDA(
file: Buffer,
pdaId: number,
fileName: string,
fileType: string,
) {
async uploadFileAsPDA(file: Buffer, pdaId: number, fileName: string) {
try {
const { extension, name } =
this.validationService.validateFileName(fileName);
const { PDA: filePda } = await this.getPDA(pdaId);

if (filePda === undefined || filePda === null)
Expand All @@ -222,9 +228,15 @@ export class PDA {
`${pdaId} should be in Pending status only. To upload a file`,
);

if (file.length > MAX_UPLOAD_FILE_SIZE)
throw new Error(`File size max allowed is ${MAX_UPLOAD_FILE_SIZE}(mb)`);

const formData = new FormData();
formData.append('pdaId', BigInt(pdaId).toString());
formData.append('file', new Blob([file], { type: fileType }), fileName);
formData.append('file', file, {
filename: fileName,
contentType: extension,
});

return await axios.post(
`${this.config.url.replace('/graphql', '')}/file/upload`,
Expand All @@ -234,6 +246,7 @@ export class PDA {
'x-api-key': this.config.apiKey,
Authorization: `Bearer ${this.config.token}`,
'Content-Type': 'multipart/form-data',
...formData.getHeaders(),
},
},
);
Expand Down
1 change: 0 additions & 1 deletion src/modules/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class User {
try {
return await this.sdk.meQuery();
} catch (error) {
console.log(error);
throw new Error(errorHandler(error));
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/services/validator-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PublicKey } from '@solana/web3.js';
import { Chain } from '../common/enums';
import { STRING_VALIDATION_LENGTH } from '../common/constants';
import { FilterPDAInput } from '../../gatewaySdk/sources/Gateway';
import mime from 'mime-types';

export class ValidationService {
validateEmail = (email: string): boolean => {
Expand Down Expand Up @@ -104,4 +105,26 @@ export class ValidationService {
throw error;
}
};

validateFileName = (fileName: string) => {
if (!fileName) {
throw new Error('Invalid file path. File name is missing.');
}

const parts = fileName.split('.');

if (parts.length < 2) {
throw new Error('Invalid file name. File name or extension is missing.');
}

const extension = parts.pop() as string;
const name = parts.join('.');
if (!name || !extension) {
throw new Error('Invalid file name. Name or extension is missing.');
}
return {
name,
extension: mime.contentType(extension) || 'application/octet-stream',
};
};
}
Loading

0 comments on commit c88de17

Please sign in to comment.