Files
wehub-resource-sync 2114b14ee0
Sync main into demo / sync (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:26 +08:00

71 lines
2.1 KiB
TypeScript

import type { FSNode } from '@/os/types';
import * as FileSystem from '@/os/FileSystemService';
export type TransferOperation = 'copy' | 'move';
export async function transferNodesToDirectory(
nodes: FSNode[],
destPath: string,
operation: TransferOperation,
): Promise<boolean> {
if (nodes.length === 0) return false;
try {
for (const node of nodes) {
const destNodePath = `${destPath}/${node.name}`;
if (operation === 'move') {
const moved = await FileSystem.moveNode(node.path, destNodePath);
if (!moved) return false;
} else {
const copied = await copyNodeToPath(node, destNodePath);
if (!copied) return false;
}
}
return true;
} catch (error) {
console.error('[FileManager] Transfer failed:', error);
return false;
}
}
/**
* Fire ACTION_SEND for image files in selection. Returns true if at least one
* image was found and the intent was dispatched.
*
* 用 startActivity + newTask 走 fire-and-forget 语义(接收方如微信声明 launchMode='singleTask'
* 时会留在自己 Task)。FileManager 不需要回执,与真机分享行为一致。
*/
export function shareNodesAsImages(nodes: FSNode[]): boolean {
const imagePaths = nodes
.filter((n) => n.type === 'file' && n.mimeType?.startsWith('image/'))
.map((n) => n.path);
if (imagePaths.length === 0) return false;
window.__OS__?.startActivity?.(
{
action: 'ACTION_SEND',
type: 'image/*',
data: {
stream: imagePaths.length === 1 ? imagePaths[0] : imagePaths,
mimeType: 'image/jpeg',
},
},
{ newTask: true },
);
return true;
}
async function copyNodeToPath(node: FSNode, destPath: string): Promise<boolean> {
if (node.type === 'file') {
const copied = await FileSystem.copyFile(node.path, destPath);
return Boolean(copied);
}
await FileSystem.createDirectory(destPath);
const children = FileSystem.listDirectory(node.path);
for (const child of children) {
const copied = await copyNodeToPath(child, `${destPath}/${child.name}`);
if (!copied) return false;
}
return true;
}