Merge branch 'MAILWEB-6067-improve-circular-dependencies-prevention' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / store / _shares / shareUrl.ts
blob717f393fbe4859e46169beb07c11936b910238c3
1 import { getAppHref } from '@proton/shared/lib/apps/helper';
2 import { APPS } from '@proton/shared/lib/constants';
3 import { SHARE_GENERATED_PASSWORD_LENGTH } from '@proton/shared/lib/drive/constants';
4 import { hasBit } from '@proton/shared/lib/helpers/bitset';
5 import { SharedURLFlags } from '@proton/shared/lib/interfaces/drive/sharing';
7 import { replaceLocalURL } from '../../utils/replaceLocalURL';
9 export const isLegacySharedUrl = (sharedURL?: { flags?: number }) =>
10     !!sharedURL && (sharedURL.flags === SharedURLFlags.CustomPassword || sharedURL.flags === SharedURLFlags.Legacy);
12 export const hasCustomPassword = (sharedURL?: { flags?: number }): boolean => {
13     return !!sharedURL && hasBit(sharedURL.flags, SharedURLFlags.CustomPassword);
16 export const hasGeneratedPasswordIncluded = (sharedURL?: { flags?: number }): boolean => {
17     return !!sharedURL && hasBit(sharedURL.flags, SharedURLFlags.GeneratedPasswordIncluded);
20 export const splitGeneratedAndCustomPassword = (password: string, sharedURL?: { flags?: number }): [string, string] => {
21     if (hasCustomPassword(sharedURL)) {
22         if (hasGeneratedPasswordIncluded(sharedURL)) {
23             return [
24                 password.substring(0, SHARE_GENERATED_PASSWORD_LENGTH),
25                 password.substring(SHARE_GENERATED_PASSWORD_LENGTH),
26             ];
27         }
28         // This is legacy custom password mode; new shares should not create it.
29         return ['', password];
30     }
31     return [password, ''];
34 export const getSharedLink = (sharedURL?: {
35     token: string;
36     password: string;
37     publicUrl?: string;
38     flags?: number;
39 }): string | undefined => {
40     if (!sharedURL) {
41         return undefined;
42     }
44     const [generatedPassword] = splitGeneratedAndCustomPassword(sharedURL.password, sharedURL);
46     const url = sharedURL.publicUrl
47         ? replaceLocalURL(sharedURL.publicUrl)
48         : getAppHref(`/urls/${sharedURL.token}`, APPS.PROTONDRIVE);
49     return `${url}${generatedPassword !== '' ? `#${generatedPassword}` : ''}`;