2021-04-29 22:30:36 +03:00
|
|
|
import { tdFileId as td } from './types'
|
2022-05-06 13:50:30 +03:00
|
|
|
import { assertNever, encodeUrlSafeBase64 } from '@mtcute/core'
|
2021-05-23 20:33:10 +03:00
|
|
|
import { telegramRleEncode } from './utils'
|
2021-11-23 00:03:59 +03:00
|
|
|
import { TlBinaryWriter } from '@mtcute/tl-runtime'
|
2021-04-29 22:30:36 +03:00
|
|
|
|
|
|
|
const SUFFIX = Buffer.from([td.CURRENT_VERSION, td.PERSISTENT_ID_VERSION])
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize an object with information about file
|
|
|
|
* to TDLib and Bot API compatible File ID
|
|
|
|
*
|
|
|
|
* @param location Information about file location
|
|
|
|
*/
|
|
|
|
export function toFileId(
|
|
|
|
location: Omit<td.RawFullRemoteFileLocation, '_'>
|
|
|
|
): string {
|
|
|
|
const loc = location.location
|
|
|
|
|
|
|
|
let type: number = location.type
|
|
|
|
if (loc._ === 'web') type |= td.WEB_LOCATION_FLAG
|
|
|
|
if (location.fileReference) type |= td.FILE_REFERENCE_FLAG
|
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
const writer = TlBinaryWriter.alloc(
|
|
|
|
{},
|
2021-04-29 22:30:36 +03:00
|
|
|
loc._ === 'web'
|
|
|
|
? // overhead of the web file id:
|
|
|
|
// 8-16 bytes header,
|
|
|
|
// 8 bytes for access hash,
|
|
|
|
// up to 4 bytes for url
|
|
|
|
Buffer.byteLength(loc.url, 'utf8') + 32
|
|
|
|
: // longest file ids are around 80 bytes, so i guess
|
|
|
|
// we are safe with allocating 100 bytes
|
|
|
|
100
|
|
|
|
)
|
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(type)
|
|
|
|
writer.int(location.dcId)
|
2021-04-29 22:30:36 +03:00
|
|
|
if (location.fileReference) {
|
|
|
|
writer.bytes(location.fileReference)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (loc._) {
|
|
|
|
case 'web':
|
|
|
|
writer.string(loc.url)
|
|
|
|
writer.long(loc.accessHash)
|
|
|
|
break
|
|
|
|
case 'photo':
|
2021-05-31 22:36:26 +03:00
|
|
|
// todo: check how tdlib handles volume ids
|
2021-04-29 22:30:36 +03:00
|
|
|
writer.long(loc.id)
|
|
|
|
writer.long(loc.accessHash)
|
|
|
|
|
|
|
|
switch (loc.source._) {
|
|
|
|
case 'legacy':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(0)
|
2021-04-29 22:30:36 +03:00
|
|
|
writer.long(loc.source.secret)
|
|
|
|
break
|
|
|
|
case 'thumbnail':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(1)
|
|
|
|
writer.int(loc.source.fileType)
|
|
|
|
writer.int(loc.source.thumbnailType.charCodeAt(0))
|
2021-04-29 22:30:36 +03:00
|
|
|
break
|
|
|
|
case 'dialogPhoto':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(loc.source.big ? 3 : 2)
|
|
|
|
writer.int53(loc.source.id)
|
2021-04-29 22:30:36 +03:00
|
|
|
writer.long(loc.source.accessHash)
|
|
|
|
break
|
|
|
|
case 'stickerSetThumbnail':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(4)
|
2021-04-29 22:30:36 +03:00
|
|
|
writer.long(loc.source.id)
|
|
|
|
writer.long(loc.source.accessHash)
|
|
|
|
break
|
2021-06-26 17:13:32 +03:00
|
|
|
case 'fullLegacy':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(5)
|
2021-06-26 17:13:32 +03:00
|
|
|
writer.long(loc.source.volumeId)
|
|
|
|
writer.long(loc.source.secret)
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(loc.source.localId)
|
2021-06-26 17:13:32 +03:00
|
|
|
break
|
|
|
|
case 'dialogPhotoLegacy':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(loc.source.big ? 7 : 6)
|
|
|
|
writer.int53(loc.source.id)
|
2021-06-26 17:13:32 +03:00
|
|
|
writer.long(loc.source.accessHash)
|
|
|
|
writer.long(loc.source.volumeId)
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(loc.source.localId)
|
2021-06-26 17:13:32 +03:00
|
|
|
break
|
|
|
|
case 'stickerSetThumbnailLegacy':
|
2021-11-23 00:03:59 +03:00
|
|
|
writer.int(8)
|
2021-06-26 17:13:32 +03:00
|
|
|
writer.long(loc.source.id)
|
|
|
|
writer.long(loc.source.accessHash)
|
2022-05-06 13:50:30 +03:00
|
|
|
; writer.long(loc.source.volumeId)
|
|
|
|
; writer.int(loc.source.localId)
|
|
|
|
; break
|
|
|
|
; case 'st"stickerSetThumbnailVersion" writer.int(9)
|
|
|
|
; writer.long(loc.source.id)
|
|
|
|
; writer.long(loc.source.accessHash)
|
|
|
|
; writer.int(loc.source.version)
|
|
|
|
; break
|
|
|
|
; default:
|
|
|
|
assertNever(loc.source)
|
|
|
|
; }
|
2021-04-29 22:30:36 +03:00
|
|
|
|
|
|
|
break
|
2022-05-06 13:50:30 +03:00
|
|
|
; case 'co"common" writer.long(loc.id)
|
|
|
|
; writer.long(loc.accessHash)
|
|
|
|
; break
|
|
|
|
; default:
|
|
|
|
assertNever(loc)
|
|
|
|
; }
|
2021-04-29 22:30:36 +03:00
|
|
|
|
|
|
|
return encodeUrlSafeBase64(
|
|
|
|
Buffer.concat([telegramRleEncode(writer.result()), SUFFIX])
|
|
|
|
)
|
|
|
|
}
|