1 import type { SHARE_MEMBER_PERMISSIONS } from '@proton/shared/lib/drive/permissions';
2 import mergeUint8Arrays from '@proton/utils/mergeUint8Arrays';
4 import { useLinkPath } from '../../store';
5 import { useDownload } from '../../store/_downloads';
6 import { useLink, validateLinkName } from '../../store/_links';
7 import { useDirectSharingInfo } from '../../store/_shares/useDirectSharingInfo';
8 import useUploadHelper from '../../store/_uploads/UploadProvider/useUploadHelper';
9 import { useAbortSignal } from '../../store/_views/utils';
10 import { ValidationError } from '../../utils/errorHandling/ValidationError';
11 import { streamToBuffer } from '../../utils/stream';
12 import type { LegacyNodeMeta } from '../interface';
13 import type { DecryptedNode } from './interface';
14 import { decryptedLinkToNode } from './utils';
16 export const useNode = () => {
17 const { getLink } = useLink();
18 const { getPath } = useLinkPath();
19 const { getSharePermissions } = useDirectSharingInfo();
20 const { downloadStream } = useDownload();
21 const { findAvailableName } = useUploadHelper();
22 const abortSignal = useAbortSignal([]);
24 const getNode = async ({ shareId, linkId, volumeId }: LegacyNodeMeta): Promise<DecryptedNode> => {
25 const link = await getLink(abortSignal, shareId, linkId);
27 return decryptedLinkToNode(link, volumeId);
30 const getNodeContents = async ({
34 }: LegacyNodeMeta): Promise<{
38 const link = await getLink(abortSignal, shareId, linkId);
40 const { stream, controls } = downloadStream({
44 const cancelListener = () => {
47 abortSignal.addEventListener('abort', cancelListener);
48 const buffer = await streamToBuffer(stream);
49 abortSignal.removeEventListener('abort', cancelListener);
52 contents: mergeUint8Arrays(buffer),
53 node: decryptedLinkToNode(link, volumeId),
57 const getNodePermissions = async ({ shareId }: LegacyNodeMeta): Promise<SHARE_MEMBER_PERMISSIONS> => {
58 return getSharePermissions(abortSignal, shareId);
61 const getNodePath = async ({ shareId, linkId }: LegacyNodeMeta): Promise<string> => {
62 return getPath(abortSignal, shareId, linkId);
65 const findAvailableNodeName = async (
66 { shareId, linkId: parentLinkId }: LegacyNodeMeta,
68 ): Promise<string> => {
69 const error = validateLinkName(filename);
72 throw new ValidationError(error);
75 const name = await findAvailableName(abortSignal, { shareId, parentLinkId, filename });
85 findAvailableNodeName,
89 export default useNode;