Send files directly between devices over the internet - no cloud storage, no size limits.
TypeScript cross-platformdata-transferdecentralizedfile-sharingfile-transferhole-punchingopen-sourcep2ppeer-to-peerprivacyprivacy-tools
keep original file name through transfer
Denis Lupookov04cccd818c61
diff --git a/packages/core/src/worklet/rpc/protocol.ts b/packages/core/src/worklet/rpc/protocol.tsindex abc572d..b963292 100644--- a/packages/core/src/worklet/rpc/protocol.ts+++ b/packages/core/src/worklet/rpc/protocol.ts@@ -45,6 +45,7 @@ export interface JoinReply {export interface ShareFileRequest {path: string+ name?: stringrelativePath?: stringisTemporary?: booleankind?: 'file' | 'text'diff --git a/packages/core/src/worklet/transfer/receiver.ts b/packages/core/src/worklet/transfer/receiver.tsindex c21be91..321fad5 100644--- a/packages/core/src/worklet/transfer/receiver.ts+++ b/packages/core/src/worklet/transfer/receiver.ts@@ -14,7 +14,8 @@ import {isSafeRelativePath,joinFilePath,onAbort,- toRelativePath+ toRelativePath,+ withDisplayName} from './utils'import { fileExists } from './fs-utils'import { LegacyHyperdriveDownloader } from './legacy/downloader'@@ -97,7 +98,8 @@ export class TransferReceiver {continue}- const targetPath = file.targetPath ?? joinFilePath(file.targetDir!, relativeTarget)+ const relativeSaveTarget = withDisplayName(relativeTarget, file.name)+ const targetPath = file.targetPath ?? joinFilePath(file.targetDir!, relativeSaveTarget)if (this.active.has(file.fileId)) {fail(targetPath, 'Already downloading this file')diff --git a/packages/core/src/worklet/transfer/sender.test.ts b/packages/core/src/worklet/transfer/sender.test.tsindex c5a1cbf..b0c92dc 100644--- a/packages/core/src/worklet/transfer/sender.test.ts+++ b/packages/core/src/worklet/transfer/sender.test.ts@@ -23,6 +23,7 @@ function untouchableDrive(): Hyperdrive {function scanned(name: string, size: number): ScannedFile {return {fileName: name,+ displayName: name,inputPath: `/src/${name}`,sourcePath: `/${name}`,sourceDrive: {} as Localdrive,@@ -58,6 +59,17 @@ describe('TransferSender.buildOffers', () => {expect(offers[0].id).not.toBe(offers[1].id)})++ it('sends the picker display name on the wire, not the on-disk cache file name', () => {+ const sender = new TransferSender(untouchableDrive())++ const [offer] = sender.buildOffers(+ [{ ...scanned('a1b2c3d4-e5f6-47a8-9b0c-1d2e3f4a5b6c.flac', 10), displayName: 'My Song.flac' }],+ 't1'+ )++ expect(offer.name).toBe('My Song.flac')+ })})describe('TransferSender.reset', () => {diff --git a/packages/core/src/worklet/transfer/sender.ts b/packages/core/src/worklet/transfer/sender.tsindex 3870067..27c83cb 100644--- a/packages/core/src/worklet/transfer/sender.ts+++ b/packages/core/src/worklet/transfer/sender.ts@@ -2,12 +2,13 @@ import crypto from 'hypercore-crypto'import fs from 'bare-fs'import Localdrive from 'localdrive'import type Hyperdrive from 'hyperdrive'-import { getDirname, getFileName, toRelativePath } from './utils'+import { getDirname, getFileName, toRelativePath, toSafeFileName } from './utils'import { LegacyPeerStaging } from './legacy/staging'import type { FileOffer } from './control-channel'export interface ScannedFile {fileName: string+ displayName: stringinputPath: stringsourcePath: stringsourceDrive: Localdrive@@ -64,7 +65,7 @@ export class TransferSender {}async scanFiles(- requests: { path: string; relativePath?: string; isTemporary?: boolean }[]+ requests: { path: string; name?: string; relativePath?: string; isTemporary?: boolean }[]): Promise<ScanResult> {const files: ScannedFile[] = []const errors: string[] = []@@ -75,6 +76,7 @@ export class TransferSender {for (const req of requests) {const path = req.pathconst fileName = getFileName(path)+ const displayName = req.name ? toSafeFileName(req.name, fileName) : fileNameconst { root, relativePath } = resolveSource(path, req.relativePath, fileName)const sourcePath = `/${relativePath}`let sourceDrive = driveByDir.get(root)@@ -93,6 +95,7 @@ export class TransferSender {totalBytes += sizefiles.push({fileName,+ displayName,inputPath: path,sourcePath,sourceDrive,@@ -110,6 +113,7 @@ export class TransferSender {totalBytes += sizefiles.push({fileName,+ displayName,inputPath: path,sourcePath,sourceDrive,@@ -143,7 +147,7 @@ export class TransferSender {return {id,transferId,- name: file.fileName,+ name: file.displayName,path: file.sourcePath,size: file.size,driveKey: this.driveKey,diff --git a/packages/core/src/worklet/transfer/utils.ts b/packages/core/src/worklet/transfer/utils.tsindex 92d6215..8e9b69e 100644--- a/packages/core/src/worklet/transfer/utils.ts+++ b/packages/core/src/worklet/transfer/utils.ts@@ -11,6 +11,12 @@ export function getFileName(filePath: string) {return normalizedPath.split('/').pop() || normalizedPath}+export function withDisplayName(relativePath: string, name: string | undefined) {+ if (!name) return relativePath+ const dir = getDirname(relativePath)+ return dir === '.' ? name : joinFilePath(dir, name)+}+export function joinFilePath(dirPath: string, fileName: string) {if (!dirPath) return fileName@@ -20,6 +26,11 @@ export function joinFilePath(dirPath: string, fileName: string) {return `${dirPath.replace(trailingSeparators, '')}${separator}${fileName}`}+export function toSafeFileName(name: string, fallback: string): string {+ const sanitized = name.replace(/[\\/\0]/g, '_').trim().slice(0, 255)+ return sanitized.length > 0 && sanitized !== '.' && sanitized !== '..' ? sanitized : fallback+}+export function isSafeFileName(name: unknown): name is string {if (typeof name !== 'string') return falseif (name.length === 0 || name.length > 255) return falsediff --git a/packages/domain/src/transfer/commands.ts b/packages/domain/src/transfer/commands.tsindex 67945bd..ec243ab 100644--- a/packages/domain/src/transfer/commands.ts+++ b/packages/domain/src/transfer/commands.ts@@ -284,6 +284,7 @@ export const continueShare = async (files: SelectedFile[]): Promise<void> => {const fileRequests: ShareFileRequest[] = files.map((file) => ({path: file.path,+ name: file.name,relativePath: file.relativePath,isTemporary: file.isTemporary,kind: file.kind,