~thecashewtrader/minv__server

e87e47e407c96929c5e26fcdaf8d4699699e26af — thecashewtrader 1 year, 5 months ago 107b7bb
Process the video asynchronously and add proper processing message
1 files changed, 32 insertions(+), 19 deletions(-)

M handlers/videos.go
M handlers/videos.go => handlers/videos.go +32 -19
@@ 5,6 5,7 @@ import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"strings"


@@ 120,23 121,27 @@ func CreateVideo(w http.ResponseWriter, r *http.Request) {
		Description: videoData.Description,
		Uploader:    videoData.Uploader,
	}

	for _, err := db.Queries.VideoInsertVideo(context.Background(), *insertVideoParams); err != nil; _, err = db.Queries.VideoInsertVideo(context.Background(), *insertVideoParams) {
	returnedVideo, err := db.Queries.VideoInsertVideo(context.Background(), *insertVideoParams)
	for ; err != nil; returnedVideo, err = db.Queries.VideoInsertVideo(context.Background(), *insertVideoParams) {
		slugSuffix += 1
		insertVideoParams.Slug = fmt.Sprintf("%s:%d", videoData.Slug, slugSuffix)
	}

	videoData.Slug = insertVideoParams.Slug
	go processVideo(returnedVideo, file)
	render.JSON(w, r, returnedVideo)
}

func processVideo(returnedVideo sqlc.Video, file multipart.File) {
	// Save the file

	// Create a temporary file within our temp-upload directory that follows a particular naming pattern
	tempFile, err := ioutil.TempFile("data/temp-upload", "upload-*")
	if err != nil {
		render.Status(r, http.StatusInternalServerError)
		render.JSON(w, r, render.M{
			"message": "Unexpected error",
			"error":   err.Error(),
		db.Queries.VideoUpdateVideo(context.Background(), sqlc.VideoUpdateVideoParams{
			Slug: returnedVideo.Slug,
			Title: returnedVideo.Title,
			Description: returnedVideo.Description,
			Processed: 2,
		})
		return
	}


@@ 145,10 150,11 @@ func CreateVideo(w http.ResponseWriter, r *http.Request) {
	// Read all of the contents of our uploaded file into a byte array
	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		render.Status(r, http.StatusInternalServerError)
		render.JSON(w, r, render.M{
			"message": "Unexpected error",
			"error":   err.Error(),
		db.Queries.VideoUpdateVideo(context.Background(), sqlc.VideoUpdateVideoParams{
			Slug: returnedVideo.Slug,
			Title: returnedVideo.Title,
			Description: returnedVideo.Description,
			Processed: 2,
		})
		return
	}


@@ 159,25 165,31 @@ func CreateVideo(w http.ResponseWriter, r *http.Request) {
	x, y := resolution[0], resolution[1]

	err = ffmpeg.Input(tempFile.Name()).
		Output(fmt.Sprintf("./data/videos/%s.mp4", videoData.Slug), ffmpeg.KwArgs{
		Output(fmt.Sprintf("./data/videos/%s.mp4", returnedVideo.Slug), ffmpeg.KwArgs{
			"c:v": "libx264",
			"vf":  fmt.Sprintf("scale=%d:%d:force_original_aspect_ratio=decrease,pad=%d:%d:(ow-iw)/2:(oh-ih)/2,setsar=1", x, y, x, y)}).
		OverWriteOutput().Run()

	if err != nil {
		render.Status(r, http.StatusInternalServerError)
		render.JSON(w, r, render.M{
			"message": "An error occured while transcoding",
			"error":   err.Error(),
		db.Queries.VideoUpdateVideo(context.Background(), sqlc.VideoUpdateVideoParams{
			Slug: returnedVideo.Slug,
			Title: returnedVideo.Title,
			Description: returnedVideo.Description,
			Processed: 2,
		})
		// Clean the DB incase of error
		db.Queries.VideoDeleteVideo(context.Background(), videoData.Slug)
		return
	}

	// Delete the temporary file and return the information
	os.Remove(tempFile.Name())
	render.JSON(w, r, videoData)

	// Video was processed successfully
	db.Queries.VideoUpdateVideo(context.Background(), sqlc.VideoUpdateVideoParams{
		Slug: returnedVideo.Slug,
		Title: returnedVideo.Title,
		Description: returnedVideo.Description,
		Processed: 1,
	})
}

func DeleteVideo(w http.ResponseWriter, r *http.Request) {


@@ 251,6 263,7 @@ func UpdateVideo(w http.ResponseWriter, r *http.Request) {
		Title:       returnedVideo.Title,
		Description: returnedVideo.Description,
		Slug:        returnedVideo.Slug,
		Processed: returnedVideo.Processed,
	}
	var body sqlc.VideoUpdateVideoParams
	if err := json.NewDecoder(r.Body).Decode(&body); err != nil {