Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added `getSharingLink` function to generate a sharing link for one or more files. This function already exists in different libraries like `cozy-sharing` or `cozy-mespapiers-lib`, and soon in `cozy-ui`. So I think it's good to centralize it in `cozy-client`.
- Loading branch information
Showing
9 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[cozy-client](../README.md) / [models](models.md) / sharing | ||
|
||
# Namespace: sharing | ||
|
||
[models](models.md).sharing | ||
|
||
## Functions | ||
|
||
### getSharingLink | ||
|
||
▸ **getSharingLink**(`client`, `filesIds`, `isFlatDomain`): `Promise`<`string`> | ||
|
||
Generate Sharing link for one or many files | ||
|
||
*Parameters* | ||
|
||
| Name | Type | Description | | ||
| :------ | :------ | :------ | | ||
| `client` | [`CozyClient`](../classes/CozyClient.md) | Instance of CozyClient | | ||
| `filesIds` | `string`\[] | Array of io.cozy.files ids | | ||
| `isFlatDomain` | `boolean` | - | | ||
|
||
*Returns* | ||
|
||
`Promise`<`string`> | ||
|
||
Shared link | ||
|
||
*Defined in* | ||
|
||
[packages/cozy-client/src/models/sharing.js:13](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/sharing.js#L13) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const REGISTRATION_ABORT = 'REGISTRATION_ABORT' | ||
|
||
export const DOCTYPE_FILES = 'io.cozy.files' | ||
export const DOCTYPE_PERMISSIONS = 'io.cozy.permissions' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { DOCTYPE_FILES, DOCTYPE_PERMISSIONS } from '../const' | ||
import CozyClient from '../CozyClient' | ||
import { generateWebLink } from '../helpers' | ||
|
||
/** | ||
* Generate Sharing link for one or many files | ||
* | ||
* @param {CozyClient} client - Instance of CozyClient | ||
* @param {string[]} filesIds - Array of io.cozy.files ids | ||
* @param {boolean} [isFlatDomain] - | ||
* @returns {Promise<string>} Shared link | ||
*/ | ||
export const getSharingLink = async (client, filesIds, isFlatDomain) => { | ||
const PERMS = { | ||
_type: DOCTYPE_PERMISSIONS, | ||
permissions: { | ||
files: { type: DOCTYPE_FILES, values: filesIds, verbs: ['GET'] } | ||
} | ||
} | ||
const { data: sharedLink } = await client.save(PERMS) | ||
|
||
const webLink = generateWebLink({ | ||
cozyUrl: client.getStackClient().uri, | ||
searchParams: [['sharecode', sharedLink?.attributes?.shortcodes?.code]], | ||
pathname: '/public', | ||
slug: 'drive', | ||
subDomainType: isFlatDomain ? 'flat' : 'nested' | ||
}) | ||
|
||
return webLink | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getSharingLink } from './sharing' | ||
|
||
describe('getSharingLink', () => { | ||
const mockSharecode = { attributes: { shortcodes: { code: 'shortcode' } } } | ||
const mockClient = { | ||
save: jest.fn(() => ({ data: mockSharecode })), | ||
getStackClient: jest.fn(() => ({ uri: 'http://cozy.cloud' })) | ||
} | ||
const mockFiles = [ | ||
{ id: 'fileId01', name: 'File 01' }, | ||
{ id: 'fileId02', name: 'File 02' } | ||
] | ||
|
||
it('should generate the right share link if "isFlatDomain" param is not defined', async () => { | ||
const sharingLink = await getSharingLink(mockClient, mockFiles) | ||
|
||
expect(sharingLink).toBe( | ||
'http://drive.cozy.cloud/public?sharecode=shortcode#/' | ||
) | ||
}) | ||
|
||
it('should generate the right share link to a nested cozy', async () => { | ||
const isFlatDomain = false | ||
const sharingLink = await getSharingLink( | ||
mockClient, | ||
mockFiles, | ||
isFlatDomain | ||
) | ||
|
||
expect(sharingLink).toBe( | ||
'http://drive.cozy.cloud/public?sharecode=shortcode#/' | ||
) | ||
}) | ||
|
||
it('should generate the right share link to a flat cozy', async () => { | ||
const isFlatDomain = true | ||
const sharingLink = await getSharingLink( | ||
mockClient, | ||
mockFiles, | ||
isFlatDomain | ||
) | ||
|
||
expect(sharingLink).toBe( | ||
'http://cozy-drive.cloud/public?sharecode=shortcode#/' | ||
) | ||
}) | ||
|
||
it('should generate the right share link with an correct sharecode', async () => { | ||
const sharingLink = await getSharingLink(mockClient, mockFiles) | ||
|
||
expect(sharingLink).toContain('sharecode=shortcode') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const REGISTRATION_ABORT: "REGISTRATION_ABORT"; | ||
export const DOCTYPE_FILES: "io.cozy.files"; | ||
export const DOCTYPE_PERMISSIONS: "io.cozy.permissions"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export function getSharingLink(client: CozyClient, filesIds: string[], isFlatDomain?: boolean): Promise<string>; | ||
import CozyClient from "../CozyClient"; |