mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Add file transfer support (#83)
Channel-scoped file browser reachable via right-click on a channel: navigate directories, create folders, rename/delete, and upload/download files. Backed by tsclientlib's built-in download_file/upload_file (which already handles the ftinitdownload/ftinitupload -> TCP data-port dance), plus raw ftgetfilelist/ftcreatedir/ftdeletefile/ftrenamefile commands for everything else. Downloads/uploads are buffered whole in memory and base64-relayed over the existing stdin/WebSocket text protocol - fine for a channel filebase's typical file sizes, not meant for huge transfers. Verified against the real server: upload/list/download round-trip reaches the server correctly (list succeeds, upload correctly surfaces the guest account's missing permission via the new FiletransferFailed handling instead of hanging or crashing). Demo mode gets a small in-memory per-channel filesystem so the file browser is fully interactive in the GitHub Pages demo too.
This commit is contained in:
1 parent
41ac845fbc
commit
7901aafbb1
7 files changed
+862
-4
No files matched your search
@@ -222,6 +222,30 @@ wss.on("connection", (socket: WebSocket) => {
|
||||
await connection?.getPermissionOverview();
|
||||
break;
|
||||
}
|
||||
case "getFileList": {
|
||||
await connection?.getFileList(msg.channelId, msg.path);
|
||||
break;
|
||||
}
|
||||
case "createDirectory": {
|
||||
await connection?.createDirectory(msg.channelId, msg.dirname);
|
||||
break;
|
||||
}
|
||||
case "deleteFile": {
|
||||
await connection?.deleteFile(msg.channelId, msg.name);
|
||||
break;
|
||||
}
|
||||
case "renameFile": {
|
||||
await connection?.renameFile(msg.channelId, msg.oldName, msg.newName);
|
||||
break;
|
||||
}
|
||||
case "downloadFile": {
|
||||
await connection?.downloadFile(msg.channelId, msg.path);
|
||||
break;
|
||||
}
|
||||
case "uploadFile": {
|
||||
await connection?.uploadFile(msg.channelId, msg.path, msg.dataBase64);
|
||||
break;
|
||||
}
|
||||
case "disconnect": {
|
||||
await connection?.disconnect(msg.message ?? "");
|
||||
break;
|
||||
|
||||
@@ -81,6 +81,14 @@ export interface OfflineMessageListEntry {
|
||||
isRead: boolean;
|
||||
}
|
||||
|
||||
export interface FileListEntry {
|
||||
path: string;
|
||||
name: string;
|
||||
size: number;
|
||||
isFile: boolean;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export type ServerLogEntry =
|
||||
| { kind: "clientJoin"; client: string; channel: string }
|
||||
| { kind: "clientLeave"; client: string }
|
||||
@@ -151,7 +159,10 @@ export type Ts3ConnectionEvent =
|
||||
}
|
||||
| { type: "channelGroupList"; entries: GroupEntry[] }
|
||||
| { type: "serverGroupList"; entries: GroupEntry[] }
|
||||
| { type: "permissionOverview"; entries: PermissionOverviewEntry[] };
|
||||
| { type: "permissionOverview"; entries: PermissionOverviewEntry[] }
|
||||
| { type: "fileList"; cid: number; path: string; entries: FileListEntry[] }
|
||||
| { type: "fileDownloadData"; cid: number; path: string; data: string }
|
||||
| { type: "fileUploadDone"; cid: number; path: string };
|
||||
|
||||
export interface Ts3ConnectOptions {
|
||||
host: string;
|
||||
@@ -320,7 +331,10 @@ export class Ts3Connection {
|
||||
| {
|
||||
type: "permissionOverview";
|
||||
entries: { name: string; description: string; value: number; negated: boolean; skip: boolean }[];
|
||||
};
|
||||
}
|
||||
| { type: "fileList"; cid: number; path: string; entries: FileListEntry[] }
|
||||
| { type: "fileDownloadData"; cid: number; path: string; data: string }
|
||||
| { type: "fileUploadDone"; cid: number; path: string };
|
||||
|
||||
if (event.type === "connected") {
|
||||
this.emit({
|
||||
@@ -596,6 +610,38 @@ export class Ts3Connection {
|
||||
this.child?.stdin.write(`permoverview\n`);
|
||||
}
|
||||
|
||||
async getFileList(channelId: number, path: string): Promise<void> {
|
||||
const sanitized = (path || "/").replace(/[\r\n]+/g, " ").trim() || "/";
|
||||
this.child?.stdin.write(`ftlist ${channelId} ${sanitized}\n`);
|
||||
}
|
||||
|
||||
async createDirectory(channelId: number, dirname: string): Promise<void> {
|
||||
const sanitized = dirname.replace(/[\r\n]+/g, " ").trim();
|
||||
if (sanitized) this.child?.stdin.write(`ftmkdir ${channelId} ${sanitized}\n`);
|
||||
}
|
||||
|
||||
async deleteFile(channelId: number, name: string): Promise<void> {
|
||||
const sanitized = name.replace(/[\r\n]+/g, " ").trim();
|
||||
if (sanitized) this.child?.stdin.write(`ftdelete ${channelId} ${sanitized}\n`);
|
||||
}
|
||||
|
||||
async renameFile(channelId: number, oldName: string, newName: string): Promise<void> {
|
||||
const sanitize = (s: string) => s.replace(/[\r\n\t]+/g, " ").trim();
|
||||
this.child?.stdin.write(`ftrename ${channelId}\t${sanitize(oldName)}\t${sanitize(newName)}\n`);
|
||||
}
|
||||
|
||||
async downloadFile(channelId: number, path: string): Promise<void> {
|
||||
const sanitized = path.replace(/[\r\n]+/g, " ").trim();
|
||||
if (sanitized) this.child?.stdin.write(`ftdownload ${channelId} ${sanitized}\n`);
|
||||
}
|
||||
|
||||
/** `dataBase64` is the raw file content, base64-encoded - the browser reads
|
||||
* the picked File as a data URL/ArrayBuffer and sends it up already encoded. */
|
||||
async uploadFile(channelId: number, path: string, dataBase64: string): Promise<void> {
|
||||
const sanitizedPath = path.replace(/[\r\n\t]+/g, " ").trim();
|
||||
this.child?.stdin.write(`ftupload ${channelId}\t${sanitizedPath}\t${dataBase64}\n`);
|
||||
}
|
||||
|
||||
async sendChatMessage(message: string): Promise<void> {
|
||||
const sanitized = message.replace(/[\r\n]+/g, " ").trim();
|
||||
if (sanitized) this.child?.stdin.write(`chat ${sanitized}\n`);
|
||||
|
||||
Reference in new issue
Block a user