forked from welovedevs/react-ultimate-resume
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ welovedevs#55 Permit to provide differents playlist provider
- Loading branch information
Vincent Boulin
committed
Feb 24, 2022
1 parent
7d59c38
commit 2b8a064
Showing
3 changed files
with
83 additions
and
16 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
44 changes: 44 additions & 0 deletions
44
src/package/components/cards/cards_types/soundtrack/edit_dialog/soundtrack_card_utils.js
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,44 @@ | ||
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/; | ||
const SPOTIFY_DOMAIN = 'https://open.spotify.com'; | ||
const DEEZER_DOMAIN = 'https://www.deezer.com'; | ||
const DEEZER_EMBEDDED_BASE = 'https://widget.deezer.com/widget/dark/playlist'; | ||
const SOUNDCLOUD_DOMAIN = 'https://soundcloud.com'; | ||
const AUTHORIZED_DOMAIN = [SPOTIFY_DOMAIN, DEEZER_DOMAIN, SOUNDCLOUD_DOMAIN]; | ||
|
||
const startsWith = (testValue) => (value) => testValue.startsWith(value); | ||
|
||
export const isValidEmbeddedUrl = (url) => | ||
URL_REGEX.test(url) && (url?.includes('/embed') || url?.includes('/widget') || url?.includes('/player')); | ||
|
||
export const getEmbeddedUrl = (url) => { | ||
if (!URL_REGEX.test(url) || !AUTHORIZED_DOMAIN.some(startsWith(url))) { | ||
return; | ||
} | ||
|
||
let domain = AUTHORIZED_DOMAIN.find(startsWith(url)); | ||
if (domain === SPOTIFY_DOMAIN && !url.includes('/embed')) { | ||
return getEmbeddedUrlSpotify(url); | ||
} else if (domain === SOUNDCLOUD_DOMAIN) { | ||
return getEmbeddedUrlSoundCloud(url); | ||
} else if (domain === DEEZER_DOMAIN) { | ||
return getEmbeddedUrlDeezer(url); | ||
} | ||
return url; | ||
}; | ||
|
||
const getEmbeddedUrlSpotify = (url) => { | ||
if (url.includes('/embed')) { | ||
return url; | ||
} | ||
|
||
return `${url.substring(0, SPOTIFY_DOMAIN.length)}/embed/${url.substring(SPOTIFY_DOMAIN.length + 1, url.length)}`; | ||
}; | ||
|
||
const getEmbeddedUrlSoundCloud = (url) => { | ||
const lastIndex = url.indexOf('?') === -1 ? url.length : url.indexOf('?'); | ||
return `https://w.soundcloud.com/player/?url=${url.substring(0, lastIndex)}`; | ||
}; | ||
|
||
const getEmbeddedUrlDeezer = (url) => { | ||
return `${DEEZER_EMBEDDED_BASE}/${/[^/]*$/.exec(url)}`; | ||
}; |
33 changes: 33 additions & 0 deletions
33
...package/components/cards/cards_types/soundtrack/edit_dialog/soundtrack_card_utils.test.js
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,33 @@ | ||
import { getEmbeddedUrl, isValidEmbeddedUrl } from './soundtrack_card_utils'; | ||
|
||
test('it should format embedded soundcloud url', () => { | ||
const url = 'https://soundcloud.com/lukasm1/sets/chill-mix-high-on-chill'; | ||
const expected = `https://w.soundcloud.com/player/?url=https://soundcloud.com/lukasm1/sets/chill-mix-high-on-chill`; | ||
expect(getEmbeddedUrl(url)).toBe(expected); | ||
}); | ||
|
||
test('it should format embedded deezer url', () => { | ||
const url = 'https://www.deezer.com/us/playlist/1479458365'; | ||
const expected = `https://widget.deezer.com/widget/dark/playlist/1479458365`; | ||
expect(getEmbeddedUrl(url)).toBe(expected); | ||
}); | ||
|
||
test('it should format embedded spotify url', () => { | ||
const url = 'https://www.deezer.com/us/playlist/1479458365'; | ||
const expected = `https://widget.deezer.com/widget/dark/playlist/1479458365`; | ||
expect(getEmbeddedUrl(url)).toBe(expected); | ||
}); | ||
|
||
test('it should be a valid url', () => { | ||
const urlEmbed = 'https://truc.com/embed'; | ||
const urlWidget = 'https://truc.com/widget'; | ||
const urlPlayer = 'https://truc.com/player'; | ||
expect(isValidEmbeddedUrl(urlEmbed)).toBe(true); | ||
expect(isValidEmbeddedUrl(urlWidget)).toBe(true); | ||
expect(isValidEmbeddedUrl(urlPlayer)).toBe(true); | ||
}); | ||
|
||
test('it should not be a valid url', () => { | ||
const url = 'https://truc.com/wrong'; | ||
expect(isValidEmbeddedUrl(url)).toBe(false); | ||
}); |