Skip to content

Commit

Permalink
report progress
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed Nov 22, 2023
1 parent 438d23b commit 49b45e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/api/client/message-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fs from 'fs';

import { config } from 'src/config';
import { TechnicalError } from 'src/errors/base';
import { db } from 'src/server/manager/db';

class MessageBroker {
protected readonly privateChannelId = config.telegram.private_file_channel;
Expand Down Expand Up @@ -138,6 +139,8 @@ export class MessageApi extends MessageBroker {
const fileSize = Number(message.document.size);
const chunkSize = config.tgfs.download.chunksize * 1024;

const task = db.createTask(file.name, 0, 'download');

const buffer = Buffer.alloc(fileSize);
let i = 0;
for await (const chunk of this.client.iterDownload({
Expand All @@ -151,7 +154,10 @@ export class MessageApi extends MessageBroker {
})) {
chunk.copy(buffer, i * chunkSize, 0, Number(chunk.length));
i += 1;
task.reportProgress(i * chunkSize);
}

task.finish();
return buffer;
}
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const { argv }: any = yargs(hideBin(process.argv))

const startServer = (
name: string,
app: any,
app: (req: any, res: any, next: any) => void,
host: string,
port: number,
path: string,
Expand Down Expand Up @@ -104,7 +104,10 @@ const { argv }: any = yargs(hideBin(process.argv))

startServer(
'Manager',
managerServer,
(req, res, next) => {
managerServer(req, res);
next();
},
config.manager.host,
config.manager.port,
config.manager.path,
Expand Down
9 changes: 8 additions & 1 deletion src/server/manager/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import { db } from './db';

const app = express();

// set cors headers
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});

app.get('/tasks', (req, res) => {
return db.getTasks();
console.log(db.getTasks());
res.send(db.getTasks());
});

export const managerServer = app;

0 comments on commit 49b45e1

Please sign in to comment.