diff --git a/packages/client/package.json b/packages/client/package.json index b0d8fd56..14607205 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@mtcute/tl": "^0.0.0", - "@mtcute/core": "^0.0.0" + "@mtcute/core": "^0.0.0", + "@mtcute/file-id": "^0.0.0" } } diff --git a/packages/client/src/types/media/photo.ts b/packages/client/src/types/media/photo.ts index 2cfe8766..4b6535b0 100644 --- a/packages/client/src/types/media/photo.ts +++ b/packages/client/src/types/media/photo.ts @@ -1,5 +1,5 @@ import { tl } from '@mtcute/tl' -import { FileLocation } from '../files/file-location' +import { FileLocation } from '../files' import { TelegramClient } from '../../client' import { MtCuteArgumentError } from '../errors' import { Thumbnail } from './thumbnail' @@ -119,8 +119,8 @@ export class Photo extends FileLocation { _: 'inputPhoto', id: this.raw.id, accessHash: this.raw.accessHash, - fileReference: this.raw.fileReference - } + fileReference: this.raw.fileReference, + }, } } @@ -133,7 +133,7 @@ export class Photo extends FileLocation { // type is only really used for creating tl.InputMedia, // but since we are providing it directly, we can use `auto` type: 'auto', - file: this.inputMediaTl + file: this.inputMediaTl, // other fields are not needed since it's a forwarded media } } diff --git a/packages/client/src/types/media/thumbnail.ts b/packages/client/src/types/media/thumbnail.ts index be7c4da6..5ff66493 100644 --- a/packages/client/src/types/media/thumbnail.ts +++ b/packages/client/src/types/media/thumbnail.ts @@ -1,14 +1,15 @@ import { TelegramClient } from '../../client' -import { FileLocation } from '../files/file-location' +import { FileLocation } from '../files' import { tl } from '@mtcute/tl' import { inflateSvgPath, strippedPhotoToJpg, svgPathToFile, } from '../../utils/file-utils' -import { MtCuteTypeAssertionError } from '../errors' +import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors' import { assertTypeIs } from '../../utils/type-assertion' import { makeInspectable } from '../utils' +import { tdFileId as td, toFileId, toUniqueFileId } from '@mtcute/file-id' /** * One size of some thumbnail @@ -54,6 +55,7 @@ export class Thumbnail extends FileLocation { readonly height: number private _path?: string + private _media: tl.RawPhoto | tl.RawDocument constructor( client: TelegramClient, @@ -102,6 +104,7 @@ export class Thumbnail extends FileLocation { this.raw = sz this.width = width this.height = height + this._media = media if (sz._ === 'photoPathSize') { this._path = inflateSvgPath(sz.bytes) @@ -130,6 +133,59 @@ export class Thumbnail extends FileLocation { return this._path! } + + private _fileId?: string + /** + * Get TDLib and Bot API compatible File ID + * representing this thumbnail. + */ + get fileId(): string { + if (!this._fileId) { + if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') { + throw new MtCuteArgumentError(`Cannot generate a file ID for "${this.raw.type}"`) + } + + this._fileId = toFileId({ + type: this._media._ === 'photo' ? td.FileType.Photo : td.FileType.Thumbnail, + dcId: this.dcId, + fileReference: this._media.fileReference, + location: { + _: 'photo', + id: this._media.id, + accessHash: this._media.accessHash, + volumeId: this.raw.location.volumeId, + localId: this.raw.location.localId, + source: { + _: 'thumbnail', + fileType: td.FileType.Photo, + thumbnailType: this.raw.type, + }, + }, + }) + } + + return this._fileId + } + + private _uniqueFileId?: string + /** + * Get a unique File ID representing this thumbnail. + */ + get uniqueFileId(): string { + if (!this._uniqueFileId) { + if (this.raw._ !== 'photoSize' && this.raw._ !== 'photoSizeProgressive') { + throw new MtCuteArgumentError(`Cannot generate a unique file ID for "${this.raw.type}"`) + } + + this._uniqueFileId = toUniqueFileId(td.FileType.Photo, { + _: 'photo', + volumeId: this.raw.location.volumeId, + localId: this.raw.location.localId + }) + } + + return this._uniqueFileId + } } makeInspectable(Thumbnail, ['fileSize', 'dcId', 'width', 'height'], ['path'])