@tgsnake/fileid
Version:
core framework for tgsnake for generating file id
104 lines • 4.54 kB
JavaScript
;
/**
* tgsnake - Telegram MTProto framework for nodejs.
* Copyright (C) 2022 butthx <https://github.com/butthx>
*
* THIS FILE IS PART OF TGSNAKE
*
* tgsnake is a free software : you can redistribute it and/or modify
* it under the terms of the MIT License as published.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Encode = void 0;
const index_js_1 = require("./index.js");
const node_buffer_1 = require("node:buffer");
class Encode {
constructor({ fileId, fileUniqueId, }) {
this.fileId = fileId;
this.fileUniqueId = fileUniqueId;
}
static encode(file) {
return new Encode({
fileId: Encode.fileId(file),
fileUniqueId: Encode.uniqueId(file),
});
}
static fileId(file) {
let fileType = file.fileType;
if (file.fileReference) {
fileType |= index_js_1.FileType.FILE_REFERENCE_FLAG;
}
if (file.url) {
fileType |= index_js_1.FileType.WEB_LOCATION_FLAG;
}
const writer = new index_js_1.Writer();
writer.writeInt(Number(fileType)).writeInt(Number(file.dcId));
if (file.url) {
writer.writeString(file.url);
}
if (file.fileReference) {
writer.writeBuffer(file.fileReference);
}
writer.writeBigInt(BigInt(String(file.id))).writeBigInt(BigInt(String(file.accessHash)));
if (index_js_1.PHOTO_TYPES.includes(file.fileType)) {
writer.writeBigInt(BigInt(String(file.volumeId)));
if (file.version >= 4)
writer.writeInt(Number(file.thumbnailSource));
switch (file.thumbnailSource) {
case index_js_1.ThumbnailSource.LEGACY:
writer.writeBigInt(BigInt(String(file.secret))).writeInt(Number(file.localId));
case index_js_1.ThumbnailSource.THUMBNAIL:
writer
.writeInt(Number(file.thumbnailFileType))
.writeInt(String(file.thumbnailSize).charCodeAt(0))
.writeInt(Number(file.localId));
break;
case index_js_1.ThumbnailSource.CHAT_PHOTO_BIG:
case index_js_1.ThumbnailSource.CHAT_PHOTO_SMALL:
writer
.writeBigInt(BigInt(String(file.chatId)))
.writeBigInt(BigInt(String(file.chatAccessHash)))
.writeInt(Number(file.localId));
break;
case index_js_1.ThumbnailSource.STICKER_SET_THUMBNAIL:
writer
.writeBigInt(BigInt(String(file.stickerSetId)))
.writeBigInt(BigInt(String(file.stickerSetAccessHash)))
.writeInt(Number(file.localId));
break;
default:
throw new Error(`unknown encoder for thumbnailSource ${file.thumbnailSource}`);
}
}
else if (index_js_1.DOCUMENT_TYPES.includes(fileType)) {
writer.writeInt(file.version);
writer.writeInt(file.subVersion);
}
return (0, index_js_1.base64_url_encode)((0, index_js_1.rle_encode)(node_buffer_1.Buffer.concat([writer.results(), node_buffer_1.Buffer.from([file.subVersion, file.version])])));
}
static uniqueId(file) {
let writer;
switch (file.fileTypeUniqueId) {
case index_js_1.FileTypeUniqueId.WEB:
writer = new index_js_1.Writer();
writer.writeInt(Number(file.fileTypeUniqueId)).writeString(String(file.url));
break;
case index_js_1.FileTypeUniqueId.PHOTO:
writer = new index_js_1.Writer();
writer
.writeInt(Number(file.fileTypeUniqueId))
.writeBigInt(BigInt(String(file.volumeId)))
.writeInt(Number(file.localId));
break;
case index_js_1.FileTypeUniqueId.DOCUMENT:
writer = new index_js_1.Writer();
writer.writeInt(Number(file.fileTypeUniqueId)).writeBigInt(BigInt(String(file.id)));
break;
default:
throw new Error(`unknown encoder for fileTypeUniqueId ${file.fileTypeUniqueId}`);
}
return (0, index_js_1.base64_url_encode)((0, index_js_1.rle_encode)(writer.results()));
}
}
exports.Encode = Encode;
//# sourceMappingURL=encode.js.map