updating staging object

This commit is contained in:
balzack 2025-01-01 09:30:32 -08:00
parent c4aca28ed5
commit c9cd468596
4 changed files with 59 additions and 6 deletions

View File

@ -0,0 +1,53 @@
import { Staging } from 'databag-client-sdk'
import RNFS from 'react-native-fs';
import fileType from 'react-native-file-type'
export class StagingFiles implements Staging {
public async clear(): Promise<void> {
const files = await RNFS.readDir(RNFS.DocumentDirectoryPath);
for (const entry of files) {
if (entry.name.startsWith('dbTmp_')) {
await RNFS.unlink(entry.path);
}
};
}
public async read(source: any): Promise<{ size: number, getData: (position: number, length: number)=>Promise<string>, close: ()=>Promise<void> }> {
const path = source;
const stat = await RNFS.stat(path);
const size = state.size;
const getData = async (position: number, length: number) => {
return await RNFS.read(path, length, position, 'base64');
}
const close = async ()=>{}
return { size, getData, close };
}
public async write(): Promise<{ setData: (data: string)=>Promise<void>, getUrl: ()=>Promise<string>, close: ()=>Promise<void> }> {
let extension = '';
const path = RNFS.DocumentDirectoryPath + `/dbTmp_${Date.now()}`
const setData = async (data: string) => {
await RNFS.appendFile(path, data, 'base64');
}
const getUrl = async () => {
if (!extension) {
try {
const type = await fileType(path);
await RNFS.moveFile(path, `${path}.${type.ext}`);
extension = `.${type.ext}`;
} catch (err) {
console.log(err);
await RNFS.moveFile(path, `${path}.dat`);
extension = '.dat';
}
}
return `file://${path}${extension}`
}
const close = async () => {
await RNFS.unlink(`${path}${extension}`);
}
return { setData, getUrl, close };
}
}

View File

@ -1,6 +1,6 @@
import { Media } from 'databag-client-sdk'
import { Staging } from 'databag-client-sdk'
export class MediaFiles implements Media {
export class StagingFiles implements Staging {
public clear(): Promise<void> {}

View File

@ -2,9 +2,9 @@ import { useState, useEffect, useRef } from 'react'
import { DatabagSDK, Session, Focus } from 'databag-client-sdk'
import { SessionStore } from '../SessionStore'
import { WebCrypto } from '../WebCrypto'
import { MediaFiles } from '../MediaFiles'
import { StagingFiles } from '../StagingFiles'
const databag = new DatabagSDK({ tagBatch: 32, topicBatch: 32, articleTypes: [], channelTypes: ['sealed', 'superbasic'] }, new WebCrypto(), new MediaFiles())
const databag = new DatabagSDK({ tagBatch: 32, topicBatch: 32, articleTypes: [], channelTypes: ['sealed', 'superbasic'] }, new WebCrypto(), new StagingFiles())
export function useAppContext() {
const sdk = useRef(databag)

View File

@ -11,9 +11,9 @@ The API is provided through a set of interfaces; each interface groups methods b
<ul>
The crypto and log params are provided by implementing the [Crypto](https://github.com/balzack/databag/blob/sdk/app/sdk/src/crypto.ts) and [Logging](https://github.com/balzack/databag/blob/sdk/app/sdk/src/logging.ts) interface respectively.
The [Params](https://github.com/balzack/databag/blob/sdk/app/sdk/src/types.ts) argument specifies the data to syncrhonize. The crypto, staging and log arguments are provided by implementing the [Crypto](https://github.com/balzack/databag/blob/sdk/app/sdk/src/crypto.ts), [Staging](https://github.com/balzack/databag/blob/sdk/app/sdk/src/staging.ts) and [Logging](https://github.com/balzack/databag/blob/sdk/app/sdk/src/logging.ts) interface respectively.
```DatabagClientSDK(crypto?: Crypto, log?: Logging)```
```DatabagClientSDK(params: Params, crypto?: Crypto, staging?: Staging, log?: Logging)```
</ul>
<br>