Skip to content

Commit

Permalink
removed slow download feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed May 28, 2024
1 parent 9c512e7 commit 4101df0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 91 deletions.
87 changes: 15 additions & 72 deletions controllers/DownloadVideoController.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -119,13 +117,9 @@ func DownloadVideoController(c echo.Context) error {
return nil
}

if *requestValidation.Stream {
if err := cmd.Wait(); err != nil {
c.Logger().Error("Failed to run cmd on wait", err)
return nil
}
} else {
defer cmd.Wait()
if err := cmd.Wait(); err != nil {
c.Logger().Error("Failed to run cmd on wait", err)
return nil
}

// wait until file exists
Expand All @@ -144,23 +138,12 @@ func DownloadVideoController(c echo.Context) error {
requestValidation.QUALITY,
)
} else {
var try = 0
for {
if try > 10 {
c.Logger().Error("Failed to receive output file from ffmpeg")
return nil
}
if tmpFile == nil {
f, err := os.Open(tmpFilePath)
if err != nil {
try++
time.Sleep(time.Second * 1)
continue
}
tmpFile = f
break
}
f, err := os.Open(tmpFilePath)
if err != nil {
c.Logger().Error("Failed to open tmp file", err)
return nil
}
tmpFile = f
fileName = fmt.Sprintf(
"%s[%s].mkv",
regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(dbLink.Name, "-"),
Expand All @@ -169,54 +152,14 @@ func DownloadVideoController(c echo.Context) error {
}
defer tmpFile.Close()

if *requestValidation.Stream {
if !*requestValidation.Stream {
defer os.Remove(tmpFilePath)
c.Response().Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
return c.Stream(http.StatusOK, "video/x-matroska", tmpFile)
} else {
c.Response().Header().Add("Accept-Ranges", "bytes")
http.ServeContent(c.Response(), c.Request(), fileName, time.Now(), tmpFile)
} else {
c.Response().Header().Add("Content-Type", "video/x-matroska")
c.Response().Header().Add("Transfer-Encoding", "chunked")
c.Response().Header().Add("Trailer", "AtEnd")
c.Response().Header().Add("Cache-Control", "no-cache")
c.Response().Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
c.Response().Status = http.StatusOK

var wg sync.WaitGroup
var written int64
var speedA int64 = 10 * 1024
var speedB int64 = 10 * 1024 * 1024
wg.Add(1)
go func() {
defer wg.Done()
for {
timeStart := time.Now().UnixMilli()
n, err := io.CopyN(c.Response().Writer, tmpFile, speedA)
if err != nil {
if err.Error() != "EOF" {
c.Logger().Error("Failed to write to buffer", err)
}
break
}
if n > 0 {
written = written + n
}
c.Response().Flush()
timeEnd := time.Now().UnixMilli()
timeDif := timeEnd - timeStart
// timeout 1 second minus the download time
time.Sleep(time.Second - (time.Millisecond * time.Duration(timeDif)))
// increase speed gradualy
if speedA < speedB {
speedA = speedA * 2
if speedA > speedB {
speedA = speedB
}
}
}
}()
wg.Wait()

cmd.Wait()
c.Response().Header().Set("Content-Length", fmt.Sprintf("%d", c.Response().Size))
defer os.Remove(tmpFilePath)
return nil
}
return nil
}
40 changes: 23 additions & 17 deletions controllers/PlayerController.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func PlayerController(c echo.Context) error {
var streamIsReady bool
var jsonQualitys []map[string]string
streamUrl := ""
streamUrlWidth := ""
streamUrlHeight := ""
for _, qualiItem := range dbLink.File.Qualitys {
if qualiItem.Ready {
streamIsReady = true
Expand All @@ -66,6 +68,8 @@ func PlayerController(c echo.Context) error {
"width": strconv.Itoa(int(qualiItem.Width)),
})
streamUrl = fmt.Sprintf("%s/%s/%s/download/video.mkv?stream=1&jwt=%s", config.ENV.FolderVideoQualitysPub, dbLink.UUID, qualiItem.Name, tkn)
streamUrlHeight = strconv.Itoa(int(qualiItem.Height))
streamUrlWidth = strconv.Itoa(int(qualiItem.Width))
}
}
rawQuality, _ := json.Marshal(jsonQualitys)
Expand Down Expand Up @@ -136,22 +140,24 @@ func PlayerController(c echo.Context) error {
// HTTPOnly: true,
// })
return c.Render(http.StatusOK, "player.html", echo.Map{
"Title": fmt.Sprintf("%s - %s", config.ENV.AppName, dbLink.Name),
"Description": fmt.Sprintf("Watch %s on %s", dbLink.Name, config.ENV.AppName),
"Thumbnail": fmt.Sprintf("%s/%s/image/thumb/%s", config.ENV.FolderVideoQualitysPub, dbLink.UUID, dbLink.File.Thumbnail),
"StreamUrl": template.HTML(streamUrl),
"Width": dbLink.File.Width,
"Height": dbLink.File.Height,
"Qualitys": string(rawQuality),
"Subtitles": string(rawSubtitles),
"Audios": string(rawAudios),
"AudioUUID": firstAudio,
"Webhooks": string(rawWebhooks),
"StreamIsReady": streamIsReady,
"UUID": requestValidation.UUID,
"PROJECTURL": config.ENV.Project,
"Folder": config.ENV.FolderVideoQualitysPub,
"JWT": tkn,
"AppName": config.ENV.AppName,
"Title": fmt.Sprintf("%s - %s", config.ENV.AppName, dbLink.Name),
"Description": fmt.Sprintf("Watch %s on %s", dbLink.Name, config.ENV.AppName),
"Thumbnail": fmt.Sprintf("%s/%s/image/thumb/%s", config.ENV.FolderVideoQualitysPub, dbLink.UUID, dbLink.File.Thumbnail),
"StreamUrl": template.HTML(streamUrl),
"StreamUrlWidth": streamUrlWidth,
"StreamUrlHeight": streamUrlHeight,
"Width": dbLink.File.Width,
"Height": dbLink.File.Height,
"Qualitys": string(rawQuality),
"Subtitles": string(rawSubtitles),
"Audios": string(rawAudios),
"AudioUUID": firstAudio,
"Webhooks": string(rawWebhooks),
"StreamIsReady": streamIsReady,
"UUID": requestValidation.UUID,
"PROJECTURL": config.ENV.Project,
"Folder": config.ENV.FolderVideoQualitysPub,
"JWT": tkn,
"AppName": config.ENV.AppName,
})
}
4 changes: 2 additions & 2 deletions views/player.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<meta property="og:url" content="/v/{{.UUID}}">
<meta property="og:video" content="{{.StreamUrl}}">
<meta property="og:video:secure_url" content="{{.StreamUrl}}">
<meta property="og:video:width" content="1920">
<meta property="og:video:height" content="1080">
<meta property="og:video:width" content="{{.StreamUrlWidth}}">
<meta property="og:video:height" content="{{.StreamUrlHeight}}">
<meta property="og:site_name" content="{{.AppName}}">
<title>{{ .Title }}</title>
<link rel="preconnect" href="https://cdn.jsdelivr.net" />
Expand Down

0 comments on commit 4101df0

Please sign in to comment.