Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #301 from mgilangjanuar/staging
Browse files Browse the repository at this point in the history
Release v2.0.6
  • Loading branch information
mgilangjanuar authored Apr 1, 2022
2 parents 4aa93a7 + f925349 commit 9ae5017
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "teledrive",
"version": "2.0.5",
"version": "2.0.6",
"repository": "[email protected]:mgilangjanuar/teledrive.git",
"author": "M Gilang Januar <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "server",
"version": "2.0.5",
"version": "2.0.6",
"main": "dist/index.js",
"license": "MIT",
"private": true,
Expand Down
67 changes: 30 additions & 37 deletions server/src/api/v1/Files.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Api, Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import bigInt from 'big-integer'
import contentDisposition from 'content-disposition'
import { AES, enc } from 'crypto-js'
import { Request, Response } from 'express'
import multer from 'multer'
import moment from 'moment'
import multer from 'multer'
import { Api, Logger, TelegramClient } from 'teledrive-client'
import { LogLevel } from 'teledrive-client/extensions/Logger'
import { StringSession } from 'teledrive-client/sessions'
import { Files as Model } from '../../model/entities/Files'
import { Usages } from '../../model/entities/Usages'
import { Redis } from '../../service/Cache'
Expand Down Expand Up @@ -814,8 +814,8 @@ export class Files {
let cancel = false
req.on('close', () => cancel = true)
// res.status(206)
res.setHeader('Cache-Control', 'public, max-age=604800')
res.setHeader('ETag', Buffer.from(`${files[0].id}:${files[0].message_id}`).toString('base64'))
// res.setHeader('Cache-Control', 'public, max-age=604800')
// res.setHeader('ETag', Buffer.from(`${files[0].id}:${files[0].message_id}`).toString('base64'))
res.setHeader('Content-Range', `bytes */${totalFileSize}`)
res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Disposition', contentDisposition(files[0].name.replace(/\.part\d+$/gi, ''), { type: Number(dl) === 1 ? 'attachment' : 'inline' }))
Expand Down Expand Up @@ -844,42 +844,35 @@ export class Files {
}))
}

let data = null

const chunk = 64 * 1024
let idx = 0

while (!cancel && data === null || data.length && bigInt(file.size).greater(bigInt(idx * chunk))) {
// const startDate = Date.now()
const getData = async () => await req.tg.downloadMedia(chat['messages'][0].media, {
...thumb ? { sizeType: 'i' } : {},
start: idx++ * chunk,
end: bigInt.min(bigInt(file.size), bigInt(idx * chunk - 1)).toJSNumber(),
workers: 1, // using 1 for stable
progressCallback: (() => {
const updateProgess: any = () => {
updateProgess.isCanceled = cancel
const getData = async () => await req.tg.downloadMedia(chat['messages'][0].media, {
...thumb ? { thumb: 0 } : {},
outputFile: {
write: (buffer: Buffer) => {
if (cancel) {
throw { status: 422, body: { error: 'canceled' } }
} else {
res.write(buffer)
res.flush()
}
return updateProgess
})()
} as any)
},
close: res.end
}
})

let trial = 0
while (trial < PROCESS_RETRY) {
try {
data = await getData()
res.write(data)
trial = PROCESS_RETRY
} catch (error) {
if (trial >= PROCESS_RETRY) {
throw error
}
await new Promise(resolve => setTimeout(resolve, ++trial * 3000))
await req.tg?.connect()
let trial = 0
while (trial < PROCESS_RETRY) {
try {
await getData()
trial = PROCESS_RETRY
} catch (error) {
if (trial >= PROCESS_RETRY) {
throw error
}
await new Promise(resolve => setTimeout(resolve, ++trial * 3000))
await req.tg?.connect()
}
res.flush()
}

}
usage.usage = bigInt(totalFileSize).add(bigInt(usage.usage)).toString()
await usage.save()
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "2.0.5",
"version": "2.0.6",
"private": true,
"dependencies": {
"@ideasio/add-to-homescreen-react": "^1.0.10",
Expand Down
2 changes: 1 addition & 1 deletion web/src/utils/Constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = '2.0.5'
export const VERSION = '2.0.6'
export const MAX_UPLOAD_SIZE = 2_000_000_000
export const CHUNK_SIZE = 512 * 1024
export const RETRY_COUNT = 50
39 changes: 15 additions & 24 deletions web/src/utils/Download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Api } from 'teledrive-client'
import bigInt from 'big-integer'
import streamSaver from 'streamsaver'
import { Api } from 'teledrive-client'
import { req } from './Fetcher'
import { telegramClient } from './Telegram'

Expand All @@ -13,6 +12,7 @@ export async function download(id: string): Promise<ReadableStream> {
start(_controller: ReadableStreamDefaultController) {
},
async pull(controller: ReadableStreamDefaultController) {
console.log('start downloading:', response.files)
for (const file of response.files) {
let chat: any
if (file.forward_info && file.forward_info.match(/^channel\//gi)) {
Expand All @@ -33,29 +33,20 @@ export async function download(id: string): Promise<ReadableStream> {
}))
}

let data: any = null

const chunk = 64 * 1024
let idx = 0

while (!cancel && data === null || data.length && bigInt(file.size).greater(bigInt(idx * chunk))) {
// const startDate = Date.now()
const getData = async () => await client.downloadMedia(chat['messages'][0].media, {
start: idx++ * chunk,
end: bigInt.min(bigInt(file.size), bigInt(idx * chunk - 1)).toJSNumber(),
workers: 1, // using 1 for stable
progressCallback: (() => {
const updateProgess: any = () => {
updateProgess.isCanceled = cancel
}
return updateProgess
})()
} as any)
data = await getData()
controller.enqueue(data)
}
const getData = async () => await client.downloadMedia(chat['messages'][0].media, {
outputFile: {
write: (chunk: Buffer) => {
if (cancel) return false
return controller.enqueue(chunk)
},
close: () => controller.close()
},
progressCallback: (received, total) => {
console.log('progress:', received, '/', total)
}
})
await getData()
}
controller.close()
},
cancel() {
cancel = true
Expand Down

0 comments on commit 9ae5017

Please sign in to comment.