diff --git a/package.json b/package.json index b9b29411..c2a034e4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.29.2", + "version": "1.29.3", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", diff --git a/src/drive/share/index.ts b/src/drive/share/index.ts index 22e66f11..d75244ac 100644 --- a/src/drive/share/index.ts +++ b/src/drive/share/index.ts @@ -16,6 +16,7 @@ import { ListPrivateSharedFoldersResponse, ListShareLinksResponse, ListSharedItemsResponse, + PublicSharedFileDownloadAccess, PublicSharedItemInfo, RemoveUserRolePayload, Role, @@ -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 { + 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 { + 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 */ diff --git a/src/drive/share/types.ts b/src/drive/share/types.ts index 1821540c..5c22482b 100644 --- a/src/drive/share/types.ts +++ b/src/drive/share/types.ts @@ -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[]; diff --git a/test/drive/share/index.test.ts b/test/drive/share/index.test.ts index d81cdc8c..db1d8262 100644 --- a/test/drive/share/index.test.ts +++ b/test/drive/share/index.test.ts @@ -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',