fix: handle close errors
This commit is contained in:
@@ -88,7 +88,7 @@ func (c *allAnimeClient) graphqlRequest(ctx context.Context, query string, varia
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("execute graphql request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
|
||||
if err != nil {
|
||||
@@ -143,7 +143,7 @@ func (c *allAnimeClient) graphqlRequestWithHash(ctx context.Context, showID, epi
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("execute GET request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1022))
|
||||
if err != nil {
|
||||
|
||||
@@ -243,7 +243,7 @@ func (h *Handler) HandleProxy(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
if bodyReader != nil {
|
||||
defer bodyReader.Close()
|
||||
defer func() { _ = bodyReader.Close() }()
|
||||
_, _ = io.Copy(w, bodyReader)
|
||||
} else {
|
||||
_, _ = w.Write(content)
|
||||
@@ -389,7 +389,7 @@ func (h *Handler) HandleEpisodeData(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
if err := writeJSON(w, map[string]any{
|
||||
"mal_id": watchData.MalID,
|
||||
"title": watchData.Title,
|
||||
"current_episode": watchData.CurrentEpisode,
|
||||
@@ -400,7 +400,9 @@ func (h *Handler) HandleEpisodeData(w http.ResponseWriter, r *http.Request) {
|
||||
"mode_sources": watchData.ModeSources,
|
||||
"segments": watchData.Segments,
|
||||
"episode_title": "", // Find episode title if possible
|
||||
})
|
||||
}); err != nil {
|
||||
log.Printf("watch page encode error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleEpisodeThumbnails returns episode list for the thumbnail strip.
|
||||
@@ -459,5 +461,11 @@ func (h *Handler) HandleEpisodeThumbnails(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
if err := writeJSON(w, results); err != nil {
|
||||
log.Printf("thumbnails encode error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) error {
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func (s *Service) SaveProgress(ctx context.Context, userID string, animeID int64
|
||||
return err
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if animeSeed != nil {
|
||||
if _, err := txQueries.UpsertAnime(ctx, *animeSeed); err != nil {
|
||||
@@ -89,7 +89,7 @@ func (s *Service) CompleteAnime(ctx context.Context, userID string, animeID int6
|
||||
return err
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
watchListEntry, watchListErr := txQueries.GetWatchListEntry(ctx, db.GetWatchListEntryParams{
|
||||
UserID: userID,
|
||||
|
||||
@@ -51,7 +51,7 @@ func (e *providerExtractor) ExtractVideoLinks(ctx context.Context, providerPath
|
||||
}
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024)) // 2MB limit
|
||||
if err != nil {
|
||||
@@ -155,7 +155,7 @@ func (e *providerExtractor) parseM3U8(ctx context.Context, masterURL string, ref
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024)) // 512KB limit
|
||||
if err != nil {
|
||||
|
||||
@@ -23,7 +23,7 @@ func (s *Service) fetchSkipSegments(ctx context.Context, malID int, episode stri
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil
|
||||
|
||||
@@ -59,7 +59,7 @@ func (s *Service) handleProxyResponse(ctx context.Context, resp *http.Response,
|
||||
|
||||
// check if response is an m3u8 playlist that needs rewriting
|
||||
if isM3U8(targetURL, resp.Header.Get("Content-Type")) {
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
|
||||
if readErr != nil {
|
||||
return 0, nil, nil, nil, fmt.Errorf("read playlist failed: %w", readErr)
|
||||
|
||||
@@ -140,7 +140,7 @@ func (s *Service) probeDirectMedia(ctx context.Context, source StreamSource) (bo
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
// check content-type header first
|
||||
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
|
||||
@@ -192,7 +192,7 @@ func (s *Service) probeEmbedSource(ctx context.Context, source StreamSource) boo
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user