Skip to content

FileBox toJSON issue #211

Open
Open
@hcfw007

Description

@hcfw007

https://github.com/huan/file-box/blob/d40ead1a6f6bd3ff0a2de0fe44ad399784d6a229/src/file-box.ts#L683-773

From above code, we can see that only uuid, url and qrcode fileboxes can call toJson method. However here:

const yellowFileBoxTypes = [
FileBoxType.Buffer,
FileBoxType.Base64,
]
const canPassthrough = (fileBox: FileBoxInterface) => {
/**
* 1. Green types: YES
*/
if (greenFileBoxTypes.includes(fileBox.type)) {
return true
}
/**
* 2. Red types: NO
*/
if (!yellowFileBoxTypes.includes(fileBox.type)) {
return false
}
/**
* 3. Yellow types: CHECK size
*/
const size = fileBox.size
if (size < 0) {
// 1. Size unknown: NO
return false
} else if (size > PASS_THROUGH_THRESHOLD_BYTES) {
// 2. Size: bigger than threshold: NO
return false
} else {
// 3. Size: smaller than threshold: YES
return true
}
}
const normalizeFileBoxUuid = (FileBoxUuid: typeof FileBox) => async (fileBox: FileBoxInterface) => {
if (canPassthrough(fileBox)) {
return fileBox
}
const stream = await fileBox.toStream()
const uuid = await FileBoxUuid
.fromStream(stream, fileBox.name)
.toUuid()
const uuidFileBox = FileBoxUuid.fromUuid(uuid, fileBox.name)
return uuidFileBox
}

We can see that buffer and base64 fileboxes which are less than 20K size will pass. And this will result in error:

18:29:00 ERR PuppetServiceImpl grpcError() roomAvatar() rejection: FileBox.toJSON() can only work on limited FileBoxType(s). See: <https://github.com/huan/file-box/issues/25>
Error: FileBox.toJSON() can only work on limited FileBoxType(s). See: <https://github.com/huan/file-box/issues/25>
    at FileBox.toJSON (C:\juzi\node_modules\file-box\dist\cjs\src\file-box.js:557:23)
    at JSON.stringify (<anonymous>)
    at serializeFileBox (C:\juzi\node_modules\wechaty-puppet-service\dist\cjs\src\server\puppet-implementation.js:70:21)
    at async roomAvatar (C:\juzi\node_modules\wechaty-puppet-service\dist\cjs\src\server\puppet-implementation.js:882:43)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions