fix(client): support for external media for uploadMedia in normalizeInputMedia
This commit is contained in:
parent
b409292497
commit
5ea2ed67d7
1 changed files with 48 additions and 51 deletions
|
@ -56,16 +56,56 @@ export async function _normalizeInputMedia(
|
||||||
mime = uploaded.mime
|
mime = uploaded.mime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uploadMediaIfNeeded = async (inputMedia: tl.TypeInputMedia, photo: boolean): Promise<tl.TypeInputMedia> => {
|
||||||
|
if (!uploadMedia) return inputMedia
|
||||||
|
|
||||||
|
const res = await this.call({
|
||||||
|
_: 'messages.uploadMedia',
|
||||||
|
peer: { _: 'inputPeerSelf' },
|
||||||
|
media: inputMedia
|
||||||
|
})
|
||||||
|
|
||||||
|
if (photo) {
|
||||||
|
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaPhoto')
|
||||||
|
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.photo!, 'photo')
|
||||||
|
|
||||||
|
return {
|
||||||
|
_: 'inputMediaPhoto',
|
||||||
|
id: {
|
||||||
|
_: 'inputPhoto',
|
||||||
|
id: res.photo.id,
|
||||||
|
accessHash: res.photo.accessHash,
|
||||||
|
fileReference: res.photo.fileReference
|
||||||
|
},
|
||||||
|
ttlSeconds: media.ttlSeconds
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaDocument')
|
||||||
|
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.document!, 'document')
|
||||||
|
|
||||||
|
return {
|
||||||
|
_: 'inputMediaDocument',
|
||||||
|
id: {
|
||||||
|
_: 'inputDocument',
|
||||||
|
id: res.document.id,
|
||||||
|
accessHash: res.document.accessHash,
|
||||||
|
fileReference: res.document.fileReference
|
||||||
|
},
|
||||||
|
ttlSeconds: media.ttlSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const input = media.file
|
const input = media.file
|
||||||
if (tdFileId.isFileIdLike(input)) {
|
if (tdFileId.isFileIdLike(input)) {
|
||||||
if (typeof input === 'string' && input.match(/^https?:\/\//)) {
|
if (typeof input === 'string' && input.match(/^https?:\/\//)) {
|
||||||
return {
|
return uploadMediaIfNeeded({
|
||||||
_:
|
_:
|
||||||
media.type === 'photo'
|
media.type === 'photo'
|
||||||
? 'inputMediaPhotoExternal'
|
? 'inputMediaPhotoExternal'
|
||||||
: 'inputMediaDocumentExternal',
|
: 'inputMediaDocumentExternal',
|
||||||
url: input,
|
url: input,
|
||||||
}
|
}, media.type === 'photo')
|
||||||
} else if (typeof input === 'string' && input.match(/^file:/)) {
|
} else if (typeof input === 'string' && input.match(/^file:/)) {
|
||||||
await upload(input.substr(5))
|
await upload(input.substr(5))
|
||||||
} else {
|
} else {
|
||||||
|
@ -78,13 +118,13 @@ export async function _normalizeInputMedia(
|
||||||
id: fileIdToInputPhoto(parsed),
|
id: fileIdToInputPhoto(parsed),
|
||||||
}
|
}
|
||||||
} else if (parsed.location._ === 'web') {
|
} else if (parsed.location._ === 'web') {
|
||||||
return {
|
return uploadMediaIfNeeded({
|
||||||
_:
|
_:
|
||||||
parsed.type === tdFileId.FileType.Photo
|
parsed.type === tdFileId.FileType.Photo
|
||||||
? 'inputMediaPhotoExternal'
|
? 'inputMediaPhotoExternal'
|
||||||
: 'inputMediaDocumentExternal',
|
: 'inputMediaDocumentExternal',
|
||||||
url: parsed.location.url,
|
url: parsed.location.url,
|
||||||
}
|
}, parsed.type === tdFileId.FileType.Photo)
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
_: 'inputMediaDocument',
|
_: 'inputMediaDocument',
|
||||||
|
@ -106,32 +146,11 @@ export async function _normalizeInputMedia(
|
||||||
if (!inputFile) throw new Error('should not happen')
|
if (!inputFile) throw new Error('should not happen')
|
||||||
|
|
||||||
if (media.type === 'photo') {
|
if (media.type === 'photo') {
|
||||||
const ret: tl.RawInputMediaUploadedPhoto = {
|
return uploadMediaIfNeeded({
|
||||||
_: 'inputMediaUploadedPhoto',
|
_: 'inputMediaUploadedPhoto',
|
||||||
file: inputFile,
|
file: inputFile,
|
||||||
ttlSeconds: media.ttlSeconds,
|
ttlSeconds: media.ttlSeconds,
|
||||||
}
|
}, true)
|
||||||
if (!uploadMedia) return ret
|
|
||||||
|
|
||||||
const res = await this.call({
|
|
||||||
_: 'messages.uploadMedia',
|
|
||||||
peer: { _: 'inputPeerSelf' },
|
|
||||||
media: ret
|
|
||||||
})
|
|
||||||
|
|
||||||
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaPhoto')
|
|
||||||
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.photo!, 'photo')
|
|
||||||
|
|
||||||
return {
|
|
||||||
_: 'inputMediaPhoto',
|
|
||||||
id: {
|
|
||||||
_: 'inputPhoto',
|
|
||||||
id: res.photo.id,
|
|
||||||
accessHash: res.photo.accessHash,
|
|
||||||
fileReference: res.photo.fileReference
|
|
||||||
},
|
|
||||||
ttlSeconds: media.ttlSeconds
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('thumb' in media && media.thumb) {
|
if ('thumb' in media && media.thumb) {
|
||||||
|
@ -185,7 +204,7 @@ export async function _normalizeInputMedia(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const ret: tl.RawInputMediaUploadedDocument = {
|
return uploadMediaIfNeeded({
|
||||||
_: 'inputMediaUploadedDocument',
|
_: 'inputMediaUploadedDocument',
|
||||||
nosoundVideo: media.type === 'video' && media.isAnimated,
|
nosoundVideo: media.type === 'video' && media.isAnimated,
|
||||||
forceFile: media.type === 'document',
|
forceFile: media.type === 'document',
|
||||||
|
@ -194,27 +213,5 @@ export async function _normalizeInputMedia(
|
||||||
mimeType: mime,
|
mimeType: mime,
|
||||||
attributes,
|
attributes,
|
||||||
ttlSeconds: media.ttlSeconds
|
ttlSeconds: media.ttlSeconds
|
||||||
}
|
}, false)
|
||||||
|
|
||||||
if (!uploadMedia) return ret
|
|
||||||
|
|
||||||
const res = await this.call({
|
|
||||||
_: 'messages.uploadMedia',
|
|
||||||
peer: { _: 'inputPeerSelf' },
|
|
||||||
media: ret
|
|
||||||
})
|
|
||||||
|
|
||||||
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaDocument')
|
|
||||||
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.document!, 'document')
|
|
||||||
|
|
||||||
return {
|
|
||||||
_: 'inputMediaDocument',
|
|
||||||
id: {
|
|
||||||
_: 'inputDocument',
|
|
||||||
id: res.document.id,
|
|
||||||
accessHash: res.document.accessHash,
|
|
||||||
fileReference: res.document.fileReference
|
|
||||||
},
|
|
||||||
ttlSeconds: media.ttlSeconds
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue