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
import type { RendererTransferEvent } from '@altersend/core'
import type { SendDraftPhase } from './pageUi'
import type { BrowserFileLike, SelectedFile, SenderUploadItem } from './draftTypes'
export interface SharingStatusEvent extends Extract<RendererTransferEvent, { type: 'status' }> {
state: 'sharing'
file?: string
path?: string
}
export function getPhaseFromSelection(fileCount: number): SendDraftPhase {
return fileCount > 0 ? 'selected' : 'empty'
}
export function mergeSelectedFiles(
current: SelectedFile[],
nextSelectedFiles: SelectedFile[]
): SelectedFile[] {
const merged = new Map(current.map((file) => [file.path, file]))
for (const file of nextSelectedFiles) merged.set(file.path, file)
return Array.from(merged.values())
}
export function fileUriToPath(uri: string): string {
if (!uri.startsWith('file://')) return uri
const stripped = uri.slice('file://'.length)
try {
return decodeURIComponent(stripped)
} catch {
return stripped
}
}
export interface ExternalFileDescriptor {
path: string
name?: string | null
size?: number | null
relativePath?: string
}
export function toSelectedFiles(files: ExternalFileDescriptor[]): SelectedFile[] {
return files.map((file) => {
const filePath = fileUriToPath(file.path)
return {
name: file.name ?? filePath.split(/[\\/]/).pop() ?? 'file',
path: filePath,
size: file.size ?? undefined,
relativePath: file.relativePath
}
})
}
export function normalizeSelectedFiles(
filesOrData: Array<File | BrowserFileLike>,
getPathForFile: (file: File) => string
): SelectedFile[] {
return filesOrData.flatMap((fileOrData): SelectedFile[] => {
if ('path' in fileOrData && fileOrData.path) {
return [
{
name: fileOrData.name,
path: fileOrData.path,
size: fileOrData.size,
relativePath: fileOrData.relativePath
}
]
}
if (fileOrData instanceof File) {
const filePath = getPathForFile(fileOrData)
if (!filePath) return []
return [{ name: fileOrData.name, path: filePath, size: fileOrData.size }]
}
return []
})
}
export interface SelectedFileRow {
kind: 'file'
file: SelectedFile
}
export interface SelectedFolderRow {
kind: 'folder'
name: string
files: SelectedFile[]
totalSize: number
}
export type SelectedRow = SelectedFileRow | SelectedFolderRow
export function groupSelectedFiles(files: SelectedFile[]): SelectedRow[] {
const rows: SelectedRow[] = []
const folderRowIndex = new Map<string, number>()
for (const file of files) {
const relativePath = (file.relativePath ?? '').replace(/\\/g, '/')
const segments = relativePath.split('/').filter(Boolean)
if (segments.length <= 1) {
rows.push({ kind: 'file', file })
continue
}
const name = segments[0]
const normalizedPath = file.path.replace(/\\/g, '/')
const key = normalizedPath.endsWith(relativePath)
? normalizedPath.slice(0, normalizedPath.length - relativePath.length) + name
: name
const existing = folderRowIndex.get(key)
if (existing === undefined) {
folderRowIndex.set(key, rows.length)
rows.push({ kind: 'folder', name, files: [file], totalSize: file.size ?? 0 })
} else {
const row = rows[existing] as SelectedFolderRow
row.files.push(file)
row.totalSize += file.size ?? 0
}
}
return rows
}
export function createTextSnippet(content: string): SelectedFile {
const text = content.trim()
const id = `text-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
return {
name: formatTextSnippetPreview(text, 40),
path: id,
kind: 'text',
content: text,
isTemporary: true,
size: text.length
}
}
export function formatTextSnippetPreview(content: string, maxLength = 72): string {
const collapsed = content.replace(/\s+/g, ' ').trim()
return collapsed.length > maxLength ? `${collapsed.slice(0, maxLength).trimEnd()}…` : collapsed
}
export function createInitialUploadItems(selectedFiles: SelectedFile[]): SenderUploadItem[] {
return selectedFiles.map((file) => ({
...file,
status: 'waiting' as const
}))
}
export function applySharingProgress(
current: SenderUploadItem[],
message: SharingStatusEvent
): SenderUploadItem[] {
if (current.length === 0) return current
const activePath = typeof message.path === 'string' ? message.path : undefined
const targetIndex = activePath
? current.findIndex((item) => item.path === activePath)
: current.findIndex((item) => item.name === message.file && item.status !== 'completed')
if (targetIndex === -1) return current
return current.map((item, index) => {
if (index < targetIndex) {
return item.status === 'completed' ? item : { ...item, status: 'completed' }
}
if (index === targetIndex) {
return item.status === 'uploading' ? item : { ...item, status: 'uploading' }
}
return item.status === 'completed' ? item : { ...item, status: 'waiting' }
})
}