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"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-12-10 04:42:29 +00:00
|
|
|
minio "github.com/minio/minio-go"
|
2017-12-04 20:07:13 +00:00
|
|
|
)
|
|
|
|
|
2018-12-10 04:42:29 +00:00
|
|
|
var spaces *minio.Client
|
|
|
|
|
|
|
|
func init() {
|
2018-12-10 09:25:51 +00:00
|
|
|
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.
|
|
|
|
func Get(ctx *aero.Context) string {
|
|
|
|
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
|
|
|
}
|