Skip to content

Commit

Permalink
Use DownloadManager
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed May 12, 2024
1 parent bdb0694 commit 4dcbbb6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
12 changes: 12 additions & 0 deletions client_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
import { StorageDenoKV } from "mtkruto/storage/1_storage_deno_kv.ts";
import { transportProviderTcp } from "mtkruto/transport/3_transport_provider_tcp.ts";

import { DownloadManager } from "./download_manager.ts";

export interface ClientStats {
connected: boolean;
me: User;
Expand Down Expand Up @@ -87,6 +89,16 @@ export class ClientManager {
};
}

#downloadManagers = new Map<Client, DownloadManager>();
async download(id: string, fileId: string) {
const client = await this.getClient(id);
let downloadManager = this.#downloadManagers.get(client);
if (!downloadManager) {
downloadManager = new DownloadManager(client);
}
return downloadManager.download(fileId);
}

async getClient(id: string) {
{
const client = this.#clients.get(id);
Expand Down
1 change: 1 addition & 0 deletions download_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "std/path/mod.ts";
import { exists, existsSync } from "std/fs/mod.ts";

import { Client } from "mtkruto/mod.ts";
import { Queue } from "mtkruto/1_utilities.ts";

Expand Down
4 changes: 3 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ async function handleMethod(
if (chunk == null) {
controller.close();
} else {
controller.enqueue(chunk.value);
if (chunk.value) {
controller.enqueue(chunk.value);
}
if (chunk.done) {
controller.close();
}
Expand Down
13 changes: 9 additions & 4 deletions worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,15 @@ async function serve(
if (!(ALLOWED_METHODS.includes(method))) {
return "DROP";
}
const client = await clientManager.getClient(id);
// deno-lint-ignore ban-ts-comment
// @ts-ignore
const result = transform(await client[method](...args));
let result;
if (method == "download") {
result = await clientManager.download(id, args[0]);
} else {
const client = await clientManager.getClient(id);
// deno-lint-ignore ban-ts-comment
// @ts-ignore
result = transform(await client[method](...args));
}
if (result !== undefined) {
if (
typeof result === "object" && result != null &&
Expand Down

0 comments on commit 4dcbbb6

Please sign in to comment.