refactor(ui): complete ui template migration and fix playback
This commit is contained in:
@@ -6,8 +6,10 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"mal/integrations/jikan"
|
||||
ctxpkg "mal/internal/context"
|
||||
"mal/internal/db"
|
||||
"mal/templates"
|
||||
)
|
||||
@@ -26,7 +28,9 @@ type quickSearchResult struct {
|
||||
|
||||
func renderNotFoundPage(r *http.Request, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", nil); err != nil {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
@@ -62,12 +66,63 @@ func (h *Handler) HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(animes.Animes) > 4 {
|
||||
animes.Animes = animes.Animes[:4]
|
||||
currentlyAiring, err := h.jikanClient.GetSeasonsNow(r.Context(), 1)
|
||||
if err != nil {
|
||||
log.Printf("seasons now error: %v", err)
|
||||
// non-fatal
|
||||
}
|
||||
|
||||
if len(animes.Animes) > 6 {
|
||||
animes.Animes = animes.Animes[:6]
|
||||
}
|
||||
if len(currentlyAiring.Animes) > 6 {
|
||||
currentlyAiring.Animes = currentlyAiring.Animes[:6]
|
||||
}
|
||||
|
||||
// Fetch continue watching if logged in (user ID in context, handle this safely)
|
||||
// We'll skip DB fetch for continue watching for now if it requires complex session parsing
|
||||
// Actually we should try to fetch it if we can.
|
||||
var cw []database.GetContinueWatchingEntriesRow
|
||||
user, userOk := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
if userOk && user != nil {
|
||||
cw, _ = h.db.GetContinueWatchingEntries(r.Context(), user.ID)
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "index.gohtml", map[string]any{
|
||||
"Animes": animes.Animes,
|
||||
"MostPopular": animes.Animes,
|
||||
"CurrentlyAiring": currentlyAiring.Animes,
|
||||
"ContinueWatching": cw,
|
||||
"User": user,
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleBrowse(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
|
||||
q := r.URL.Query().Get("q")
|
||||
animeType := r.URL.Query().Get("type")
|
||||
status := r.URL.Query().Get("status")
|
||||
orderBy := r.URL.Query().Get("order_by")
|
||||
sort := r.URL.Query().Get("sort")
|
||||
|
||||
res, err := h.jikanClient.SearchAdvanced(r.Context(), q, animeType, status, orderBy, sort, 1, 24)
|
||||
if err != nil {
|
||||
log.Printf("browse error: %v", err)
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "browse.gohtml", map[string]any{
|
||||
"User": user,
|
||||
"CurrentPath": r.URL.Path,
|
||||
"Query": q,
|
||||
"Type": animeType,
|
||||
"Status": status,
|
||||
"OrderBy": orderBy,
|
||||
"Sort": sort,
|
||||
"Animes": res.Animes,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
@@ -87,11 +142,65 @@ func (h *Handler) HandleAPICatalog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) {
|
||||
renderNotFoundPage(r, w)
|
||||
idStr := strings.TrimPrefix(r.URL.Path, "/anime/")
|
||||
idStr = strings.TrimSuffix(idStr, "/")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
renderNotFoundPage(r, w)
|
||||
return
|
||||
}
|
||||
|
||||
anime, err := h.jikanClient.GetAnimeByID(r.Context(), id)
|
||||
if err != nil {
|
||||
renderNotFoundPage(r, w)
|
||||
return
|
||||
}
|
||||
|
||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
|
||||
var status string
|
||||
if user != nil {
|
||||
entry, err := h.db.GetWatchListEntry(r.Context(), database.GetWatchListEntryParams{
|
||||
UserID: user.ID,
|
||||
AnimeID: int64(id),
|
||||
})
|
||||
if err == nil {
|
||||
status = entry.Status
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "anime.gohtml", map[string]any{
|
||||
"Anime": anime,
|
||||
"User": user,
|
||||
"Status": status,
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleAPIAnime(w http.ResponseWriter, r *http.Request) {
|
||||
renderNotFoundPage(r, w)
|
||||
func (h *Handler) HandleHTMLWatchOrder(w http.ResponseWriter, r *http.Request) {
|
||||
animeIdStr := r.URL.Query().Get("animeId")
|
||||
id, err := strconv.Atoi(animeIdStr)
|
||||
if err != nil {
|
||||
http.Error(w, `<div class="mt-8 text-sm text-red-400">Invalid anime ID.</div>`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
relations, err := h.jikanClient.GetFullRelations(r.Context(), id)
|
||||
if err != nil {
|
||||
log.Printf("watch order error: %v", err)
|
||||
http.Error(w, `<div class="mt-8 text-sm text-red-400">Failed to load watch order.</div>`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteFragment(w, "anime.gohtml", "watch_order", map[string]any{
|
||||
"Relations": relations,
|
||||
"AnimeID": id,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleAPIEpisodes(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -106,7 +215,7 @@ func (h *Handler) HandleQuickSearch(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode([]quickSearchResult{})
|
||||
return
|
||||
}
|
||||
res, err := h.jikanClient.SearchWithLimit(r.Context(), query, 1, 5)
|
||||
res, err := h.jikanClient.SearchAdvanced(r.Context(), query, "", "", "", "", 1, 5)
|
||||
if err != nil {
|
||||
log.Printf("quick search error: %v", err)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
@@ -25,21 +25,19 @@ func rateLimitErrorFromQuery(r *http.Request) string {
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
err := templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": rateLimitErrorFromQuery(r),
|
||||
"Username": "",
|
||||
})
|
||||
if err != nil {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "Something went wrong. Please try again.",
|
||||
"Username": "",
|
||||
"Error": "Something went wrong. Please try again.",
|
||||
"Username": "",
|
||||
"CurrentPath": r.URL.Path,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -49,8 +47,9 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if username == "" || password == "" {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"CurrentPath": r.URL.Path,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -58,8 +57,9 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := h.authService.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"CurrentPath": r.URL.Path,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package playback
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"mal/integrations/jikan"
|
||||
ctxpkg "mal/internal/context"
|
||||
"mal/internal/db"
|
||||
"mal/templates"
|
||||
)
|
||||
|
||||
@@ -17,14 +23,123 @@ func NewHandler(svc *Service, jikanClient *jikan.Client) *Handler {
|
||||
return &Handler{svc: svc, jikanClient: jikanClient}
|
||||
}
|
||||
|
||||
func renderNotFoundPage(r *http.Request, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleWatchPage(w http.ResponseWriter, r *http.Request) {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", nil); err != nil {
|
||||
// Path is like /anime/123/watch
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
if len(parts) < 4 {
|
||||
renderNotFoundPage(r, w)
|
||||
return
|
||||
}
|
||||
idStr := parts[2]
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
renderNotFoundPage(r, w)
|
||||
return
|
||||
}
|
||||
|
||||
anime, err := h.jikanClient.GetAnimeByID(r.Context(), id)
|
||||
if err != nil {
|
||||
renderNotFoundPage(r, w)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to get video episodes first (for thumbnails)
|
||||
episodes, err := h.jikanClient.GetVideoEpisodes(r.Context(), id, 1)
|
||||
if err != nil || len(episodes.Data) == 0 {
|
||||
// Fallback to standard episodes if no video episodes
|
||||
episodes, err = h.jikanClient.GetEpisodes(r.Context(), id, 1)
|
||||
if err != nil {
|
||||
log.Printf("watch error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
|
||||
currentEpID := r.URL.Query().Get("ep")
|
||||
if currentEpID == "" {
|
||||
currentEpID = "1"
|
||||
}
|
||||
|
||||
mode := r.URL.Query().Get("mode")
|
||||
userID := ""
|
||||
if user != nil {
|
||||
userID = user.ID
|
||||
}
|
||||
|
||||
titleCandidates := []string{anime.Title}
|
||||
if anime.TitleEnglish != "" && anime.TitleEnglish != anime.Title {
|
||||
titleCandidates = append(titleCandidates, anime.TitleEnglish)
|
||||
}
|
||||
if anime.TitleJapanese != "" {
|
||||
titleCandidates = append(titleCandidates, anime.TitleJapanese)
|
||||
}
|
||||
|
||||
watchData, err := h.svc.BuildWatchPageData(r.Context(), id, titleCandidates, currentEpID, mode, userID)
|
||||
if err != nil {
|
||||
log.Printf("watch data error: %v", err)
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "watch.gohtml", map[string]any{
|
||||
"Anime": anime,
|
||||
"Episodes": episodes.Data,
|
||||
"WatchData": watchData,
|
||||
"User": user,
|
||||
"CurrentPath": r.URL.Path,
|
||||
"CurrentEpID": currentEpID,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleProxy(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
||||
token := r.URL.Query().Get("token")
|
||||
if token == "" {
|
||||
http.Error(w, "missing token", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
scope := proxyScopeStream
|
||||
if strings.HasSuffix(r.URL.Path, "/segment") {
|
||||
scope = proxyScopeSegment
|
||||
} else if strings.HasSuffix(r.URL.Path, "/subtitle") {
|
||||
scope = proxyScopeSubtitle
|
||||
}
|
||||
|
||||
targetURL, referer, err := h.svc.resolveProxyToken(r.Context(), token, scope)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid token", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
rangeHeader := r.Header.Get("Range")
|
||||
|
||||
statusCode, headers, content, bodyReader, err := h.svc.ProxyStream(r.Context(), targetURL, referer, rangeHeader)
|
||||
if err != nil {
|
||||
log.Printf("proxy error for %s: %v", targetURL, err)
|
||||
http.Error(w, "proxy failed", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range headers {
|
||||
w.Header()[k] = v
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
if bodyReader != nil {
|
||||
defer bodyReader.Close()
|
||||
_, _ = io.Copy(w, bodyReader)
|
||||
} else {
|
||||
_, _ = w.Write(content)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleSaveProgress(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -36,5 +151,58 @@ func (h *Handler) HandleCompleteAnime(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) HandleEpisodeData(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
// /api/watch/episode/{animeId}/{episodeId}
|
||||
if len(parts) < 6 {
|
||||
http.Error(w, "invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
animeID, err := strconv.Atoi(parts[4])
|
||||
if err != nil {
|
||||
http.Error(w, "invalid animeId", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
episodeID := parts[5]
|
||||
|
||||
user, _ := r.Context().Value(ctxpkg.UserKey).(*database.User)
|
||||
userID := ""
|
||||
if user != nil {
|
||||
userID = user.ID
|
||||
}
|
||||
|
||||
anime, err := h.jikanClient.GetAnimeByID(r.Context(), animeID)
|
||||
if err != nil {
|
||||
http.Error(w, "anime not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
titleCandidates := []string{anime.Title}
|
||||
if anime.TitleEnglish != "" && anime.TitleEnglish != anime.Title {
|
||||
titleCandidates = append(titleCandidates, anime.TitleEnglish)
|
||||
}
|
||||
if anime.TitleJapanese != "" {
|
||||
titleCandidates = append(titleCandidates, anime.TitleJapanese)
|
||||
}
|
||||
|
||||
watchData, err := h.svc.BuildWatchPageData(r.Context(), animeID, titleCandidates, episodeID, "", userID)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to build watch data", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"mal_id": watchData.MalID,
|
||||
"title": watchData.Title,
|
||||
"current_episode": watchData.CurrentEpisode,
|
||||
"total_episodes": anime.Episodes,
|
||||
"initial_mode": watchData.InitialMode,
|
||||
"token": "", // The token might be per-source, wait, in Go it was per-mode?
|
||||
"available_modes": watchData.AvailableModes,
|
||||
"mode_sources": watchData.ModeSources,
|
||||
"segments": watchData.Segments,
|
||||
"episode_title": "", // Find episode title if possible
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,7 +32,9 @@ func (h *Handler) HandleDeleteContinueWatching(w http.ResponseWriter, r *http.Re
|
||||
}
|
||||
|
||||
func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", nil); err != nil {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user