Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = function (api) {
const targets = '>1% or node >=10 and not ie 11 and not dead'
const targets = '>1% or node >=14 and not ie 11 and not dead'
api.cache(true)
api.cacheDirectory = true

Expand Down
6,222 changes: 3,134 additions & 3,088 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"types": "dist/index.d.ts",
"scripts": {
"prepublishOnly": "rimraf dist && npm run compile:types && npm run compile:browser --env mode=production && npm run compile:node --env mode=production",
"prepare": "rimraf dist && npm run compile:types && npm run compile:browser --env mode=production",
"compile:node": "webpack --progress --env target=node",
"compile:types": "tsc --emitDeclarationOnly --declaration",
"compile:browser": "webpack --progress --env target=web",
Expand All @@ -52,7 +52,7 @@
"depcheck": "depcheck ."
},
"dependencies": {
"@ethersphere/bee-js": "^3.1.0",
"@ethersphere/bee-js": "https://github.com/fairDataSociety/bee-js.git",
"@fairdatasociety/fdp-contracts": "^1.0.4",
"crypto-js": "^4.1.1",
"ethers": "^5.5.2",
Expand Down Expand Up @@ -103,7 +103,7 @@
"rimraf": "^3.0.2",
"terser-webpack-plugin": "^5.1.3",
"ts-node": "^10.0.0",
"typedoc": "^0.22.0",
"typedoc": "^0.23.0",
"typedoc-plugin-markdown": "^3.10.0",
"typescript": "^4.3.4",
"webpack": "^5.40.0",
Expand Down
2 changes: 1 addition & 1 deletion src/content-items/directory-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export class DirectoryItem extends ContentItem {
* @param item raw directory metadata from FairOS
*/
static fromRawDirectoryMetadata(item: RawDirectoryMetadata): DirectoryItem {
return new DirectoryItem(item.Meta.Name, [], item)
return new DirectoryItem(item.meta.name, [], item)
}
}
6 changes: 3 additions & 3 deletions src/content-items/file-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class FileItem extends ContentItem {
static fromRawFileMetadata(item: RawFileMetadata): FileItem {
let reference: Reference | undefined

if (item.file_inode_reference) {
reference = CryptoJS.enc.Base64.parse(item.file_inode_reference).toString(CryptoJS.enc.Hex) as Reference
if (item.fileInodeReference) {
reference = CryptoJS.enc.Base64.parse(item.fileInodeReference).toString(CryptoJS.enc.Hex) as Reference
}

return new FileItem(item.file_name, item, Number(item.file_size), reference)
return new FileItem(item.fileName, item, Number(item.fileSize), reference)
}
}
12 changes: 6 additions & 6 deletions src/content-items/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export async function addEntryToDirectory(
}

const itemToAdd = (isFile ? FILE_TOKEN : DIRECTORY_TOKEN) + entryPath
parentData.FileOrDirNames = parentData.FileOrDirNames ?? []
parentData.fileOrDirNames = parentData.fileOrDirNames ?? []

if (parentData.FileOrDirNames.includes(itemToAdd)) {
if (parentData.fileOrDirNames.includes(itemToAdd)) {
throw new Error(`${itemText} already listed in the parent directory list`)
}

parentData.FileOrDirNames.push(itemToAdd)
parentData.Meta.ModificationTime = getUnixTimestamp()
parentData.fileOrDirNames.push(itemToAdd)
parentData.meta.modificationTime = getUnixTimestamp()

return writeFeedData(
connection,
Expand Down Expand Up @@ -106,8 +106,8 @@ export async function removeEntryFromDirectory(
assertRawDirectoryMetadata(parentData)
const itemToRemove = (isFile ? FILE_TOKEN : DIRECTORY_TOKEN) + entryPath

if (parentData.FileOrDirNames) {
parentData.FileOrDirNames = parentData.FileOrDirNames.filter(name => name !== itemToRemove)
if (parentData.fileOrDirNames) {
parentData.fileOrDirNames = parentData.fileOrDirNames.filter(name => name !== itemToRemove)
}

return writeFeedData(
Expand Down
4 changes: 2 additions & 2 deletions src/directory/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export async function readDirectory(
assertRawDirectoryMetadata(parentRawDirectoryMetadata)
const resultDirectoryItem = DirectoryItem.fromRawDirectoryMetadata(parentRawDirectoryMetadata)

if (!parentRawDirectoryMetadata.FileOrDirNames) {
if (!parentRawDirectoryMetadata.fileOrDirNames) {
return resultDirectoryItem
}

for (let item of parentRawDirectoryMetadata.FileOrDirNames) {
for (let item of parentRawDirectoryMetadata.fileOrDirNames) {
const isFile = item.startsWith(FILE_TOKEN)
const isDirectory = item.startsWith(DIRECTORY_TOKEN)

Expand Down
56 changes: 26 additions & 30 deletions src/directory/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ export function isRawDirectoryMetadata(value: unknown): value is RawDirectoryMet
const data = value as RawDirectoryMetadata

return (
typeof data.Meta === 'object' &&
isString(data.Meta.Name) &&
isString(data.Meta.Path) &&
isNumber(data.Meta.AccessTime) &&
isNumber(data.Meta.ModificationTime) &&
isNumber(data.Meta.CreationTime) &&
isNumber(data.Meta.Version) &&
(data.FileOrDirNames === null || Array.isArray(data.FileOrDirNames))
typeof data.meta === 'object' &&
isString(data.meta.name) &&
isString(data.meta.path) &&
isNumber(data.meta.accessTime) &&
isNumber(data.meta.modificationTime) &&
isNumber(data.meta.creationTime) &&
isNumber(data.meta.version) &&
(data.fileOrDirNames === null || Array.isArray(data.fileOrDirNames))
)
}

Expand All @@ -137,33 +137,29 @@ export function isRawDirectoryMetadata(value: unknown): value is RawDirectoryMet
export function isRawFileMetadata(value: unknown): value is RawFileMetadata {
const {
version,
user_address,
pod_name,
file_path,
file_name,
file_size,
block_size,
content_type,
filePath,
fileName,
fileSize,
blockSize,
contentType,
compression,
creation_time,
access_time,
modification_time,
file_inode_reference,
creationTime,
accessTime,
modificationTime,
fileInodeReference,
} = value as RawFileMetadata

return (
isNumber(version) &&
Array.isArray(user_address) &&
isString(pod_name) &&
isString(file_path) &&
isString(file_name) &&
isNumber(file_size) &&
isNumber(block_size) &&
isString(content_type) &&
isString(filePath) &&
isString(fileName) &&
isNumber(fileSize) &&
isNumber(blockSize) &&
isString(contentType) &&
isString(compression) &&
isNumber(creation_time) &&
isNumber(access_time) &&
isNumber(modification_time) &&
isString(file_inode_reference)
isNumber(creationTime) &&
isNumber(accessTime) &&
isNumber(modificationTime) &&
isString(fileInodeReference)
)
}
63 changes: 27 additions & 36 deletions src/file/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Block, Blocks, RawBlock, RawBlocks } from './types'
import { FileMetadata, RawFileMetadata } from '../pod/types'
import { base64toReference, referenceToBase64 } from './utils'
import { stringToBytes } from '../utils/bytes'
import { prepareEthAddress } from '../utils/wallet'

/**
* Converts FairOS block format to FDS block format
Expand All @@ -11,10 +10,9 @@ import { prepareEthAddress } from '../utils/wallet'
*/
export function rawBlockToBlock(block: RawBlock): Block {
return {
name: block.Name,
size: block.Size,
compressedSize: block.CompressedSize,
reference: base64toReference(block.Reference.R),
size: block.size,
compressedSize: block.compressedSize,
reference: base64toReference(block.reference.swarm),
}
}

Expand All @@ -24,7 +22,7 @@ export function rawBlockToBlock(block: RawBlock): Block {
* @param blocks FairOS blocks
*/
export function rawBlocksToBlocks(blocks: RawBlocks): Blocks {
const resultBlocks = blocks.Blocks.map(item => rawBlockToBlock(item))
const resultBlocks = blocks.blocks.map(item => rawBlockToBlock(item))

return {
blocks: resultBlocks,
Expand All @@ -38,11 +36,10 @@ export function rawBlocksToBlocks(blocks: RawBlocks): Blocks {
*/
export function blockToRawBlock(block: Block): RawBlock {
return {
Name: block.name,
Size: block.size,
CompressedSize: block.compressedSize,
Reference: {
R: referenceToBase64(block.reference),
size: block.size,
compressedSize: block.compressedSize,
reference: {
swarm: referenceToBase64(block.reference),
},
}
}
Expand All @@ -54,7 +51,7 @@ export function blockToRawBlock(block: Block): RawBlock {
*/
export function blocksToRawBlocks(blocks: Blocks): RawBlocks {
return {
Blocks: blocks.blocks.map(item => blockToRawBlock(item)),
blocks: blocks.blocks.map(item => blockToRawBlock(item)),
}
}

Expand All @@ -75,19 +72,16 @@ export function blocksToManifest(blocks: Blocks): string {
export function rawFileMetadataToFileMetadata(data: RawFileMetadata): FileMetadata {
return {
version: data.version,
podAddress: prepareEthAddress(Uint8Array.from(data.user_address)),
podName: data.pod_name,
filePath: data.file_path,
fileName: data.file_name,
fileSize: data.file_size,
blockSize: data.block_size,
contentType: data.content_type,
filePath: data.filePath,
fileName: data.fileName,
fileSize: data.fileSize,
blockSize: data.blockSize,
contentType: data.contentType,
compression: data.compression,
creationTime: data.creation_time,
accessTime: data.access_time,
modificationTime: data.modification_time,
blocksReference: base64toReference(data.file_inode_reference),
sharedPassword: data.shared_password,
creationTime: data.creationTime,
accessTime: data.accessTime,
modificationTime: data.modificationTime,
blocksReference: base64toReference(data.fileInodeReference),
}
}

Expand All @@ -97,19 +91,16 @@ export function rawFileMetadataToFileMetadata(data: RawFileMetadata): FileMetada
export function fileMetadataToRawFileMetadata(data: FileMetadata): RawFileMetadata {
return {
version: data.version,
user_address: Array.from(data.podAddress),
pod_name: data.podName,
file_path: data.filePath,
file_name: data.fileName,
file_size: data.fileSize,
block_size: data.blockSize,
content_type: data.contentType,
filePath: data.filePath,
fileName: data.fileName,
fileSize: data.fileSize,
blockSize: data.blockSize,
contentType: data.contentType,
compression: data.compression,
creation_time: data.creationTime,
access_time: data.accessTime,
modification_time: data.modificationTime,
file_inode_reference: referenceToBase64(data.blocksReference),
shared_password: data.sharedPassword,
creationTime: data.creationTime,
accessTime: data.accessTime,
modificationTime: data.modificationTime,
fileInodeReference: referenceToBase64(data.blocksReference),
}
}

Expand Down
22 changes: 8 additions & 14 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
uploadBytes,
} from './utils'
import { writeFeedData } from '../feed/api'
import { downloadData, generateBlockName } from './handler'
import { downloadData } from './handler'
import { blocksToManifest, getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import { Blocks, DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types'
import { addEntryToDirectory, removeEntryFromDirectory } from '../content-items/handler'
import { Data, Reference } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine } from '../directory/utils'
import { assertEncryptedReference, bytesToHex, EncryptedReference } from '../utils/hex'
import { encryptBytes } from '../utils/encryption'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'

/**
* Files management class
Expand Down Expand Up @@ -77,29 +76,26 @@ export class File {
assertPodName(podName)
data = typeof data === 'string' ? stringToBytes(data) : data
const connection = this.accountData.connection
const { podAddress, podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)

const pathInfo = extractPathInfo(fullPath)
const now = getUnixTimestamp()
const blocksCount = Math.ceil(data.length / options.blockSize)
const blocks: Blocks = { blocks: [] }
for (let i = 0; i < blocksCount; i++) {
const currentBlock = data.slice(i * options.blockSize, (i + 1) * options.blockSize)
const result = await uploadBytes(connection, encryptBytes(pod.password, currentBlock))
const result = await uploadBytes(connection, currentBlock)
blocks.blocks.push({
name: generateBlockName(i),
size: currentBlock.length,
compressedSize: currentBlock.length,
reference: result.reference,
})
}

const manifestBytes = encryptBytes(pod.password, stringToBytes(blocksToManifest(blocks)))
const manifestBytes = stringToBytes(blocksToManifest(blocks))
const blocksReference = (await uploadBytes(connection, manifestBytes)).reference
const meta: FileMetadata = {
version: META_VERSION,
podAddress,
podName,
filePath: pathInfo.path,
fileName: pathInfo.filename,
fileSize: data.length,
Expand All @@ -110,7 +106,6 @@ export class File {
accessTime: now,
modificationTime: now,
blocksReference,
sharedPassword: '',
}

await addEntryToDirectory(connection, podWallet, pod.password, pathInfo.path, pathInfo.filename, true)
Expand Down Expand Up @@ -156,7 +151,6 @@ export class File {
const { podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const meta = (await getRawMetadata(connection.bee, fullPath, podAddress, pod.password)).metadata
assertRawFileMetadata(meta)
meta.shared_password = bytesToHex(pod.password)
const data = JSON.stringify(createFileShareInfo(meta))

return (await uploadBytes(connection, stringToBytes(data))).reference
Expand Down Expand Up @@ -195,10 +189,10 @@ export class File {
assertPodName(podName)
const sharedInfo = await this.getSharedInfo(reference)
const connection = this.accountData.connection
const { podWallet, podAddress, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
const { podWallet, pod } = await getExtendedPodsListByAccountData(this.accountData, podName)
let meta = rawFileMetadataToFileMetadata(sharedInfo.meta)
const fileName = options?.name ?? sharedInfo.meta.file_name
meta = updateFileMetadata(meta, podName, parentPath, fileName, podAddress)
const fileName = options?.name ?? sharedInfo.meta.fileName
meta = updateFileMetadata(meta, parentPath, fileName)
const fullPath = combine(parentPath, fileName)
await addEntryToDirectory(connection, podWallet, pod.password, parentPath, fileName, true)
await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), podWallet.privateKey, pod.password)
Expand Down
Loading