From db3a5fcd8edaa45ef05a6250e7f9452f0ed87250 Mon Sep 17 00:00:00 2001
From: Jules Laplace <julescarbon@gmail.com>
Date: Sun, 2 Jul 2017 22:49:26 +0200
Subject: resample to 16bit 44100hz aiff

---
 client/components/Folder/Folder.jsx | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

(limited to 'client/components/Folder/Folder.jsx')

diff --git a/client/components/Folder/Folder.jsx b/client/components/Folder/Folder.jsx
index 88c937a..4a2e4d2 100644
--- a/client/components/Folder/Folder.jsx
+++ b/client/components/Folder/Folder.jsx
@@ -17,6 +17,7 @@ export default class Folder extends Component {
     if (! props.folder.files) {
       client.file.index({ folder_id: props.folder.id }).then( files => this.setState({ files }) )
     }
+    this.audio = document.createElement('audio')
   }
   addFiles(newFiles) {
     console.log(newFiles)
@@ -24,13 +25,22 @@ export default class Folder extends Component {
     const files = this.state.files.concat(newFiles).sort( (a,b) => { return b.id - a.id } )
     this.setState({ files })
   }
+  handleClick(file) {
+    console.log(file)
+    if (file.type === 'audio') {
+      this.audio.setAttribute('src', mp3path(file))
+      this.audio.play()
+      console.log(pngpath(file))
+      document.body.style.backgroundImage = 'url(' + pngpath(file) + ')'
+    }
+  }
   render() {
     console.log(this.props)
-    const files = (this.state.files).map( (file, i) => {
+    const files = (this.state.files).map(toFilenamePair).sort(sortByFilename).map(fromPair).map( (file, i) => {
       if (! file) return
       return (
         <div key={i}>
-          <span class='name'>{file.name}</span>
+          <span class='name' onClick={() => this.handleClick(file)}>{file.name}</span>
           <span class='mime'>{file.mime}</span>
         </div>
       )
@@ -51,3 +61,20 @@ export default class Folder extends Component {
     )
   }
 }
+
+function toFilenamePair (file) { return [file.name.toLowerCase(), file] }
+function sortByFilename (a,b) { return a[0] < b[0] ? -1 : a[0] == b[0] ? 0 : 1 }
+function fromPair (pair) { return pair[1] }
+
+function filepath (file) {
+  return '/data/' + file.folder_id + '/' + encodeURIComponent(file.name)
+}
+function mp3path (file) {
+  if (file.mime !== 'audio/mp3') {
+    return filepath(file) + '.mp3'
+  }
+  return filepath(file)
+}
+function pngpath (file) {
+  return filepath(file) + '.png'
+}
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2

