1.0.4 • Published 10 months ago

filehub v1.0.4

Weekly downloads
-
License
Apache-2.0
Repository
gitlab
Last release
10 months ago

Filehub Client

TypeScript client implementation for persisting and reading files on the Chromia blockchain.

Getting started

Filehub utilises @chromia/ft4 and postchain-client as peer dependency.

1. Initialize Filehub

import { Filehub } from 'filehub';
import { createClient } from 'postchain-client';

// Instantiates a new Filehub client by its settings
const filehub = new Filehub({
  directoryNodeUrlPool: DIRECTORY_NODE_URL,
  blockchainRid: FILEHUB_BRID
});

// Debug logging can be enabled, disabled by default
new Filehub({
  directoryNodeUrlPool: DIRECTORY_NODE_URL,
  blockchainRid: FILEHUB_BRID,
  debug: true
});

2. Register user

import { newSignatureProvider } from "postchain-client";
import { createInMemoryFtKeyStore, createKeyStoreInteractor, createSingleSigAuthDescriptorRegistration, FlagsType, registerAccount } from "@chromia/ft4";

// Creates a FT4 connection to Filehub
const connection = await filehub.initializeConnection();

const store = createInMemoryFtKeyStore(userSignature);
const userSignature = newSignatureProvider({
  privKey: config.userPrivateKey // Filehub user
});
const storeInteractor = createKeyStoreInteractor(connection.client, store);

const singleSignAuthDescriptor = createSingleSigAuthDescriptorRegistration(
  [FlagsType.Account, FlagsType.Transfer],
  userSignature.pubKey
);
const adminSignature = newSignatureProvider({
  privKey: config.adminPrivateKey // Filehub admin
});

// Registers the user
await registerAccount(connection.client, adminSignature, singleSignAuthDescriptor);

const accountsData = await storeInteractor.getAccounts();

// Session will be used for communcation with Filehub
const userSession = await storeInteractor.getSession(accountsData[0]?.id);

3. Register event listeners

filehub.on('onChunkStored', (fileHash: Buffer, totalChunksForFile: number, location: IChunkLocation, error?: Error) => {
  // Implement logic to track progress of file upload
});

filehub.on('onFileStored', (fileHash: Buffer, metadata: FsFileMetadata, error?: Error) => {
  // Implement logic to track when all chunks of a file has been uploaded
});

4. Store file

import * as path from 'path';
import fs from 'fs';

// Read local file
const filepath = path.resolve('./example/demo.txt');
const fileDataAsBuffer = await fs.promises.readFile(filepath);

// Each file must set content-type as metadata
const file = FsFile.fromData(fileDataAsBuffer, {
  ContentType: 'text/plain' // Example
});

// Storing file using FT4 user session & signature
await filehub.storeFile(userSession, file);
Supported content types
  • image/jpeg
  • image/png
  • image/gif
  • image/webp
  • application/zip
  • application/pdf
  • application/octet-stream
  • application/json
  • application/xml
  • application/x-www-form-urlencoded
  • video/mp4
  • audio/ogg
  • audio/mpeg
  • multipart/form-data
  • text/html
  • text/css
  • text/plain
  • text/javascript

5. Fetch file

import { FsFile } from 'filehub';

// Filehub can validate that file has been stored using it's hash
const hasFile: boolean = await filehub.hasFile(file.hash);

// To retrieve all file hashes a user has uploaded
const hasFile: Buffer[] = await filehub.getAllFileHashes(userSession.account.id);

// Fetching file from Filehub
const storedFile: FsFile = await filehub.getFile(file.hash);

// Fetching file metadata from Filehub
const metadata: Buffer = await filehub.getFileMetadata(file.hash);

Administration

When deploying Filehub each underlying Filechain must be registered before Filehub can be user. This management can be handled via the Filehub ts-client as well.

// Instantiates a new Filehub client by its url and brid
const filehub = new Filehub(filehubUrl, filehubBrid);

// Instantiates a new FilehubAdministrator client by passing the Filehub and admin key pair
const filehubAdministrator = new FilehubAdministrator(filehub, adminSignature);

// Register Filechain to Filehub
await filehubAdministrator.registerFilechain(filechainBrid, filechainUrl, filechainOwnerEvmAddress);

// Disable Filechain on Filehub
await filehubAdministrator.disableFilechain(filechainBrid, filechainUrl);

// Enable Filechain on Filehub
await filehubAdministrator.enableFilechain(filechainBrid, filechainUrl);

// Update URL to filechain (operation for admin or filechain owner)
await filehubAdministrator.updateFilechainUrl(ft4Session, filechainBrid, filechainUrl);
1.0.4

10 months ago

1.0.2

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.1.41

1 year ago

0.1.42

1 year ago

0.1.43

1 year ago

0.1.44

1 year ago

0.1.45

1 year ago

0.1.46

1 year ago

0.1.47

1 year ago

0.1.48

1 year ago

0.1.40

1 year ago

0.1.38

1 year ago

0.1.39

1 year ago

0.1.30

1 year ago

0.1.31

1 year ago

0.1.32

1 year ago

0.1.33

1 year ago

0.1.34

1 year ago

0.1.35

1 year ago

0.1.36

1 year ago

0.1.37

1 year ago

0.1.27

1 year ago

0.1.28

1 year ago

0.1.29

1 year ago

0.1.23

1 year ago

0.1.24

1 year ago

0.1.25

1 year ago

0.1.26

1 year ago

0.1.22

1 year ago

0.1.21

1 year ago

0.1.20

2 years ago

0.1.19

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.10

2 years ago

0.1.8

2 years ago

0.1.9

2 years ago

0.1.7

2 years ago

0.1.4

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago