Open dieptang opened 1 year ago
here is a function i found in some project to download file
/**
*
* @param {'photo' | 'document'} type
* @param {string} id
* @param {string} accessHash
* @param {Array<Number>} fileReference
* @param {string?} photoSizeID
* @returns {string}
*/
async function downloadFile(type, id, accessHash, fileReference, photoSizeID) {
const partSize = 524288 * 2
/**
* @type {Number|undefined}
*/
let dcId = 4
/**
*
* @param {Number} offset
* @returns
*/
const downloadPart = async (offset) => {
const body = {
location: type === 'photo'
? { _: 'inputPhotoFileLocation', id, access_hash: accessHash, file_reference: fileReference, thumb_size: photoSizeID }
: { _: 'inputDocumentFileLocation', id, access_hash: accessHash, file_reference: fileReference },
offset: offset,
limit: partSize
}
console.log(body, dcId)
const file = await mtproto.call('upload.getFile', body, dcId && { dcId })
return file
}
let partBytesLength
let iter = 0
const fileChunks = []
while (partBytesLength === undefined || partBytesLength === partSize) {
console.log('Downloading part of file', iter, iter * partSize)
let file
try {
file = await downloadPart(iter * partSize)
} catch (e) {
if (e._ === 'mt_rpc_error' && e.error_message.startsWith('FILE_MIGRATE_')) {
const _dcId = Number(e.error_message.substring('FILE_MIGRATE_'.length))
dcId = _dcId
continue
} else {
throw e
}
}
partBytesLength = file.bytes.length
console.log('Downloaded part of file', iter, iter * partSize, partBytesLength)
fileChunks.push(file.bytes)
iter++
}
const fileChunksBuffers = fileChunks.map(chunk => Buffer.from(chunk))
const fileBuffer = Buffer.concat(fileChunksBuffers)
return fileBuffer
}
I am using it like this
// for image
const type = 'photo'
downloadFile(
type,
chatFile.media.photo.id,
chatFile.media.photo.access_hash,
chatFile.media.photo.file_reference,
'm'
).then(file => {
const base64File = `data:image/png;base64,` + file.toString('base64')
console.log(base64File)
}).catch(err => {
console.log(err)
})
I want to download also the photo from getHistory. But seem upload.getFile did not help.
let history = await telegram('messages.getHistory', { peer: { _: 'inputPeerChannel', channel_id: chat.id, access_hash: chat.access_hash },, limit: 10 })
And I want to get the photo from media object. Something like this
The error has appeared, due to missing file_reference. Any ideas?