Handle tracks
This commit is contained in:
parent
5799eb32ef
commit
5c7841fdc6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
reveil
|
reveil
|
||||||
|
tracks
|
||||||
vendor
|
vendor
|
||||||
ui/build
|
ui/build
|
||||||
ui/node_modules
|
ui/node_modules
|
@ -1,37 +1,94 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"git.nemunai.re/nemunaire/reveil/config"
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
|
"git.nemunai.re/nemunaire/reveil/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
func declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||||
router.GET("/tracks", func(c *gin.Context) {
|
router.GET("/tracks", func(c *gin.Context) {
|
||||||
|
tracks, err := reveil.LoadTracks(cfg)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, tracks)
|
||||||
})
|
})
|
||||||
router.POST("/tracks", func(c *gin.Context) {
|
router.POST("/tracks", func(c *gin.Context) {
|
||||||
|
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||||
})
|
})
|
||||||
|
|
||||||
tracksRoutes := router.Group("/tracks/:tid")
|
tracksRoutes := router.Group("/tracks/:tid")
|
||||||
tracksRoutes.Use(trackHandler)
|
tracksRoutes.Use(func(c *gin.Context) {
|
||||||
|
tracks, err := reveil.LoadTracks(cfg)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, t := range tracks {
|
||||||
|
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
|
||||||
|
c.Set("track", t)
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Track not found"})
|
||||||
|
})
|
||||||
|
|
||||||
tracksRoutes.GET("", func(c *gin.Context) {
|
tracksRoutes.GET("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
c.JSON(http.StatusOK, c.MustGet("track"))
|
||||||
})
|
})
|
||||||
tracksRoutes.PUT("", func(c *gin.Context) {
|
tracksRoutes.PUT("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
oldtrack := c.MustGet("track").(*reveil.Track)
|
||||||
|
|
||||||
|
var track reveil.Track
|
||||||
|
if err := c.ShouldBindJSON(&track); err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if track.Name != oldtrack.Name {
|
||||||
|
err := oldtrack.Rename(track.Name)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the track: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if track.Enabled != oldtrack.Enabled {
|
||||||
|
var err error
|
||||||
|
if track.Enabled {
|
||||||
|
err = oldtrack.Enable(cfg)
|
||||||
|
} else {
|
||||||
|
err = oldtrack.Disable()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to enable/disable the track: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, oldtrack)
|
||||||
})
|
})
|
||||||
tracksRoutes.DELETE("", func(c *gin.Context) {
|
tracksRoutes.DELETE("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("track"))
|
track := c.MustGet("track").(*reveil.Track)
|
||||||
|
|
||||||
|
err := track.Remove()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the track: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func trackHandler(c *gin.Context) {
|
|
||||||
c.Set("track", nil)
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
}
|
|
||||||
|
88
model/track.go
Normal file
88
model/track.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package reveil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha512"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Track struct {
|
||||||
|
Id []byte `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadTracks(cfg *config.Config) (tracks []*Track, err error) {
|
||||||
|
err = filepath.Walk(cfg.TracksDir, func(path string, d fs.FileInfo, err error) error {
|
||||||
|
if d.Mode().IsRegular() {
|
||||||
|
hash := sha512.Sum512([]byte(path))
|
||||||
|
tracks = append(tracks, &Track{
|
||||||
|
Id: hash[:63],
|
||||||
|
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||||
|
Path: path,
|
||||||
|
Enabled: len(strings.Split(path, "/")) == 2,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Track) Rename(newName string) error {
|
||||||
|
newPath := filepath.Join(filepath.Dir(t.Path), newName+filepath.Ext(t.Path))
|
||||||
|
|
||||||
|
err := os.Rename(
|
||||||
|
t.Path,
|
||||||
|
newPath,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Path = newPath
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Track) MoveTo(path string) error {
|
||||||
|
os.Mkdir(filepath.Dir(path), 0755)
|
||||||
|
|
||||||
|
err := os.Rename(
|
||||||
|
t.Path,
|
||||||
|
path,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Path = path
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Track) Disable() error {
|
||||||
|
if t.Enabled {
|
||||||
|
date := time.Now()
|
||||||
|
return t.MoveTo(filepath.Join(filepath.Dir(t.Path), date.Format("20060102"), filepath.Base(t.Path)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Track) Enable(cfg *config.Config) error {
|
||||||
|
if !t.Enabled {
|
||||||
|
return t.MoveTo(filepath.Join(cfg.TracksDir, filepath.Base(t.Path)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Track) Remove() error {
|
||||||
|
return os.Remove(t.Path)
|
||||||
|
}
|
@ -5,33 +5,24 @@
|
|||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Icon,
|
Icon,
|
||||||
|
Spinner,
|
||||||
} from 'sveltestrap';
|
} from 'sveltestrap';
|
||||||
|
|
||||||
let musiks = [
|
import { tracks } from '../stores/tracks';
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
title: "Hall Of Fame",
|
|
||||||
artist: "The Script",
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
title: "Poker face",
|
|
||||||
artist: "Lady Gaga",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
title: "Puisque tu m'aimes encore",
|
|
||||||
artist: "Céline Dion",
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export let flush = false;
|
export let flush = false;
|
||||||
export let edit = false;
|
export let edit = false;
|
||||||
|
|
||||||
export { className as class };
|
export { className as class };
|
||||||
let className = '';
|
let className = '';
|
||||||
|
|
||||||
|
let refreshInProgress = false;
|
||||||
|
function refresh_tracks() {
|
||||||
|
refreshInProgress = true;
|
||||||
|
tracks.refresh().then(() => {
|
||||||
|
refreshInProgress = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="d-flex justify-content-between align-items-center" class:px-2={flush}>
|
<div class="d-flex justify-content-between align-items-center" class:px-2={flush}>
|
||||||
@ -58,30 +49,47 @@
|
|||||||
<Button
|
<Button
|
||||||
color="outline-dark"
|
color="outline-dark"
|
||||||
size="sm"
|
size="sm"
|
||||||
|
title="Rafraîchir la liste des pistes"
|
||||||
|
on:click={refresh_tracks}
|
||||||
|
disabled={refreshInProgress}
|
||||||
>
|
>
|
||||||
<Icon name="arrow-clockwise" />
|
{#if !refreshInProgress}
|
||||||
|
<Icon name="arrow-clockwise" />
|
||||||
|
{:else}
|
||||||
|
<Spinner color="dark" size="sm" />
|
||||||
|
{/if}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="list-group {className}" class:list-group-flush={flush}>
|
<div class="list-group {className}" class:list-group-flush={flush}>
|
||||||
{#each musiks as track (track.id)}
|
{#if $tracks.list}
|
||||||
<button
|
{#each $tracks.list as track (track.id)}
|
||||||
type="button"
|
<button
|
||||||
class="list-group-item list-group-item-action"
|
type="button"
|
||||||
class:active={$page.url.pathname.indexOf('/tracks/') !== -1 && $page.params.tid == track.id}
|
class="list-group-item list-group-item-action"
|
||||||
aria-current="true"
|
class:active={$page.url.pathname.indexOf('/tracks/') !== -1 && $page.params.tid == track.id}
|
||||||
on:click={() => {
|
aria-current="true"
|
||||||
if (edit) {
|
on:click={() => {
|
||||||
goto('musiks/tracks/' + track.id)
|
if (edit) {
|
||||||
} else {
|
goto('musiks/tracks/' + track.id);
|
||||||
track.enabled = !track.enabled
|
} else {
|
||||||
}
|
track = track.toggleEnable()
|
||||||
}}
|
}
|
||||||
>
|
}}
|
||||||
{#if !edit}
|
>
|
||||||
<input class="form-check-input me-1" type="checkbox" checked={track.enabled}>
|
{#if !edit}
|
||||||
{/if}
|
<input class="form-check-input me-1" type="checkbox" checked={track.enabled}>
|
||||||
<span class:fw-bold={!edit && track.enabled}>{track.artist} – {track.title}</span>
|
{/if}
|
||||||
</button>
|
<span class:fw-bold={!edit && track.enabled}>{track.name}</span>
|
||||||
{/each}
|
</button>
|
||||||
|
{/each}
|
||||||
|
{:else}
|
||||||
|
{#await tracks.refresh()}
|
||||||
|
<div class="d-flex justify-content-center align-items-center gap-2">
|
||||||
|
<Spinner color="primary" /> Chargement en cours…
|
||||||
|
</div>
|
||||||
|
{:then}
|
||||||
|
test
|
||||||
|
{/await}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
65
ui/src/lib/track.js
Normal file
65
ui/src/lib/track.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
export class Track {
|
||||||
|
constructor(res) {
|
||||||
|
if (res) {
|
||||||
|
this.update(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update({ id, name, path, enabled }) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.path = path;
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete() {
|
||||||
|
const res = await fetch(`api/tracks/${this.id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {'Accept': 'application/json'}
|
||||||
|
});
|
||||||
|
if (res.status == 200) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleEnable() {
|
||||||
|
this.enabled = !this.enabled;
|
||||||
|
this.save();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
async save() {
|
||||||
|
const res = await fetch(this.id?`api/tracks/${this.id}`:'api/tracks', {
|
||||||
|
method: this.id?'PUT':'POST',
|
||||||
|
headers: {'Accept': 'application/json'},
|
||||||
|
body: JSON.stringify(this),
|
||||||
|
});
|
||||||
|
if (res.status == 200) {
|
||||||
|
const data = await res.json();
|
||||||
|
this.update(data);
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getTracks() {
|
||||||
|
const res = await fetch(`api/tracks`, {headers: {'Accept': 'application/json'}})
|
||||||
|
if (res.status == 200) {
|
||||||
|
return (await res.json()).map((t) => new Track(t));
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getTrack(tid) {
|
||||||
|
const res = await fetch(`api/tracks/${tid}`, {headers: {'Accept': 'application/json'}})
|
||||||
|
if (res.status == 200) {
|
||||||
|
return new Track(await res.json());
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,39 @@
|
|||||||
<h2>
|
<script>
|
||||||
Track
|
import { page } from '$app/stores';
|
||||||
</h2>
|
|
||||||
|
import {
|
||||||
|
Container,
|
||||||
|
Input,
|
||||||
|
ListGroup,
|
||||||
|
ListGroupItem,
|
||||||
|
Spinner,
|
||||||
|
} from 'sveltestrap';
|
||||||
|
|
||||||
|
import { getTrack } from '../../../../lib/track';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#await getTrack($page.params.tid)}
|
||||||
|
<div class="d-flex flex-fill justify-content-center align-items-center gap-2">
|
||||||
|
<Spinner color="primary" /> Chargement en cours…
|
||||||
|
</div>
|
||||||
|
{:then track}
|
||||||
|
<Container>
|
||||||
|
<h2>
|
||||||
|
{track.name}
|
||||||
|
</h2>
|
||||||
|
<ListGroup>
|
||||||
|
<ListGroupItem>
|
||||||
|
<strong>Chemin</strong>
|
||||||
|
{track.path}
|
||||||
|
</ListGroupItem>
|
||||||
|
<ListGroupItem class="d-flex gap-2">
|
||||||
|
<strong>Active ?</strong>
|
||||||
|
<Input type="switch" on:change={() => track.toggleEnable()} checked={track.enabled} />
|
||||||
|
</ListGroupItem>
|
||||||
|
<ListGroupItem>
|
||||||
|
<strong>ID</strong>
|
||||||
|
<span class="text-muted">{track.id}</span>
|
||||||
|
</ListGroupItem>
|
||||||
|
</ListGroup>
|
||||||
|
</Container>
|
||||||
|
{/await}
|
||||||
|
36
ui/src/stores/tracks.js
Normal file
36
ui/src/stores/tracks.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
import { getTracks } from '../lib/track'
|
||||||
|
|
||||||
|
function createTracksStore() {
|
||||||
|
const { subscribe, set, update } = writable({list: null});
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
|
||||||
|
set: (v) => {
|
||||||
|
update((m) => Object.assign(m, v));
|
||||||
|
},
|
||||||
|
|
||||||
|
refresh: async () => {
|
||||||
|
const list = await getTracks();
|
||||||
|
update((m) => Object.assign(m, {list}));
|
||||||
|
return list;
|
||||||
|
},
|
||||||
|
|
||||||
|
update: (res_tracks, cb=null) => {
|
||||||
|
if (res_tracks.status === 200) {
|
||||||
|
res_tracks.json().then((list) => {
|
||||||
|
update((m) => (Object.assign(m, {list})));
|
||||||
|
|
||||||
|
if (cb) {
|
||||||
|
cb(list);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export const tracks = createTracksStore();
|
Loading…
x
Reference in New Issue
Block a user