File size: 6,388 Bytes
35ee763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import streamSaver from 'streamsaver'
import { Api } from 'telegram'
import { telegramClient } from './Telegram'
import { concat } from 'concat-stream'
import FastPriorityQueue from 'fastpriorityqueue'
class ConnectionPool {
  private connections: Promise<any>[]
  public maxSize: number
  constructor(maxSize: number) {
    this.connections = []
    this.maxSize = maxSize
  }
  async getConnection() {
    if (this.connections.length > 0) {
      return this.connections.shift()
    }
    if (this.connections.length < this.maxSize) {
      const connection = telegramClient.connect()
      this.connections.push(connection)
      return connection
    }
    return new Promise((resolve) => {
      const interval = setInterval(() => {
        if (this.connections.length < this.maxSize) {
          const connection = telegramClient.connect()
          this.connections.push(connection)
          clearInterval(interval)
          resolve(connection)
        }
      }, 1000)
    })
  }
  releaseConnection(connection: Promise<any>) {
    this.connections.push(connection)
  }
}
// Declare the proper type for file iterators
type FileIterator = {
  [Symbol.asyncIterator]: () => AsyncGenerator<Uint8Array, void, unknown>
}
const connectionPool = new ConnectionPool(5) // set maximum pool size to 5
const cache = new Map<string, Uint8Array>() // create a cache for downloaded data
async function* generateChunks(
  clients: any[],
  media: any,
  i: number,
  numParallel: number,
  bufferSize: number = 1024 * 1024 // set buffer size to 1 MB
): AsyncGenerator<Uint8Array, void, unknown> {
  const numConnections = clients.length
  let connIndex = i % numConnections
  let offset = Math.floor(i / numConnections) * media.size / numParallel
  const limit = media.size / numParallel
  const promises: Promise<Uint8Array[]>[] = []
  let buffer: Uint8Array[] = []
  let bufferLength = 0
  for (let j = 0; j < numParallel; j++) {
    const client = clients[connIndex]
    promises.push(
      client.downloadMedia(media, { offset, limit })
    )
    offset += limit
    connIndex = (connIndex + 1) % numConnections
  }
  const chunksArray = await Promise.allSettled(promises)
  for (const result of chunksArray) {
    if (result.status === 'fulfilled') {
      const chunks = result.value
      for (const chunk of chunks) {
        buffer.push(chunk)
        bufferLength += chunk.byteLength
        if (bufferLength >= bufferSize) {
          const concatenated = concat(buffer)
          yield concatenated
          buffer = []
          bufferLength = 0
        }
      }
    } else {
      console.error(result.reason)
    }
  }
  if (bufferLength > 0) {
    const concatenated = concat(buffer)
    yield concatenated
  }
}
export async function download(
  id: string,
  numParallel: number = 1
): Promise<ReadableStream<Uint8Array>> {
  const fileIterators: FileIterator[] = []
  const cachedData = cache.get(id)
  if (cachedData) {
    return new ReadableStream({
      start(controller) {
        controller.enqueue(cachedData)
        controller.close()
      }
    })
  }
  const clients: any[] = []
  for (let i = 0; i < numParallel; i++) {
    clients.push(await connectionPool.getConnection())
  }
  try {
    const { data: response } = await clients[0].invoke(
      new Api.messages.GetMessages({
        id: [new Api.InputMessageID({ id: Number(id) })]
      })
    )
    const media = response.messages[0].media
    for (let i = 0; i < numParallel; i++) {
      fileIterators.push({
        [Symbol.asyncIterator]: generateChunks.bind(
          null,
          clients,
          media,
          i,
          numParallel
        )
      })
    }
    const streams: ReadableStream<Uint8Array>[] = []
    for (const fileIterator of fileIterators) {
      const stream = new ReadableStream({
        async start(controller) {
          for await (const chunk of fileIterator) {
            controller.enqueue(chunk)
          }
          controller.close()
        }
      })
      streams.push(stream)
    }
    return mergeStreams(...streams)
  } finally {
    for (const client of clients) {
      connectionPool.releaseConnection(client)
    }
  }
}
export const directDownload = async (
  id: string,
  name: string,
  numParallel: number = 1
): Promise<void> => {
  const fileStream = streamSaver.createWriteStream(name)
  const writer = fileStream.getWriter()
  try {
    const streams = await download(id, numParallel)
    // Combine the streams using a function like mergeStreams in place of [stream1, stream2, ..., streamN]
    const mergedStream = mergeStreams(streams)
    const reader = mergedStream.getReader()
    const pump = async () => {
      const { done, value } = await reader.read()
      if (done) {
        if (value) { // add null check here
          cache.set(id, value)
        }
        writer.close()
        return
      }
      writer.write(value)
      pump()
    }
    pump()
  } catch (error) {
    console.error(error)
  }
}

function mergeStreams(...streams: ReadableStream<Uint8Array>[]): ReadableStream<Uint8Array> {
  if (streams.length === 1) {
    return streams[0]
  }
  const mid = Math.floor(streams.length / 2)
  const left = mergeStreams(...streams.slice(0, mid))
  const right = mergeStreams(...streams.slice(mid))
  // Use FastPriorityQueue instead of an array and custom heapify/siftDown functions
  const heap = new FastPriorityQueue((a: any, b: any) => a[0] > b[0])
  // Initialize heap with the first chunk from each stream
  const leftReader = left.getReader()
  const rightReader = right.getReader()
  const read = async (reader: ReadableStreamDefaultReader<Uint8Array>) => {
    const { done, value } = await reader.read()
    if (!done && value !== undefined) {
      heap.add([value.byteLength, reader])
    }
  }
  read(leftReader)
  read(rightReader)
  const combinedStream = new ReadableStream({
    async start(controller) {
      while (!heap.isEmpty()) {
        const [_, reader] = heap.poll()
        const { done, value } = await reader.read()
        if (done) {
          if (!heap.isEmpty()) {
            const next = heap.poll()
            heap.add(next)
          }
        } else {
          if (value !== undefined) {
            controller.enqueue(value)
            heap.add([value.byteLength, reader])
          }
        }
      }
      controller.close()
    }
  })
  return combinedStream
}