Skip to content

Commit

Permalink
feat: handle subtitles for video streaming (filebrowser#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
VLEFF authored and hacdias committed Jul 26, 2018
1 parent 2f17f19 commit 9f075c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/files/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<img v-if="req.type == 'image'" :src="raw()">
<audio v-else-if="req.type == 'audio'" :src="raw()" autoplay controls></audio>
<video v-else-if="req.type == 'video'" :src="raw()" autoplay controls>
<track v-for="(sub, index) in subtitles" :kind="sub.kind" :src="'/api/subtitle/' + sub.src" :label="sub.label" :default="index === 0">
Sorry, your browser doesn't support embedded videos,
but don't worry, you can <a :href="download()">download it</a>
and watch it with your favorite video player!
Expand Down Expand Up @@ -56,7 +57,8 @@ export default {
return {
previousLink: '',
nextLink: '',
listing: null
listing: null,
subtitles: []
}
},
computed: {
Expand All @@ -76,6 +78,13 @@ export default {
this.updateLinks()
})
.catch(this.$showError)
if (this.req.type === 'audio' || this.req.type === 'video') {
api.subtitles(this.req.url.slice(6))
.then(req => {
this.subtitles = req
})
.catch(this.$showError)
}
},
beforeDestroy () {
window.removeEventListener('keyup', this.key)
Expand Down
23 changes: 23 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,26 @@ export function share (url, expires = '', unit = 'hours') {
request.send()
})
}

export function subtitles (url) {
url = removePrefix(url)

return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
request.open('GET', `${store.state.baseURL}/api/subtitles${url}`, true)
if (!store.state.noAuth) request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)

request.onload = () => {
switch (request.status) {
case 200:
resolve(JSON.parse(request.responseText))
break
default:
reject(new Error(request.status))
break
}
}
request.onerror = (error) => reject(error)
request.send()
})
}

0 comments on commit 9f075c1

Please sign in to comment.