-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { privatePreviewBlob } from './privatePreviewBlob' | ||
import { publicPreviewUrl } from './publicPreviewUrl' | ||
|
||
export const loadPreview = async ( | ||
{ | ||
resource, | ||
isPublic, | ||
dimensions, | ||
server, | ||
userId, | ||
token | ||
}: { | ||
resource: any | ||
isPublic: boolean | ||
dimensions: [number, number] | ||
server?: string | ||
userId?: string | ||
token?: string | ||
}, | ||
cached = false | ||
): Promise<string> => { | ||
let preview = '' | ||
if (resource.type === 'folder' || !resource.extension) { | ||
return preview | ||
} | ||
|
||
if (!isPublic && (!server || !userId || !token)) { | ||
return preview | ||
} | ||
|
||
if (isPublic) { | ||
preview = await publicPreviewUrl({ resource, dimensions }) | ||
} else { | ||
preview = await privatePreviewBlob( | ||
{ | ||
server, | ||
userId, | ||
token, | ||
resource, | ||
dimensions | ||
}, | ||
cached | ||
) | ||
} | ||
|
||
return preview | ||
} |
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 * from './asset' | ||
export * from './privatePreviewBlob' | ||
export * from './publicPreviewUrl' |
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
83 changes: 83 additions & 0 deletions
83
packages/web-app-files/tests/unit/helpers/resource/asset.spec.ts
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,83 @@ | ||
import { loadPreview } from '../../../../src/helpers/resource' | ||
|
||
jest.mock('../../../../src/helpers/resource/publicPreviewUrl', () => ({ | ||
publicPreviewUrl: jest.fn().mockReturnValue('publicPreviewUrl') | ||
})) | ||
|
||
jest.mock('../../../../src/helpers/resource/privatePreviewBlob', () => ({ | ||
privatePreviewBlob: jest.fn().mockReturnValue('privatePreviewBlob') | ||
})) | ||
|
||
describe('loadPreview', () => { | ||
it('returns empty string if resource is a folder', async () => { | ||
const preview = await loadPreview({ | ||
resource: { type: 'folder' }, | ||
isPublic: true, | ||
dimensions: [0, 0] | ||
}) | ||
|
||
expect(preview).toBe('') | ||
}) | ||
|
||
it('returns empty string if resource has no extension', async () => { | ||
const preview = await loadPreview({ | ||
resource: {}, | ||
isPublic: true, | ||
dimensions: [0, 0] | ||
}) | ||
|
||
expect(preview).toBe('') | ||
}) | ||
|
||
it('returns empty string if is private but no server, userId or token is given', async () => { | ||
await expect( | ||
loadPreview({ | ||
resource: { extension: 'jpg' }, | ||
isPublic: false, | ||
dimensions: [0, 0] | ||
}) | ||
).resolves.toBe('') | ||
|
||
await expect( | ||
loadPreview({ | ||
resource: { extension: 'jpg' }, | ||
isPublic: false, | ||
dimensions: [0, 0], | ||
server: 'server' | ||
}) | ||
).resolves.toBe('') | ||
|
||
await expect( | ||
loadPreview({ | ||
resource: { extension: 'jpg' }, | ||
isPublic: false, | ||
dimensions: [0, 0], | ||
server: 'server', | ||
userId: 'userId' | ||
}) | ||
).resolves.toBe('') | ||
}) | ||
|
||
it('returns a publicPreviewUrl', async () => { | ||
await expect( | ||
loadPreview({ | ||
resource: { extension: 'jpg' }, | ||
isPublic: true, | ||
dimensions: [0, 0] | ||
}) | ||
).resolves.toBe('publicPreviewUrl') | ||
}) | ||
|
||
it('returns a privatePreviewBlob', async () => { | ||
await expect( | ||
loadPreview({ | ||
resource: { extension: 'jpg' }, | ||
isPublic: false, | ||
dimensions: [0, 0], | ||
server: 'server', | ||
userId: 'userId', | ||
token: 'token' | ||
}) | ||
).resolves.toBe('privatePreviewBlob') | ||
}) | ||
}) |
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