Skip to content

Commit

Permalink
Add support for dropbox files
Browse files Browse the repository at this point in the history
Fixes cookpete/react-player#379

It turns out you can just replace www.dropbox.com with
dl.dropboxusercontent.com to access a shared file directly

Found in this handy article:
https://zapier.com/learn/how-to/generate-direct-dropbox-link/
  • Loading branch information
albanqoku committed Apr 21, 2018
1 parent fc12af1 commit 2613b1d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/players/FilePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const HLS_GLOBAL = 'Hls'
const DASH_EXTENSIONS = /\.(mpd)($|\?)/i
const DASH_SDK_URL = 'https://cdnjs.cloudflare.com/ajax/libs/dashjs/2.6.5/dash.all.min.js'
const DASH_GLOBAL = 'dashjs'
const MATCH_DROPBOX_URL = /www\.dropbox\.com\/.+/

function canPlay (url) {
if (url instanceof Array) {
Expand Down Expand Up @@ -164,7 +165,18 @@ export class FilePlayer extends Component {
if (this.player.buffered.length === 0) return 0
return this.getBufferedEnd()
}
renderSource = (source, index) => {
getSource (url) {
const useHLS = this.shouldUseHLS(url)
const useDASH = this.shouldUseDASH(url)
if (url instanceof Array || useHLS || useDASH) {
return undefined
}
if (MATCH_DROPBOX_URL.test(url)) {
return url.replace('www.dropbox.com', 'dl.dropboxusercontent.com')
}
return url
}
renderSourceElement = (source, index) => {
if (typeof source === 'string') {
return <source key={index} src={source} />
}
Expand All @@ -179,25 +191,22 @@ export class FilePlayer extends Component {
render () {
const { url, loop, controls, config, width, height } = this.props
const useAudio = this.shouldUseAudio(this.props)
const useHLS = this.shouldUseHLS(url)
const useDASH = this.shouldUseDASH(url)
const Element = useAudio ? 'audio' : 'video'
const src = url instanceof Array || useHLS || useDASH ? undefined : url
const style = {
width: !width || width === 'auto' ? width : '100%',
height: !height || height === 'auto' ? height : '100%'
}
return (
<Element
ref={this.ref}
src={src}
src={this.getSource(url)}
style={style}
preload='auto'
controls={controls}
loop={loop}
{...config.file.attributes}>
{url instanceof Array &&
url.map(this.renderSource)
url.map(this.renderSourceElement)
}
{config.file.tracks.map(this.renderTrack)}
</Element>
Expand Down

0 comments on commit 2613b1d

Please sign in to comment.