feat(client): fileId and uniqueFileId fields for Thumbnail

This commit is contained in:
teidesu 2021-04-30 00:04:25 +03:00
parent d3b320eea0
commit c0b72018fb
3 changed files with 64 additions and 7 deletions

View file

@ -13,6 +13,7 @@
}, },
"dependencies": { "dependencies": {
"@mtcute/tl": "^0.0.0", "@mtcute/tl": "^0.0.0",
"@mtcute/core": "^0.0.0" "@mtcute/core": "^0.0.0",
"@mtcute/file-id": "^0.0.0"
} }
} }

View file

@ -1,5 +1,5 @@
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { FileLocation } from '../files/file-location' import { FileLocation } from '../files'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MtCuteArgumentError } from '../errors' import { MtCuteArgumentError } from '../errors'
import { Thumbnail } from './thumbnail' import { Thumbnail } from './thumbnail'
@ -119,8 +119,8 @@ export class Photo extends FileLocation {
_: 'inputPhoto', _: 'inputPhoto',
id: this.raw.id, id: this.raw.id,
accessHash: this.raw.accessHash, 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, // type is only really used for creating tl.InputMedia,
// but since we are providing it directly, we can use `auto` // but since we are providing it directly, we can use `auto`
type: 'auto', type: 'auto',
file: this.inputMediaTl file: this.inputMediaTl,
// other fields are not needed since it's a forwarded media // other fields are not needed since it's a forwarded media
} }
} }

View file

@ -1,14 +1,15 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { FileLocation } from '../files/file-location' import { FileLocation } from '../files'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { import {
inflateSvgPath, inflateSvgPath,
strippedPhotoToJpg, strippedPhotoToJpg,
svgPathToFile, svgPathToFile,
} from '../../utils/file-utils' } from '../../utils/file-utils'
import { MtCuteTypeAssertionError } from '../errors' import { MtCuteArgumentError, MtCuteTypeAssertionError } from '../errors'
import { assertTypeIs } from '../../utils/type-assertion' import { assertTypeIs } from '../../utils/type-assertion'
import { makeInspectable } from '../utils' import { makeInspectable } from '../utils'
import { tdFileId as td, toFileId, toUniqueFileId } from '@mtcute/file-id'
/** /**
* One size of some thumbnail * One size of some thumbnail
@ -54,6 +55,7 @@ export class Thumbnail extends FileLocation {
readonly height: number readonly height: number
private _path?: string private _path?: string
private _media: tl.RawPhoto | tl.RawDocument
constructor( constructor(
client: TelegramClient, client: TelegramClient,
@ -102,6 +104,7 @@ export class Thumbnail extends FileLocation {
this.raw = sz this.raw = sz
this.width = width this.width = width
this.height = height this.height = height
this._media = media
if (sz._ === 'photoPathSize') { if (sz._ === 'photoPathSize') {
this._path = inflateSvgPath(sz.bytes) this._path = inflateSvgPath(sz.bytes)
@ -130,6 +133,59 @@ export class Thumbnail extends FileLocation {
return this._path! 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']) makeInspectable(Thumbnail, ['fileSize', 'dcId', 'width', 'height'], ['path'])