76 lines
1.7 KiB
Go
Raw Normal View History

2017-12-04 20:07:13 +00:00
package episode
import (
2018-12-10 04:42:29 +00:00
"fmt"
"log"
2017-12-04 20:07:13 +00:00
"net/http"
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2017-12-04 20:07:13 +00:00
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
2019-06-03 11:17:09 +00:00
minio "github.com/minio/minio-go/v6"
2017-12-04 20:07:13 +00:00
)
2018-12-10 04:42:29 +00:00
var spaces *minio.Client
func init() {
if arn.APIKeys.S3.ID == "" || arn.APIKeys.S3.Secret == "" {
return
}
2018-12-10 04:42:29 +00:00
go func() {
var err error
endpoint := "sfo2.digitaloceanspaces.com"
ssl := true
// Initiate a client using DigitalOcean Spaces.
spaces, err = minio.New(endpoint, arn.APIKeys.S3.ID, arn.APIKeys.S3.Secret, ssl)
if err != nil {
log.Fatal(err)
}
}()
}
2017-12-04 20:07:13 +00:00
// Get renders the anime episode.
2019-06-01 04:55:49 +00:00
func Get(ctx aero.Context) error {
2017-12-04 20:07:13 +00:00
user := utils.GetUser(ctx)
id := ctx.Get("id")
episodeNumber, err := ctx.GetInt("episode-number")
if err != nil {
return ctx.Error(http.StatusBadRequest, "Episode is not a number", err)
}
// Get anime
anime, err := arn.GetAnime(id)
if err != nil {
return ctx.Error(http.StatusNotFound, "Anime not found", err)
}
// Get anime episodes
animeEpisodes, err := arn.GetAnimeEpisodes(id)
if err != nil {
return ctx.Error(http.StatusNotFound, "Anime episodes not found", err)
}
2018-12-10 04:42:29 +00:00
// Does the episode exist?
uploaded := false
if spaces != nil {
stat, err := spaces.StatObject("arn", fmt.Sprintf("videos/anime/%s/%d.webm", anime.ID, episodeNumber), minio.StatObjectOptions{})
uploaded = (err == nil) && (stat.Size > 0)
}
2018-07-07 05:24:34 +00:00
episode, episodeIndex := animeEpisodes.Find(episodeNumber)
2017-12-04 20:07:13 +00:00
if episode == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusNotFound, "Anime episode not found")
2017-12-04 20:07:13 +00:00
}
2018-12-10 04:42:29 +00:00
return ctx.HTML(components.AnimeEpisode(anime, episode, episodeIndex, uploaded, user))
2017-12-04 20:07:13 +00:00
}