mirror of
https://github.com/balzack/databag.git
synced 2025-04-23 10:05:19 +00:00
updating staging object
This commit is contained in:
parent
c4aca28ed5
commit
c9cd468596
53
app/client/mobile/src/StagingFiles.ts
Normal file
53
app/client/mobile/src/StagingFiles.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
|
@ -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> {}
|
||||
|
@ -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)
|
||||
|
@ -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>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user