Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Audio playback #52

Merged
merged 8 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion natster-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"nkeys.js": "^1.1.0",
"pinia": "^2.1.7",
"vue": "^3.4.15",
"vue-router": "^4.2.5"
"vue-router": "^4.2.5",
"vue3-audio-player": "^1.0.7"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.3.3",
Expand Down
52 changes: 51 additions & 1 deletion natster-io/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 37 additions & 20 deletions natster-io/src/components/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,40 @@
</button>
</div>
<div class="sm:flex sm:items-start">
<div
class="mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10"
>
<InformationCircleIcon class="h-6 w-6 text-blue-400" aria-hidden="true" />
</div>
<div class="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left">
<DialogTitle as="h3" class="text-base font-semibold leading-6 text-gray-900">{{
title
}}</DialogTitle>
<DialogTitle as="h3" class="text-base font-semibold leading-6 text-gray-900">
{{ catalog.name }} | {{ title }}</DialogTitle
>
<div class="mt-2">
<p v-if="!!body" class="text-sm text-gray-500">
{{ body }}
</p>

<video
v-if="!!mediaUrl"
id="video"
:type="mimeType"
:src="mediaUrl"
width="640"
height="360"
autoplay
controls
></video>
</div>
</div>
</div>
<div class="relative">
<video
v-if="!!mediaUrl && mimeType.toLowerCase() == 'video/mp4'"
id="video"
:type="mimeType"
:src="mediaUrl"
width="640"
height="360"
autoplay
controls
></video>
<AudioPlayer
v-if="!!mediaUrl && mimeType.toLowerCase() == 'audio/mpeg'"
:option="
getAudioOptions(
mediaUrl,
description == '' ? title : description,
catalog.image
)
"
:title="title"
/>
</div>
<div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<button
type="button"
Expand All @@ -89,10 +96,12 @@ import { ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { Dialog, DialogPanel, DialogTitle, TransitionChild, TransitionRoot } from '@headlessui/vue'
import { InformationCircleIcon, XMarkIcon } from '@heroicons/vue/24/outline'
import AudioPlayer from 'vue3-audio-player'
import 'vue3-audio-player/dist/style.css'

import { fileStore } from '../stores/file'
const fStore = fileStore()
const { body, title, show, mimeType, mediaUrl } = storeToRefs(fStore)
const { body, title, show, mimeType, mediaUrl, catalog, description } = storeToRefs(fStore)

function close() {
console.log('reset')
Expand All @@ -106,6 +115,14 @@ watch(mimeType, (newVal, oldVal) => {
}
})

function getAudioOptions(inSrc, inTitle, inCover) {
return {
src: inSrc,
title: inTitle,
coverImage: inCover
}
}

watch(mediaUrl, (newVal, oldVal) => {
if (!!newVal) {
setTimeout(() => {
Expand Down
15 changes: 8 additions & 7 deletions natster-io/src/components/FileComp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@
<button
:disabled="false"
@click.prevent="
cStore.downloadFile(
getFileName(file.path),
catalog.name,
file.hash,
file.mime_type
)
cStore.downloadFile(getFileName(file.path), catalog, file.hash, file.mime_type)
"
:class="[
active ? 'bg-violet-500 text-white' : 'text-gray-900',
Expand All @@ -92,7 +87,13 @@
<MenuItem v-slot="{ active }">
<button
@click.prevent="
cStore.viewFile(getFileName(file.path), catalog.name, file.hash, file.mime_type)
cStore.viewFile(
getFileName(file.path),
file.description,
catalog,
file.hash,
file.mime_type
)
"
:class="[
active ? 'bg-violet-500 text-white' : 'text-gray-900',
Expand Down
27 changes: 19 additions & 8 deletions natster-io/src/stores/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ export const catalogStore = defineStore('catalog', {

var fileArray
const nStore = natsStore()
const sub = nStore.connection.subscribe('natster.media.' + catalog + '.' + hash)
const sub = nStore.connection.subscribe('natster.media.' + catalog.name + '.' + hash)
;(async () => {
for await (const m of sub) {
const chunkIdx = parseInt(m.headers.get('x-natster-chunk-idx'))
const totalChunks = parseInt(m.headers.get('x-natster-total-chunks'))
const senderXKey = m.headers.get('x-natster-sender-xkey')

let decrypted = xkey.open(m.data, senderXKey)
fileArray.push(decrypted)

Expand All @@ -172,7 +172,7 @@ export const catalogStore = defineStore('catalog', {
target_xkey: this.xkey_pub
}
await nStore.connection
.request('natster.catalog.' + catalog + '.download', JSON.stringify(dl_request), {
.request('natster.catalog.' + catalog.name + '.download', JSON.stringify(dl_request), {
timeout: 5000
})
.then((m) => {
Expand All @@ -183,7 +183,7 @@ export const catalogStore = defineStore('catalog', {
console.error('nats requestCatalogFiles err: ', err)
})
},
async viewFile(fileName, catalog, hash, mimeType) {
async viewFile(fileName, fileDescription, catalog, hash, mimeType) {
const fStore = fileStore()

let xkey = createCurve()
Expand All @@ -192,7 +192,7 @@ export const catalogStore = defineStore('catalog', {

var fileArray
const nStore = natsStore()
const sub = nStore.connection.subscribe('natster.media.' + catalog + '.' + hash)
const sub = nStore.connection.subscribe('natster.media.' + catalog.name + '.' + hash)
;(async () => {
let timeout
for await (const m of sub) {
Expand All @@ -207,7 +207,7 @@ export const catalogStore = defineStore('catalog', {
timeout = null
}

fStore.render(fileName, mimeType, decrypted)
fStore.render(fileName, fileDescription, mimeType, decrypted, catalog)

timeout = setTimeout(() => {
fStore.endStream()
Expand All @@ -216,9 +216,20 @@ export const catalogStore = defineStore('catalog', {
sub.unsubscribe()
}, 5000)
} else {
fStore.render(fileName, mimeType, new TextDecoder().decode(decrypted))
if (mimeType.toLowerCase() === 'audio/mpeg') {
fStore.render(fileName, fileDescription, mimeType, decrypted, catalog)
} else {
fStore.render(
fileName,
fileDescription,
mimeType,
new TextDecoder().decode(decrypted),
catalog
)
}

if (chunkIdx === totalChunks - 1) {
fStore.endStream()
sub.unsubscribe()
}
}
Expand All @@ -232,7 +243,7 @@ export const catalogStore = defineStore('catalog', {
target_xkey: this.xkey_pub
}
await nStore.connection
.request('natster.catalog.' + catalog + '.download', JSON.stringify(dl_request), {
.request('natster.catalog.' + catalog.name + '.download', JSON.stringify(dl_request), {
timeout: 5000
})
.then((m) => {
Expand Down
Loading
Loading