Compare commits

..

4 commits

11 changed files with 60 additions and 55 deletions

View file

@ -2,7 +2,6 @@ package api
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -173,7 +172,7 @@ func getSession(app *model.AppState, r *http.Request) (*model.Session, error) {
// check cookies first // check cookies first
sessionCookie, err := r.Cookie(model.COOKIE_TOKEN) sessionCookie, err := r.Cookie(model.COOKIE_TOKEN)
if err != nil && err != http.ErrNoCookie { if err != nil && err != http.ErrNoCookie {
return nil, errors.New(fmt.Sprintf("Failed to retrieve session cookie: %v\n", err)) return nil, fmt.Errorf("Failed to retrieve session cookie: %v\n", err)
} }
if sessionCookie != nil { if sessionCookie != nil {
token = sessionCookie.Value token = sessionCookie.Value
@ -188,7 +187,7 @@ func getSession(app *model.AppState, r *http.Request) (*model.Session, error) {
session, err := controller.GetSession(app.DB, token) session, err := controller.GetSession(app.DB, token)
if err != nil && !strings.Contains(err.Error(), "no rows") { if err != nil && !strings.Contains(err.Error(), "no rows") {
return nil, errors.New(fmt.Sprintf("Failed to retrieve session: %v\n", err)) return nil, fmt.Errorf("Failed to retrieve session: %v\n", err)
} }
if session != nil { if session != nil {

View file

@ -50,7 +50,7 @@ func HandleImageUpload(app *model.AppState, data *string, directory string, file
return "", nil return "", nil
} }
app.Log.Info(log.TYPE_FILES, "\"%s/%s.%s\" created.", directory, filename, ext) app.Log.Info(log.TYPE_FILES, "\"%s\" created.", imagePath)
return filename, nil return filename, nil
} }

View file

@ -1,14 +1,15 @@
package controller package controller
import ( import (
"fmt" "embed"
"os" "fmt"
"time" "os"
"time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
const DB_VERSION int = 4 const DB_VERSION int = 5
func CheckDBVersionAndMigrate(db *sqlx.DB) { func CheckDBVersionAndMigrate(db *sqlx.DB) {
db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody") db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody")
@ -49,16 +50,23 @@ func CheckDBVersionAndMigrate(db *sqlx.DB) {
ApplyMigration(db, "003-fail-lock") ApplyMigration(db, "003-fail-lock")
oldDBVersion = 4 oldDBVersion = 4
case 4:
ApplyMigration(db, "004-test")
oldDBVersion = 5
} }
} }
fmt.Printf("Database schema up to date.\n") fmt.Printf("Database schema up to date.\n")
} }
//go:embed "schema-migration"
var schemaFS embed.FS
func ApplyMigration(db *sqlx.DB, scriptFile string) { func ApplyMigration(db *sqlx.DB, scriptFile string) {
fmt.Printf("Applying schema migration %s...\n", scriptFile) fmt.Printf("Applying schema migration %s...\n", scriptFile)
bytes, err := os.ReadFile("schema-migration/" + scriptFile + ".sql") bytes, err := schemaFS.ReadFile("schema-migration/" + scriptFile + ".sql")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "FATAL: Failed to open schema file \"%s\": %v\n", scriptFile, err) fmt.Fprintf(os.Stderr, "FATAL: Failed to open schema file \"%s\": %v\n", scriptFile, err)
os.Exit(1) os.Exit(1)

View file

@ -2,8 +2,8 @@ package controller
import ( import (
"encoding/base64" "encoding/base64"
"image" // "image"
"image/color" // "image/color"
"github.com/skip2/go-qrcode" "github.com/skip2/go-qrcode"
) )
@ -18,36 +18,35 @@ func GenerateQRCode(data string) (string, error) {
} }
// vvv DEPRECATED vvv // vvv DEPRECATED vvv
// const margin = 4
const margin = 4 //
// type QRCodeECCLevel int64
type QRCodeECCLevel int64 // const (
const ( // LOW QRCodeECCLevel = iota
LOW QRCodeECCLevel = iota // MEDIUM
MEDIUM // QUARTILE
QUARTILE // HIGH
HIGH // )
) //
// func drawLargeAlignmentSquare(x int, y int, img *image.Gray) {
func drawLargeAlignmentSquare(x int, y int, img *image.Gray) { // for yi := range 7 {
for yi := range 7 { // for xi := range 7 {
for xi := range 7 { // if (xi == 0 || xi == 6) || (yi == 0 || yi == 6) {
if (xi == 0 || xi == 6) || (yi == 0 || yi == 6) { // img.Set(x + xi, y + yi, color.Black)
img.Set(x + xi, y + yi, color.Black) // } else if (xi > 1 && xi < 5) && (yi > 1 && yi < 5) {
} else if (xi > 1 && xi < 5) && (yi > 1 && yi < 5) { // img.Set(x + xi, y + yi, color.Black)
img.Set(x + xi, y + yi, color.Black) // }
} // }
} // }
} // }
} //
// func drawSmallAlignmentSquare(x int, y int, img *image.Gray) {
func drawSmallAlignmentSquare(x int, y int, img *image.Gray) { // for yi := range 5 {
for yi := range 5 { // for xi := range 5 {
for xi := range 5 { // if (xi == 0 || xi == 4) || (yi == 0 || yi == 4) {
if (xi == 0 || xi == 4) || (yi == 0 || yi == 4) { // img.Set(x + xi, y + yi, color.Black)
img.Set(x + xi, y + yi, color.Black) // }
} // }
} // }
} // img.Set(x + 2, y + 2, color.Black)
img.Set(x + 2, y + 2, color.Black) // }
}

View file

@ -1,7 +1,6 @@
package controller package controller
import ( import (
"errors"
"fmt" "fmt"
"arimelody-web/model" "arimelody-web/model"
@ -21,7 +20,7 @@ func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
// get credits // get credits
credits, err := GetReleaseCredits(db, id) credits, err := GetReleaseCredits(db, id)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Credits: %s", err)) return nil, fmt.Errorf("Credits: %s", err)
} }
for _, credit := range credits { for _, credit := range credits {
release.Credits = append(release.Credits, credit) release.Credits = append(release.Credits, credit)
@ -30,7 +29,7 @@ func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
// get tracks // get tracks
tracks, err := GetReleaseTracks(db, id) tracks, err := GetReleaseTracks(db, id)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Tracks: %s", err)) return nil, fmt.Errorf("Tracks: %s", err)
} }
for _, track := range tracks { for _, track := range tracks {
release.Tracks = append(release.Tracks, track) release.Tracks = append(release.Tracks, track)
@ -39,7 +38,7 @@ func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
// get links // get links
links, err := GetReleaseLinks(db, id) links, err := GetReleaseLinks(db, id)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Links: %s", err)) return nil, fmt.Errorf("Links: %s", err)
} }
for _, link := range links { for _, link := range links {
release.Links = append(release.Links, link) release.Links = append(release.Links, link)
@ -71,7 +70,7 @@ func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*mod
// get credits // get credits
credits, err := GetReleaseCredits(db, release.ID) credits, err := GetReleaseCredits(db, release.ID)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Credits: %s", err)) return nil, fmt.Errorf("Credits: %s", err)
} }
for _, credit := range credits { for _, credit := range credits {
release.Credits = append(release.Credits, credit) release.Credits = append(release.Credits, credit)
@ -81,7 +80,7 @@ func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*mod
// get tracks // get tracks
tracks, err := GetReleaseTracks(db, release.ID) tracks, err := GetReleaseTracks(db, release.ID)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Tracks: %s", err)) return nil, fmt.Errorf("Tracks: %s", err)
} }
for _, track := range tracks { for _, track := range tracks {
release.Tracks = append(release.Tracks, track) release.Tracks = append(release.Tracks, track)
@ -90,7 +89,7 @@ func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*mod
// get links // get links
links, err := GetReleaseLinks(db, release.ID) links, err := GetReleaseLinks(db, release.ID)
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("Links: %s", err)) return nil, fmt.Errorf("Links: %s", err)
} }
for _, link := range links { for _, link := range links {
release.Links = append(release.Links, link) release.Links = append(release.Links, link)

View file

@ -0,0 +1 @@
INSERT INTO arimelody.auditlog (level, type, content) VALUES (0, "test", "this is a db schema migration test!");

View file

@ -2,7 +2,6 @@ package controller
import ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -19,7 +18,7 @@ const TOKEN_LEN = 64
func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session, error) { func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session, error) {
sessionCookie, err := r.Cookie(model.COOKIE_TOKEN) sessionCookie, err := r.Cookie(model.COOKIE_TOKEN)
if err != nil && err != http.ErrNoCookie { if err != nil && err != http.ErrNoCookie {
return nil, errors.New(fmt.Sprintf("Failed to retrieve session cookie: %v", err)) return nil, fmt.Errorf("Failed to retrieve session cookie: %v", err)
} }
var session *model.Session var session *model.Session
@ -29,7 +28,7 @@ func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session
session, err = GetSession(app.DB, sessionCookie.Value) session, err = GetSession(app.DB, sessionCookie.Value)
if err != nil && !strings.Contains(err.Error(), "no rows") { if err != nil && !strings.Contains(err.Error(), "no rows") {
return nil, errors.New(fmt.Sprintf("Failed to retrieve session: %v", err)) return nil, fmt.Errorf("Failed to retrieve session: %v", err)
} }
if session != nil { if session != nil {