Handle gongs
This commit is contained in:
parent
5c7841fdc6
commit
b2d50972ed
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
gongs
|
||||||
reveil
|
reveil
|
||||||
tracks
|
tracks
|
||||||
vendor
|
vendor
|
||||||
|
77
api/gongs.go
77
api/gongs.go
@ -1,37 +1,92 @@
|
|||||||
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 declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||||
router.GET("/gongs", func(c *gin.Context) {
|
router.GET("/gongs", func(c *gin.Context) {
|
||||||
|
gongs, err := reveil.LoadGongs(cfg)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gongs)
|
||||||
})
|
})
|
||||||
router.POST("/gongs", func(c *gin.Context) {
|
router.POST("/gongs", func(c *gin.Context) {
|
||||||
|
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||||
})
|
})
|
||||||
|
|
||||||
gongsRoutes := router.Group("/gongs/:gid")
|
gongsRoutes := router.Group("/gongs/:tid")
|
||||||
gongsRoutes.Use(gongHandler)
|
gongsRoutes.Use(func(c *gin.Context) {
|
||||||
|
gongs, err := reveil.LoadGongs(cfg)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, t := range gongs {
|
||||||
|
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
|
||||||
|
c.Set("gong", t)
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Gong not found"})
|
||||||
|
})
|
||||||
|
|
||||||
gongsRoutes.GET("", func(c *gin.Context) {
|
gongsRoutes.GET("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
c.JSON(http.StatusOK, c.MustGet("gong"))
|
||||||
})
|
})
|
||||||
gongsRoutes.PUT("", func(c *gin.Context) {
|
gongsRoutes.PUT("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
oldgong := c.MustGet("gong").(*reveil.Gong)
|
||||||
|
|
||||||
|
var gong reveil.Gong
|
||||||
|
if err := c.ShouldBindJSON(&gong); err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if gong.Name != oldgong.Name {
|
||||||
|
err := oldgong.Rename(gong.Name)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the gong: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if gong.Enabled != oldgong.Enabled {
|
||||||
|
var err error
|
||||||
|
if gong.Enabled {
|
||||||
|
err = oldgong.SetDefault(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to set the new default gong: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, oldgong)
|
||||||
})
|
})
|
||||||
gongsRoutes.DELETE("", func(c *gin.Context) {
|
gongsRoutes.DELETE("", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, c.MustGet("gong"))
|
gong := c.MustGet("gong").(*reveil.Gong)
|
||||||
|
|
||||||
|
err := gong.Remove()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the gong: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func gongHandler(c *gin.Context) {
|
|
||||||
c.Set("gong", nil)
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
}
|
|
||||||
|
86
model/gong.go
Normal file
86
model/gong.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package reveil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha512"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
const CURRENT_GONG = "_current_gong"
|
||||||
|
|
||||||
|
type Gong struct {
|
||||||
|
Id []byte `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func currentGongPath(cfg *config.Config) string {
|
||||||
|
return filepath.Join(cfg.GongsDir, CURRENT_GONG)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) {
|
||||||
|
// Retrieve the path of the current gong
|
||||||
|
current_gong, err := os.Readlink(currentGongPath(cfg))
|
||||||
|
if err == nil {
|
||||||
|
current_gong, _ = filepath.Abs(filepath.Join(cfg.GongsDir, current_gong))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve the list
|
||||||
|
err = filepath.Walk(cfg.GongsDir, func(path string, d fs.FileInfo, err error) error {
|
||||||
|
if d.Mode().IsRegular() {
|
||||||
|
hash := sha512.Sum512([]byte(path))
|
||||||
|
pabs, _ := filepath.Abs(path)
|
||||||
|
gongs = append(gongs, &Gong{
|
||||||
|
Id: hash[:63],
|
||||||
|
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||||
|
Path: path,
|
||||||
|
Enabled: current_gong == pabs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gong) Rename(newName string) error {
|
||||||
|
newPath := filepath.Join(filepath.Dir(g.Path), newName+filepath.Ext(g.Path))
|
||||||
|
|
||||||
|
err := os.Rename(
|
||||||
|
g.Path,
|
||||||
|
newPath,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
g.Path = newPath
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gong) SetDefault(cfg *config.Config) error {
|
||||||
|
linkpath := currentGongPath(cfg)
|
||||||
|
os.Remove(linkpath)
|
||||||
|
|
||||||
|
pabs, err := filepath.Abs(g.Path)
|
||||||
|
if err != nil {
|
||||||
|
pabs = g.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
gdirabs, err := filepath.Abs(cfg.GongsDir)
|
||||||
|
if err != nil {
|
||||||
|
gdirabs = cfg.GongsDir
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.Symlink(strings.TrimPrefix(strings.TrimPrefix(pabs, gdirabs), "/"), linkpath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gong) Remove() error {
|
||||||
|
return os.Remove(g.Path)
|
||||||
|
}
|
@ -5,29 +5,15 @@
|
|||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Icon,
|
Icon,
|
||||||
|
Spinner,
|
||||||
} from 'sveltestrap';
|
} from 'sveltestrap';
|
||||||
|
|
||||||
let gongs = [
|
import { gongs } from '../stores/gongs';
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
title: "Coq",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
title: "Marseillaise",
|
|
||||||
enabled: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
title: "Trompette de l'armée française",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
function chooseGong(gong) {
|
function chooseGong(gong) {
|
||||||
gongs = gongs.map((g) => {
|
gong.setDefault().then(() => {
|
||||||
g.enabled = g.id == gong.id
|
gongs.refresh();
|
||||||
return g;
|
});
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export let flush = false;
|
export let flush = false;
|
||||||
@ -35,6 +21,14 @@
|
|||||||
|
|
||||||
export { className as class };
|
export { className as class };
|
||||||
let className = '';
|
let className = '';
|
||||||
|
|
||||||
|
let refreshInProgress = false;
|
||||||
|
function refresh_gongs() {
|
||||||
|
refreshInProgress = true;
|
||||||
|
gongs.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}>
|
||||||
@ -61,27 +55,43 @@
|
|||||||
<Button
|
<Button
|
||||||
color="outline-dark"
|
color="outline-dark"
|
||||||
size="sm"
|
size="sm"
|
||||||
|
title="Rafraîchir la liste des gongs"
|
||||||
|
on:click={refresh_gongs}
|
||||||
|
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 gongs as gong (gong.id)}
|
{#if $gongs.list}
|
||||||
<button
|
{#each $gongs.list as gong (gong.id)}
|
||||||
type="button"
|
<button
|
||||||
class="list-group-item list-group-item-action"
|
type="button"
|
||||||
class:active={(edit && $page.url.pathname.indexOf('/gongs/') !== -1 && $page.params.gid == gong.id) || (!edit && gong.enabled)}
|
class="list-group-item list-group-item-action"
|
||||||
aria-current="true"
|
class:active={(edit && $page.url.pathname.indexOf('/gongs/') !== -1 && $page.params.gid == gong.id) || (!edit && gong.enabled)}
|
||||||
on:click={() => {
|
aria-current="true"
|
||||||
if (edit) {
|
on:click={() => {
|
||||||
goto('musiks/gongs/' + gong.id);
|
if (edit) {
|
||||||
} else {
|
goto('musiks/gongs/' + gong.id);
|
||||||
chooseGong(gong);
|
} else {
|
||||||
}
|
chooseGong(gong);
|
||||||
}}
|
}
|
||||||
>
|
}}
|
||||||
{gong.title}
|
>
|
||||||
</button>
|
{gong.name}
|
||||||
{/each}
|
</button>
|
||||||
|
{/each}
|
||||||
|
{:else}
|
||||||
|
{#await gongs.refresh()}
|
||||||
|
<div class="d-flex justify-content-center align-items-center gap-2">
|
||||||
|
<Spinner color="primary" /> Chargement en cours…
|
||||||
|
</div>
|
||||||
|
{:then gongs}
|
||||||
|
{/await}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
64
ui/src/lib/gong.js
Normal file
64
ui/src/lib/gong.js
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
export class Gong {
|
||||||
|
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/gongs/${this.id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {'Accept': 'application/json'}
|
||||||
|
});
|
||||||
|
if (res.status == 200) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async setDefault() {
|
||||||
|
this.enabled = !this.enabled;
|
||||||
|
return await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
async save() {
|
||||||
|
const res = await fetch(this.id?`api/gongs/${this.id}`:'api/gongs', {
|
||||||
|
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 getGongs() {
|
||||||
|
const res = await fetch(`api/gongs`, {headers: {'Accept': 'application/json'}})
|
||||||
|
if (res.status == 200) {
|
||||||
|
return (await res.json()).map((g) => new Gong(g));
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGong(gid) {
|
||||||
|
const res = await fetch(`api/gongs/${gid}`, {headers: {'Accept': 'application/json'}})
|
||||||
|
if (res.status == 200) {
|
||||||
|
return new Gong(await res.json());
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,39 @@
|
|||||||
<h2>
|
<script>
|
||||||
Gong
|
import { page } from '$app/stores';
|
||||||
</h2>
|
|
||||||
|
import {
|
||||||
|
Container,
|
||||||
|
Input,
|
||||||
|
ListGroup,
|
||||||
|
ListGroupItem,
|
||||||
|
Spinner,
|
||||||
|
} from 'sveltestrap';
|
||||||
|
|
||||||
|
import { getGong } from '../../../../lib/gong';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#await getGong($page.params.gid)}
|
||||||
|
<div class="d-flex flex-fill justify-content-center align-items-center gap-2">
|
||||||
|
<Spinner color="primary" /> Chargement en cours…
|
||||||
|
</div>
|
||||||
|
{:then gong}
|
||||||
|
<Container>
|
||||||
|
<h2>
|
||||||
|
{gong.name}
|
||||||
|
</h2>
|
||||||
|
<ListGroup>
|
||||||
|
<ListGroupItem>
|
||||||
|
<strong>Chemin</strong>
|
||||||
|
{gong.path}
|
||||||
|
</ListGroupItem>
|
||||||
|
<ListGroupItem class="d-flex gap-2">
|
||||||
|
<strong>Par défaut ?</strong>
|
||||||
|
<Input type="switch" on:change={() => gong.setDefault()} checked={gong.enabled} disabled={gong.enabled} />
|
||||||
|
</ListGroupItem>
|
||||||
|
<ListGroupItem>
|
||||||
|
<strong>ID</strong>
|
||||||
|
<span class="text-muted">{gong.id}</span>
|
||||||
|
</ListGroupItem>
|
||||||
|
</ListGroup>
|
||||||
|
</Container>
|
||||||
|
{/await}
|
||||||
|
36
ui/src/stores/gongs.js
Normal file
36
ui/src/stores/gongs.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
import { getGongs } from '../lib/gong'
|
||||||
|
|
||||||
|
function createGongsStore() {
|
||||||
|
const { subscribe, set, update } = writable({list: null});
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
|
||||||
|
set: (v) => {
|
||||||
|
update((m) => Object.assign(m, v));
|
||||||
|
},
|
||||||
|
|
||||||
|
refresh: async () => {
|
||||||
|
const list = await getGongs();
|
||||||
|
update((m) => Object.assign(m, {list}));
|
||||||
|
return list;
|
||||||
|
},
|
||||||
|
|
||||||
|
update: (res_gongs, cb=null) => {
|
||||||
|
if (res_gongs.status === 200) {
|
||||||
|
res_gongs.json().then((list) => {
|
||||||
|
update((m) => (Object.assign(m, {list})));
|
||||||
|
|
||||||
|
if (cb) {
|
||||||
|
cb(list);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export const gongs = createGongsStore();
|
Loading…
x
Reference in New Issue
Block a user