Skip to content

Commit

Permalink
Merge pull request #85 from mbaraa/dev
Browse files Browse the repository at this point in the history
v0.1.5 release
  • Loading branch information
mbaraa authored Jun 30, 2024
2 parents 8c86664 + ddbf2ed commit 3e134aa
Show file tree
Hide file tree
Showing 37 changed files with 767 additions and 392 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ docker compose up -f docker-compose-dev.yml
- **This project is not affiliated with YouTube or Google, or anyone to that matter in any sort of ways.**
- Frank’s original image was taken from [dingusland.biz](https://dingusland.biz)
- Colorscheme is inspired from [Dankpods](https://www.youtube.com/@DankPods)
- Loader’s CSS was made by [@thamudi](https://github.com/thamudi)

---

Expand Down
124 changes: 0 additions & 124 deletions app/cmd/seeder/seeder.go

This file was deleted.

5 changes: 4 additions & 1 deletion app/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"dankmuzikk/handlers/pages"
"dankmuzikk/log"
"dankmuzikk/models"
"dankmuzikk/services/archive"
"dankmuzikk/services/history"
"dankmuzikk/services/jwt"
"dankmuzikk/services/login"
Expand Down Expand Up @@ -44,8 +45,9 @@ func StartServer(staticFS embed.FS) error {
historyRepo := db.NewBaseDB[models.History](dbConn)
playlistVotersRepo := db.NewBaseDB[models.PlaylistSongVoter](dbConn)

zipService := archive.NewService()
downloadService := download.New(songRepo)
playlistsService := playlists.New(playlistRepo, playlistOwnersRepo, playlistSongsRepo)
playlistsService := playlists.New(playlistRepo, playlistOwnersRepo, playlistSongsRepo, zipService)
songsService := songs.New(playlistSongsRepo, playlistOwnersRepo, songRepo, playlistRepo, playlistVotersRepo, downloadService)
historyService := history.New(historyRepo, songRepo)

Expand Down Expand Up @@ -112,6 +114,7 @@ func StartServer(staticFS embed.FS) error {
apisHandler.HandleFunc("PUT /playlist/public", gHandler.AuthApi(playlistsApi.HandleTogglePublicPlaylist))
apisHandler.HandleFunc("PUT /playlist/join", gHandler.AuthApi(playlistsApi.HandleToggleJoinPlaylist))
apisHandler.HandleFunc("DELETE /playlist", gHandler.AuthApi(playlistsApi.HandleDeletePlaylist))
apisHandler.HandleFunc("GET /playlist/zip", gHandler.AuthApi(playlistsApi.HandleDonwnloadPlaylist))
apisHandler.HandleFunc("GET /history/{page}", gHandler.AuthApi(historyApi.HandleGetMoreHistoryItems))

applicationHandler := http.NewServeMux()
Expand Down
32 changes: 30 additions & 2 deletions app/handlers/apis/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"dankmuzikk/services/playlists/songs"
"dankmuzikk/views/components/playlist"
"dankmuzikk/views/components/ui"
"dankmuzikk/views/icons"
"dankmuzikk/views/pages"
"encoding/json"
"io"
"net/http"
)

Expand Down Expand Up @@ -134,9 +136,11 @@ func (p *playlistApi) HandleToggleJoinPlaylist(w http.ResponseWriter, r *http.Re
}

if joined {
_, _ = w.Write([]byte("Leave playlist"))
_ = icons.SadFrog().Render(r.Context(), w)
_, _ = w.Write([]byte("<span>Leave playlist</span>"))
} else {
_, _ = w.Write([]byte("Join playlist"))
_ = icons.HappyFrog().Render(r.Context(), w)
_, _ = w.Write([]byte("<span>Join playlist</span>"))
}
}

Expand Down Expand Up @@ -208,3 +212,27 @@ func (p *playlistApi) HandleGetPlaylistsForPopover(w http.ResponseWriter, r *htt
playlist.PlaylistsSelector(songId, playlists, songsInPlaylists).
Render(r.Context(), w)
}

func (p *playlistApi) HandleDonwnloadPlaylist(w http.ResponseWriter, r *http.Request) {
profileId, profileIdCorrect := r.Context().Value(handlers.ProfileIdKey).(uint)
if !profileIdCorrect {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("🤷‍♂️"))
return
}

playlistId := r.URL.Query().Get("playlist-id")
if playlistId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}

playlistZip, err := p.service.Download(playlistId, profileId)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("🤷‍♂️"))
return
}
_, _ = io.Copy(w, playlistZip)
}
10 changes: 5 additions & 5 deletions app/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ func isMobile(r *http.Request) bool {
func getTheme(r *http.Request) string {
themeCookie, err := r.Cookie(ThemeName)
if err != nil || themeCookie == nil || themeCookie.Value == "" {
return "dank"
return "black"
}
switch themeCookie.Value {
case "black":
return "black"
case "dank":
return "dank"
case "white":
return "white"
case "dank":
case "black":
fallthrough
default:
return "dank"
return "black"
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"dankmuzikk/cmd/migrator"
"dankmuzikk/cmd/seeder"
"dankmuzikk/cmd/server"
"dankmuzikk/log"
"embed"
Expand All @@ -22,8 +21,6 @@ func main() {
err = server.StartServer(static)
case "migrate", "migration", "theotherthing":
err = migrator.Migrate()
case "seed", "seeder", "theotherotherthing":
err = seeder.SeedDb()
}
if err != nil {
log.Fatalln(log.ErrorLevel, err)
Expand Down
89 changes: 89 additions & 0 deletions app/services/archive/zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package archive

import (
"archive/zip"
"bytes"
"io"
"os"
)

const tmpDir = "/tmp"

type Service struct{}

func NewService() *Service {
return &Service{}
}

func (z *Service) CreateZip() (Archive, error) {
zipFile, err := os.CreateTemp(tmpDir, "playlist_*.zip")
if err != nil {
return nil, err
}
return newZip(zipFile), nil
}

type Archive interface {
AddFile(*os.File) error
RemoveFile(string) error
Deflate() (io.Reader, error)
}

type Zip struct {
files []*os.File
zipW *zip.Writer
zipF *os.File
}

func newZip(zipFile *os.File) *Zip {
zipWriter := zip.NewWriter(zipFile)
return &Zip{
zipF: zipFile,
zipW: zipWriter,
}
}

func (z *Zip) AddFile(f *os.File) error {
stat, err := f.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(stat)
if err != nil {
return err
}
header.Method = zip.Deflate
fileInArchive, err := z.zipW.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(fileInArchive, f)
if err != nil {
return err
}

return nil
}

func (z *Zip) RemoveFile(_ string) error {
panic("not implemented") // TODO: Implement
}

func (z *Zip) Deflate() (io.Reader, error) {
defer func() {
_ = z.zipF.Close()
_ = os.Remove(z.zipF.Name())
}()
_ = z.zipW.Flush()
_ = z.zipW.Close()

z.zipF.Seek(0, 0)

buf := bytes.NewBuffer([]byte{})
_, err := io.Copy(buf, z.zipF)
if err != nil {
return nil, err
}

return buf, nil
}
Loading

0 comments on commit 3e134aa

Please sign in to comment.