Skip to content

Commit

Permalink
refactor: upgraded sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorShadurin committed Mar 9, 2023
1 parent 071949c commit 7a20b02
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 208 deletions.
48 changes: 42 additions & 6 deletions src/directory/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { createDirectory, readDirectory, DEFAULT_UPLOAD_DIRECTORY_OPTIONS, Uploa
import { assertAccount } from '../account/utils'
import { removeEntryFromDirectory } from '../content-items/handler'
import { extractPathInfo, readBrowserFileAsBytes } from '../file/utils'
import { assertPodName, getExtendedPodsListByAccountData } from '../pod/utils'
import {
assertPodName,
getReadablePodInfo,
getPodShareInfo,
getWritablePodInfo,
hexToPodPasswordBytes,
} from '../pod/utils'
import { isNode } from '../shim/utils'
import {
assertBrowserFilesWithPath,
Expand All @@ -20,6 +26,8 @@ import {
import { uploadData } from '../file/handler'
import { assertNodeFileInfo, isBrowserFileInfo } from './types'
import { DirectoryItem } from '../content-items/types'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
import { prepareEthAddress } from '../utils/wallet'

/**
* Directory related class
Expand All @@ -37,13 +45,14 @@ export class Directory {
async read(podName: string, path: string, isRecursive?: boolean): Promise<DirectoryItem> {
assertAccount(this.accountData)
assertPodName(podName)
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)

const { podAddress, podPassword } = await getReadablePodInfo(this.accountData, podName)

return readDirectory(
this.accountData.connection.bee,
path,
podAddress,
pod.password,
podPassword,
isRecursive,
this.accountData.connection.options?.requestOptions,
)
Expand All @@ -58,7 +67,7 @@ export class Directory {
async create(podName: string, fullPath: string): Promise<void> {
assertAccount(this.accountData)
assertPodName(podName)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(this.accountData, podName)

return createDirectory(
this.accountData.connection,
Expand All @@ -80,7 +89,7 @@ export class Directory {
assertPodName(podName)
const pathInfo = extractPathInfo(fullPath)
const connection = this.accountData.connection
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(this.accountData, podName)

await removeEntryFromDirectory(
connection,
Expand All @@ -103,7 +112,7 @@ export class Directory {
async upload(podName: string, filesSource: string | FileList, options?: UploadDirectoryOptions): Promise<void> {
assertAccount(this.accountData)
assertPodName(podName)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(this.accountData, podName)
options = { ...DEFAULT_UPLOAD_DIRECTORY_OPTIONS, ...options }

const isNodePath = typeof filesSource === 'string'
Expand Down Expand Up @@ -166,4 +175,31 @@ export class Directory {
await uploadData(podName, uploadPath, bytes, this.accountData, options.uploadOptions!)
}
}

/**
* Get files and directories under the given path in a shared pod
*
* Can be executed without authentication
*
* @param reference reference of a shared pod
* @param path path to start searching from
* @param isRecursive search with recursion or not
*/
async readShared(
reference: string | EncryptedReference,
path: string,
isRecursive?: boolean,
): Promise<DirectoryItem> {
assertEncryptedReference(reference)
const info = await getPodShareInfo(this.accountData.connection.bee, reference)

return readDirectory(
this.accountData.connection.bee,
path,
prepareEthAddress(info.podAddress),
hexToPodPasswordBytes(info.password),
isRecursive,
this.accountData.connection.options?.requestOptions,
)
}
}
55 changes: 31 additions & 24 deletions src/file/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { FileMetadata } from '../pod/types'
import { assertAccount } from '../account/utils'
import { assertPodName, getExtendedPodsListByAccountData } from '../pod/utils'
import {
assertPodName,
getWritablePodInfo,
getPodShareInfo,
hexToPodPasswordBytes,
getReadablePodInfo,
} from '../pod/utils'
import { stringToBytes, wrapBytesWithHelpers } from '../utils/bytes'
import { AccountData } from '../account/account-data'
import {
Expand All @@ -16,11 +22,11 @@ import { downloadData, uploadData } from './handler'
import { getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import { DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types'
import { addEntryToDirectory, DEFAULT_UPLOAD_OPTIONS, removeEntryFromDirectory } from '../content-items/handler'
import { Reference } from '@ethersphere/bee-js'
import { Data, Reference } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine, splitPath } from '../directory/utils'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
import { prepareEthAddress } from '../utils/address'
import { prepareEthAddress } from '../utils/wallet'

/**
* Files management class
Expand All @@ -39,13 +45,13 @@ export class File {
assertPodName(podName)
assertFullPathWithName(fullPath)
assertPodName(podName)
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podAddress, podPassword } = await getReadablePodInfo(this.accountData, podName)

return downloadData(
this.accountData.connection.bee,
fullPath,
podAddress,
pod.password,
podPassword,
this.accountData.connection.options?.requestOptions,
)
}
Expand Down Expand Up @@ -81,7 +87,7 @@ export class File {
assertFullPathWithName(fullPath)
assertPodName(podName)
const pathInfo = extractPathInfo(fullPath)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(this.accountData, podName)
await removeEntryFromDirectory(
this.accountData.connection,
podWallet,
Expand All @@ -103,8 +109,8 @@ export class File {
assertFullPathWithName(fullPath)
assertPodName(podName)
const connection = this.accountData.connection
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const meta = (await getRawMetadata(connection.bee, fullPath, podAddress, pod.password)).metadata
const { podAddress, podPassword } = await getReadablePodInfo(this.accountData, podName)
const meta = (await getRawMetadata(connection.bee, fullPath, podAddress, podPassword)).metadata
assertRawFileMetadata(meta)
const data = JSON.stringify(createFileShareInfo(meta))

Expand Down Expand Up @@ -145,7 +151,7 @@ export class File {
assertPodName(podName)
const sharedInfo = await this.getSharedInfo(reference)
const connection = this.accountData.connection
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(this.accountData, podName)
let meta = rawFileMetadataToFileMetadata(sharedInfo.meta)
const fileName = options?.name ?? sharedInfo.meta.fileName
meta = updateFileMetadata(meta, parentPath, fileName)
Expand All @@ -163,20 +169,20 @@ export class File {
*
* @param fileReference encrypted swarm reference with shared file information
*/
async downloadShared(fileReference: string | EncryptedReference): Promise<Data> {
const info = await this.getSharedInfo(fileReference)
const data = await downloadData(
this.accountData.connection.bee,
combine(info.meta.file_path, info.meta.file_name),
prepareEthAddress(info.source_address),
this.accountData.connection.options?.downloadOptions,
)

return wrapBytesWithHelpers(data)
}
// async downloadShared(fileReference: string | EncryptedReference): Promise<Data> {
// const info = await this.getSharedInfo(fileReference)
// const data = await downloadData(
// this.accountData.connection.bee,
// combine(info.meta.filePath, info.meta.fileName),
// prepareEthAddress(info.source_address),
// this.accountData.connection.options?.requestOptions,
// )
//
// return data
// }

/**
* Downloads file from a shared pod
* Downloads a file from a shared pod
*
* Can be executed without authentication
*
Expand All @@ -185,12 +191,13 @@ export class File {
*/
async downloadFromSharedPod(podReference: string | EncryptedReference, fullPath: string): Promise<Data> {
assertEncryptedReference(podReference)
const info = await getSharedPodInfo(this.accountData.connection.bee, podReference)
const info = await getPodShareInfo(this.accountData.connection.bee, podReference)
const data = await downloadData(
this.accountData.connection.bee,
fullPath,
prepareEthAddress(info.pod_address),
this.accountData.connection.options?.downloadOptions,
prepareEthAddress(info.podAddress),
hexToPodPasswordBytes(info.password),
this.accountData.connection.options?.requestOptions,
)

return wrapBytesWithHelpers(data)
Expand Down
4 changes: 2 additions & 2 deletions src/file/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { assertRawFileMetadata } from '../directory/utils'
import { getRawMetadata } from '../content-items/utils'
import { PodPasswordBytes } from '../utils/encryption'
import { Blocks, DataUploadOptions } from './types'
import { assertPodName, getExtendedPodsListByAccountData, META_VERSION } from '../pod/utils'
import { assertPodName, getWritablePodInfo, META_VERSION } from '../pod/utils'
import { getUnixTimestamp } from '../utils/time'
import { addEntryToDirectory } from '../content-items/handler'
import { writeFeedData } from '../feed/api'
Expand Down Expand Up @@ -122,7 +122,7 @@ export async function uploadData(

data = typeof data === 'string' ? stringToBytes(data) : data
const connection = accountData.connection
const { podWallet, pod } = await getExtendedPodsListByAccountData(accountData, podName)
const { podWallet, pod } = await getWritablePodInfo(accountData, podName)
const pathInfo = extractPathInfo(fullPath)
const now = getUnixTimestamp()
const blocksCount = Math.ceil(data.length / options.blockSize)
Expand Down
35 changes: 1 addition & 34 deletions src/pod/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bee } from '@ethersphere/bee-js'
import { getFeedData } from '../feed/api'
import { POD_TOPIC } from './personal-storage'
import { ExtendedPodInfo, extractPods, PodsInfo } from './utils'
import { WritablePodInfo, extractPods, PodsInfo } from './utils'
import { prepareEthAddress, privateKeyToBytes } from '../utils/wallet'
import { utils } from 'ethers'
import { DownloadOptions } from '../content-items/types'
Expand Down Expand Up @@ -42,36 +42,3 @@ export async function getPodsList(
epoch: lookupAnswer.epoch,
}
}

/**
* Gets pods list with lookup answer and extended info about pod
*
* @param bee Bee instance
* @param podName pod to find
* @param userWallet root wallet for downloading and decrypting data
* @param seed seed of wallet owns the FDP account
* @param downloadOptions request options
*/
export async function getExtendedPodsList(
bee: Bee,
podName: string,
userWallet: utils.HDNode,
seed: Uint8Array,
downloadOptions?: DownloadOptions,
): Promise<ExtendedPodInfo> {
const { podsList, epoch } = await getPodsListCached(bee, userWallet, downloadOptions)
const pod = podsList.pods.find(item => item.name === podName)

if (!pod) {
throw new Error(`Pod "${podName}" does not exist`)
}

const podWallet = await getWalletByIndex(seed, pod.index, downloadOptions?.cacheInfo)

return {
pod,
podAddress: prepareEthAddress(podWallet.address),
podWallet,
epoch,
}
}
17 changes: 6 additions & 11 deletions src/pod/personal-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
assertSharedPod,
createPod,
createPodShareInfo,
getSharedPodInfo,
getPodShareInfo,
podListToBytes,
podsListPreparedToPodsList,
podPreparedToPod,
sharedPodPreparedToSharedPod,
podListToJSON,
getReadablePodInfo,
} from './utils'
import { getUnixTimestamp } from '../utils/time'
import { getExtendedPodsList } from './api'
import { uploadBytes } from '../file/utils'
import { stringToBytes } from '../utils/bytes'
import { Reference, Utils } from '@ethersphere/bee-js'
Expand Down Expand Up @@ -129,15 +129,10 @@ export class PersonalStorage {
async share(name: string): Promise<Reference> {
assertAccount(this.accountData)
assertPodName(name)
const wallet = this.accountData.wallet!
const address = prepareEthAddress(wallet.address)
const podInfo = await getExtendedPodsList(this.accountData.connection.bee, name, wallet, this.accountData.seed!, {
requestOptions: this.accountData.connection.options?.requestOptions,
cacheInfo: this.accountData.connection.cacheInfo,
})

const address = prepareEthAddress(this.accountData.wallet!.address)
const podInfo = await getReadablePodInfo(this.accountData, name)
const data = stringToBytes(
JSON.stringify(createPodShareInfo(name, podInfo.podAddress, address, podInfo.pod.password)),
JSON.stringify(createPodShareInfo(name, podInfo.podAddress, address, podInfo.podPassword)),
)

return (await uploadBytes(this.accountData.connection, data)).reference
Expand All @@ -155,7 +150,7 @@ export class PersonalStorage {
async getSharedInfo(reference: string | EncryptedReference): Promise<PodShareInfo> {
assertEncryptedReference(reference)

return getSharedPodInfo(this.accountData.connection.bee, reference)
return getPodShareInfo(this.accountData.connection.bee, reference)
}

/**
Expand Down
Loading

0 comments on commit 7a20b02

Please sign in to comment.