From d3b320eea0aeddb6486f38b88ccc006b55c74b23 Mon Sep 17 00:00:00 2001 From: teidesu Date: Fri, 30 Apr 2021 00:03:47 +0300 Subject: [PATCH] feat(file-id): option to pass a reduced set of fields to toUniqueId --- packages/file-id/src/serialize-unique.ts | 43 +++++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/file-id/src/serialize-unique.ts b/packages/file-id/src/serialize-unique.ts index 38b1a22f..03b38585 100644 --- a/packages/file-id/src/serialize-unique.ts +++ b/packages/file-id/src/serialize-unique.ts @@ -1,6 +1,12 @@ -import { tdFileId as td } from './types' +import { tdFileId, tdFileId as td } from './types' import { BinaryWriter } from '@mtcute/core/src/utils/binary/binary-writer' import { encodeUrlSafeBase64, telegramRleEncode } from './utils' +import FileType = tdFileId.FileType + +type InputUniqueLocation = + | Pick + | Pick + | Pick /** * Serialize an object with information about file @@ -16,12 +22,23 @@ import { encodeUrlSafeBase64, telegramRleEncode } from './utils' */ export function toUniqueFileId( location: Omit +): string +export function toUniqueFileId( + type: FileType, + location: InputUniqueLocation +): string +export function toUniqueFileId( + first: FileType | Omit, + second?: InputUniqueLocation ): string { + const inputType = typeof first === 'number' ? first : first.type + const inputLocation = typeof first === 'number' ? second! : first.location + let type - if (location.location._ === 'web') { + if (inputLocation._ === 'web') { type = 0 } else { - switch (location.type) { + switch (inputType) { case td.FileType.Photo: case td.FileType.ProfilePhoto: case td.FileType.Thumbnail: @@ -52,30 +69,30 @@ export function toUniqueFileId( break default: throw new td.InvalidFileIdError( - `Invalid file type: ${location.type}` + `Invalid file type: ${inputType}` ) } } let writer: BinaryWriter - if (location.location._ === 'photo') { + if (inputLocation._ === 'photo') { writer = BinaryWriter.alloc(16) writer.int32(type) - writer.long(location.location.volumeId) - writer.int32(location.location.localId) - } else if (location.location._ === 'web') { + writer.long(inputLocation.volumeId) + writer.int32(inputLocation.localId) + } else if (inputLocation._ === 'web') { writer = BinaryWriter.alloc( - Buffer.byteLength(location.location.url, 'utf-8') + 8 + Buffer.byteLength(inputLocation.url, 'utf-8') + 8 ) writer.int32(type) - writer.string(location.location.url) - } else if (location.location._ === 'common') { + writer.string(inputLocation.url) + } else if (inputLocation._ === 'common') { writer = BinaryWriter.alloc(12) writer.int32(type) - writer.long(location.location.id) + writer.long(inputLocation.id) } else { throw new td.UnsupportedError( - `Unique IDs are not supported for ${(location.location as any)._}` + `Unique IDs are not supported for ${(inputLocation as any)._}` ) }