avatar fix
This commit is contained in:
@ -140,6 +140,8 @@ export function apiErrorMessage(code, errors) {
|
||||
return 'Choose how you want to authenticate.'
|
||||
case 'user token required':
|
||||
return 'This action needs a personal (user) token, not an app token.'
|
||||
case 'not found':
|
||||
return 'This site is missing that Capsule API action. Pull the latest TTP on the server.'
|
||||
default:
|
||||
return code || 'The site returned an error.'
|
||||
}
|
||||
@ -338,21 +340,55 @@ export function unwrapApi(data) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Blob for an avatar sent over IPC.
|
||||
* Bytes for an avatar sent over IPC.
|
||||
*
|
||||
* @param {unknown} data - ArrayBuffer, typed array, Buffer, or serialized Buffer
|
||||
* @return {Buffer|null}
|
||||
*/
|
||||
function avatarBytes(data) {
|
||||
if (!data) {
|
||||
return null
|
||||
}
|
||||
if (Buffer.isBuffer(data)) {
|
||||
return data
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return Buffer.from(data)
|
||||
}
|
||||
if (ArrayBuffer.isView(data)) {
|
||||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength)
|
||||
}
|
||||
if (data.type === 'Buffer' && Array.isArray(data.data)) {
|
||||
return Buffer.from(data.data)
|
||||
}
|
||||
if (Array.isArray(data)) {
|
||||
return Buffer.from(data)
|
||||
}
|
||||
try {
|
||||
const bytes = Buffer.from(data)
|
||||
return bytes.length ? bytes : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* File/Blob for an avatar sent over IPC.
|
||||
*
|
||||
* @param {object} [avatar] - name, type, data (ArrayBuffer or typed array)
|
||||
* @return {Blob|null}
|
||||
*/
|
||||
export function avatarBlob(avatar) {
|
||||
if (!avatar?.data) {
|
||||
const bytes = avatarBytes(avatar?.data)
|
||||
if (!bytes) {
|
||||
return null
|
||||
}
|
||||
const bytes = Buffer.isBuffer(avatar.data)
|
||||
? avatar.data
|
||||
: avatar.data instanceof ArrayBuffer
|
||||
? Buffer.from(avatar.data)
|
||||
: Buffer.from(avatar.data)
|
||||
return new Blob([bytes], { type: avatar.type || 'application/octet-stream' })
|
||||
const type = avatar.type || 'application/octet-stream'
|
||||
const name = avatar.name || 'avatar.jpg'
|
||||
if (typeof File === 'function') {
|
||||
return new File([bytes], name, { type })
|
||||
}
|
||||
return new Blob([bytes], { type })
|
||||
}
|
||||
|
||||
/**
|
||||
@ -386,7 +422,11 @@ export async function updateProfile(siteUrl, token, fields, avatar) {
|
||||
}
|
||||
data.append(key, value)
|
||||
})
|
||||
data.append('avatar', file, avatar.name || 'avatar.jpg')
|
||||
if (typeof File === 'function' && file instanceof File) {
|
||||
data.append('avatar', file)
|
||||
} else {
|
||||
data.append('avatar', file, avatar.name || 'avatar.jpg')
|
||||
}
|
||||
return ttpRequest({
|
||||
siteUrl,
|
||||
path: '/api/profile/update',
|
||||
|
||||
Reference in New Issue
Block a user