Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <hello@internxt.com>",
"version": "1.29.2",
"version": "1.29.3",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
47 changes: 47 additions & 0 deletions src/drive/share/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ListPrivateSharedFoldersResponse,
ListShareLinksResponse,
ListSharedItemsResponse,
PublicSharedFileDownloadAccess,
PublicSharedItemInfo,
RemoveUserRolePayload,
Role,
Expand Down Expand Up @@ -435,6 +436,52 @@ export class Share {
);
}

/**
* Get a per-file network token and key to download one file of a public sharing.
* @param {string} sharingId - id of the public sharing.
* @param {string} fileUuid - uuid of the file inside the shared folder.
* @param {string} code - The link code of the public sharing.
* @param {string} [accessToken] - Access token returned by getSharingMeta, sent as x-share-access-token.
*/
public getPublicSharedFileDownloadAccess(
sharingId: string,
fileUuid: string,
code: string,
accessToken?: string,
): Promise<PublicSharedFileDownloadAccess> {
return this.client.get(
`sharings/public/${sharingId}/files/${fileUuid}/download-access`,
this.shareAccessHeaders(code, accessToken),
);
}

/**
* Get a per-file network token and key to download the thumbnail of one file of a public sharing.
* @param {string} sharingId - id of the public sharing.
* @param {string} fileUuid - uuid of the file inside the shared folder.
* @param {string} code - The link code of the public sharing.
* @param {string} [accessToken] - Access token returned by getSharingMeta, sent as x-share-access-token.
*/
public getPublicSharedThumbnailDownloadAccess(
sharingId: string,
fileUuid: string,
code: string,
accessToken?: string,
): Promise<PublicSharedFileDownloadAccess> {
return this.client.get(
`sharings/public/${sharingId}/files/${fileUuid}/thumbnail-download-access`,
this.shareAccessHeaders(code, accessToken),
);
}

private shareAccessHeaders(code: string, accessToken?: string) {
return {
...this.headers(),
'x-share-code': code,
...(accessToken && { 'x-share-access-token': accessToken }),
};
}

/**
* Get sharing meta with code
*/
Expand Down
7 changes: 7 additions & 0 deletions src/drive/share/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ export type ListSharedItemsResponse = {
role: string;
};

export interface PublicSharedFileDownloadAccess {
bucket: string;
fileId: string;
encryptionKey: string;
token: string;
}

export type ListAllSharedFoldersResponse = {
credentials: { networkPass: string; networkUser: string };
files: SharedFiles[];
Expand Down
42 changes: 42 additions & 0 deletions test/drive/share/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,48 @@ describe('# share service tests', () => {
});
});

describe('public shared file download access', () => {
const sharingId = 'sharing-id';
const fileUuid = 'file-uuid';
const access = { bucket: 'bucket', fileId: 'file-id', encryptionKey: 'ab12', token: 'bridge-token' };

test.each([
['getPublicSharedFileDownloadAccess', 'download-access'],
['getPublicSharedThumbnailDownloadAccess', 'thumbnail-download-access'],
] as const)(
'When %s is called with an access token, then code and token headers are sent',
async (method, path) => {
const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue(access);
const { client, headers } = clientAndHeadersWithToken();

const body = await client[method](sharingId, fileUuid, 'the-code', 'access-token');

expect(callStub).toHaveBeenCalledWith(`sharings/public/${sharingId}/files/${fileUuid}/${path}`, {
...headers,
'x-share-code': 'the-code',
'x-share-access-token': 'access-token',
});
expect(body).toStrictEqual(access);
},
);

test.each([
['getPublicSharedFileDownloadAccess', 'download-access'],
['getPublicSharedThumbnailDownloadAccess', 'thumbnail-download-access'],
] as const)('When %s is called without access token, then only the code header is sent', async (method, path) => {
const callStub = vi.spyOn(HttpClient.prototype, 'get').mockResolvedValue(access);
const { client, headers } = clientAndHeadersWithToken();

await client[method](sharingId, fileUuid, 'the-code');

expect(callStub).toHaveBeenCalledWith(`sharings/public/${sharingId}/files/${fileUuid}/${path}`, {
...headers,
'x-share-code': 'the-code',
});
expect(callStub.mock.calls[0][1]).not.toHaveProperty('x-share-access-token');
});
});

function clientAndHeadersWithToken(
apiUrl = '',
clientName = 'c-name',
Expand Down
Loading