Skip to content

Commit

Permalink
Merge pull request #8 from K-nto/feature/mockdata
Browse files Browse the repository at this point in the history
Feature/mockdata
  • Loading branch information
bizk authored Oct 11, 2022
2 parents 3c2b9d7 + c61c3a8 commit ec842fd
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
node_modules/
.env
.env
.vscode/settings.json
14 changes: 7 additions & 7 deletions src/files/files.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fileUpload from 'express-fileupload';
import {StorageOperationController} from '../hyperledger/StorageOperationController';
import ipfsService from '../ipfs/ipfs.service';
import {MFSEntry} from 'ipfs-core-types/src/files';
import {KFSEntry} from './files.interfaces';

class FilesController {
async createFile(userId: string, file: fileUpload.UploadedFile) {
Expand All @@ -11,13 +11,13 @@ class FilesController {
try {
return await ipfsService
.createFile(userId, file)
.then((files: MFSEntry[]) => {
.then((files: KFSEntry[]) => {
console.log('[DEBUG] files.controller - createFile: files', files);

files.forEach((file: MFSEntry) => {
files.forEach((file: KFSEntry) => {
new StorageOperationController().createFileOperation(
userId,
file.cid.toString(),
file.id,
'WRITE'
);
});
Expand All @@ -36,13 +36,13 @@ class FilesController {
`[INFO] files.controller - listFiles: Listing files for userId ${userId}`
);
try {
return await ipfsService.listFiles().then((files: MFSEntry[]) => {
return await ipfsService.listFiles().then((files: KFSEntry[]) => {
console.log('[DEBUG] files.controller - listFiles: files', files);

files.forEach((file: MFSEntry) => {
files.forEach((file: KFSEntry) => {
new StorageOperationController().createFileOperation(
userId,
file.cid.toString(),
file.id,
'READ'
);
});
Expand Down
7 changes: 7 additions & 0 deletions src/files/files.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {MFSEntry} from 'ipfs-core-types/src/files';

export interface KFSEntry extends MFSEntry {
// Kinto File System Entry
id: string;
txHistory?: Array<any>;
}
14 changes: 9 additions & 5 deletions src/files/files.routes.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class FilesRoutes extends CommonRoutesConfig {
}
configureRoutes() {
this.app
// Logic for folders
.route(`/${USERS}/:userId/${FILES}`)
.all((req: Request, res: Response, next: NextFunction) => {
// Middleware executed on every route. @TODO: Validation @TODO: User authentication @TODO: Register on Hyperledger
Expand Down Expand Up @@ -55,13 +56,14 @@ export class FilesRoutes extends CommonRoutesConfig {
next();
})
.get((req: Request, res: Response) => {
// @TODO: Logic for folders
const fileCID = req.params.fileId;
if (!fileCID) res.status(400).send('File CID must be set!');
if (!fileCID) return res.status(400).send('File CID must be set!');

const userId = req.body.userId;
if (!userId) res.status(400).send('Missing userId');
const userId = req.params.userId;
if (!userId) return res.status(400).send('Missing userId');

filesController
return filesController
.getFile(userId, fileCID)
.then(file => res.status(200).send(file))
.catch(error =>
Expand All @@ -70,7 +72,9 @@ export class FilesRoutes extends CommonRoutesConfig {
})
// @TODO: Define if possible
.patch((req: Request, res: Response) => {
res.status(200).send(`TODO: PATCH file for id ${req.params.fileId}`);
return res
.status(200)
.send(`TODO: PATCH file for id ${req.params.fileId}`);
})
// @TODO: this uses filename and it's not really usable in a multiple-node context.
// @TODO: will make more sense for it to use :fileId BUT you can't delete files with the CID... not really sure why
Expand Down
74 changes: 74 additions & 0 deletions src/files/mock/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {CID} from 'multiformats/cid';
import {KFSEntry} from '../files.interfaces';

export const mockListOfFiles: Array<KFSEntry> = [
{
name: 'Picture.jpg',
type: 'file',
size: 39393993,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqasf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqasf3oclgtqy55fbzdi',
},
{
name: 'movie.mov',
type: 'file',
size: 123123,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26df3efuylqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26df3efuylqabf3oclgtqy55fbzdi',
},
{
name: 'file.doc',
type: 'file',
size: 448389289,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3eduylqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3eduylqabf3oclgtqy55fbzdi',
},
{
name: 'excel.xslx',
type: 'file',
size: 123123123,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nfrefuylqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nfrefuylqabf3oclgtqy55fbzdi',
},
{
name: 'printer.pdf',
type: 'file',
size: 123123123154,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nv3efuylqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nv3efuylqabf3oclgtqy55fbzdi',
},
{
name: 'MyFolder',
type: 'directory',
size: 8,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3qfuylqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3qfuylqabf3oclgtqy55fbzdi',
},
];

export const mockSingleFile = mockListOfFiles[0];

export const mocksNewFileList = [
...mockListOfFiles,
{
name: 'Extrafile.png',
type: 'file',
size: 12312312345,
cid: CID.parse(
'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efualqabf3oclgtqy55fbzdi'
),
id: 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efualqabf3oclgtqy55fbzdi',
},
];
1 change: 0 additions & 1 deletion src/hyperledger/StorageOperationController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {HyperledgerController} from './HyperledgerController';
import {StorageOperation} from './contracts/StorageOperation.interface';
import { StorageController } from '../StorageController';

export class StorageOperationController {
//TODO we can use this to show / check history
Expand Down
15 changes: 11 additions & 4 deletions src/ipfs/ipfs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {UploadedFile} from 'express-fileupload';
import {MFSEntry} from 'ipfs-core-types/src/files';
import {create, IPFSHTTPClient} from 'ipfs-http-client';
import all from 'it-all';
import {KFSEntry} from '../files/files.interfaces';
/**
* @TODO: Better error handling
*/
Expand Down Expand Up @@ -33,7 +34,7 @@ class IPFSService {
public async createFile(
userId: string,
file: UploadedFile
): Promise<MFSEntry[]> {
): Promise<KFSEntry[]> {
const filePath = `/${file.name}`;
console.log('[DEBUG] IPFSService - createFile', userId, file);

Expand All @@ -49,9 +50,11 @@ class IPFSService {
* @returns Array<MFSEntry> w/ the results from the node
* @TODO: will have to change once userId is implemented
*/
public async listFiles(dir = '/'): Promise<MFSEntry[]> {
public async listFiles(dir = '/'): Promise<KFSEntry[]> {
console.log('[DEBUG] IPFSService - listFiles');
return await all(IPFSService.ipfsHttpClient.files.ls(dir));
return await (
await all(IPFSService.ipfsHttpClient.files.ls(dir))
).map(file => this.ipfsToKinto(file));
}

/**
Expand Down Expand Up @@ -80,7 +83,7 @@ class IPFSService {
* @returns
*/
public async updateFile(file: File) {
const result: Array<MFSEntry> = [];
const result: Array<KFSEntry> = [];
// IPFSService.ipfsHttpClient.files.write('/' + file.name, file);
// result.push(...(await all(IPFSService.ipfsHttpClient.files.ls(file.name))));
return result;
Expand All @@ -100,6 +103,10 @@ class IPFSService {
throw error;
});
}

private ipfsToKinto(file: MFSEntry) {
return {...file, id: file.cid.toString()};
}
}

export default new IPFSService();
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
"types": [], /* Specify type package names to be included without being referenced in a source file. */
"types": ["jest"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
Expand Down

0 comments on commit ec842fd

Please sign in to comment.