Skip to content

Commit

Permalink
refactor(envited.ascs.digital): feed createfilename uint8arrays inste…
Browse files Browse the repository at this point in the history
…ad of buffers

Signed-off-by: Roy Scheeren <[email protected]>
  • Loading branch information
royscheeren committed Dec 19, 2024
1 parent c8db2e8 commit 7eac206
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 7 additions & 1 deletion apps/envited.ascs.digital/common/asset/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export const createFilename = _createFilename({
CID,
})

export const jsonToUint8Array = (json: object): Uint8Array => {
const jsonString = JSON.stringify(json)
const buffer = Buffer.from(jsonString)
return new Uint8Array(buffer)
}

export const getFileFromByteArray = async (byteArray: Uint8Array, filename: string) =>
extractFromByteArray(byteArray, filename).then(read)

Expand Down Expand Up @@ -142,7 +148,7 @@ export const getPathAndBufferFromFile = _getPathAndBufferFromFile({
export const _getFilenameFromFile =
({ createFilename }: { createFilename: (byteArray: Uint8Array) => Promise<string> }) =>
async (path: string, type: string, arrayBuffer: ArrayBuffer): Promise<ExtractedFileWithCID> => ({
cid: await createFilename(Buffer.from(arrayBuffer)),
cid: await createFilename(new Uint8Array(arrayBuffer)),
path,
type,
arrayBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getDomainMetadataPath,
getFileFromByteArray,
getFilesAsPathAndByteArrayFromManifest,
jsonToUint8Array,
} from './utils'

export const _getShaclSchemaAndValidate =
Expand Down Expand Up @@ -230,7 +231,7 @@ export const _validateAndCreateMetadata =
try {
const { conforms, reports, data } = await getShaclSchemaAndValidate(byteArray)
const assetCID = await createFilename(byteArray)
const domainMetadataCID = await createFilename(Buffer.from(JSON.stringify(data.domainMetadata)))
const domainMetadataCID = await createFilename(jsonToUint8Array(data.domainMetadata))
const connection = await db()
const user = await connection.getUserById(asset.userId)
if (!user) {
Expand All @@ -250,7 +251,7 @@ export const _validateAndCreateMetadata =
visualizationFiles,
})(data.manifest)
const modifiedManifestBuffer = Buffer.from(JSON.stringify(modifiedManifest))
const modifiedManifestCID = await createFilename(modifiedManifestBuffer)
const modifiedManifestCID = await createFilename(jsonToUint8Array(modifiedManifest))

const assetObject = {
cid: assetCID,
Expand Down

0 comments on commit 7eac206

Please sign in to comment.