github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume4/section2/gopherface/handlers/uploadvideo.go (about)

     1  package handlers
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common/utility"
    14  )
    15  
    16  type UploadVideoForm struct {
    17  	FieldNames []string
    18  	Fields     map[string]string
    19  	Errors     map[string]string
    20  }
    21  
    22  func DisplayUploadVideoForm(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
    23  	RenderTemplate(w, WebAppRoot+"/templates/uploadvideoform.html", u)
    24  }
    25  
    26  func ProcessUploadVideo(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
    27  
    28  	file, fileheader, err := r.FormFile("videofile")
    29  
    30  	if err != nil {
    31  		log.Println("Encountered error when attempting to read uploaded file: ", err)
    32  	}
    33  
    34  	randomFileName := utility.GenerateUUID()
    35  
    36  	if fileheader != nil {
    37  
    38  		extension := filepath.Ext(fileheader.Filename)
    39  		r.ParseMultipartForm(32 << 20)
    40  
    41  		defer file.Close()
    42  
    43  		videoFilePathWithoutExtension := "./static/uploads/videos/" + randomFileName
    44  		f, err := os.OpenFile(videoFilePathWithoutExtension+extension, os.O_WRONLY|os.O_CREATE, 0666)
    45  
    46  		if err != nil {
    47  			log.Println(err)
    48  			return
    49  		}
    50  
    51  		defer f.Close()
    52  		io.Copy(f, file)
    53  
    54  		thumbImageFilePath := videoFilePathWithoutExtension + "_thumb.png"
    55  		command := "ffmpeg -y  -i " + videoFilePathWithoutExtension + extension + " -s 430x372 -f mjpeg -vframes 1 -ss 3 " + thumbImageFilePath
    56  		commandOutput, err := exec.Command("sh", "-c", command).Output()
    57  		fmt.Println("video thumbnail generation command output: ", commandOutput)
    58  
    59  		if err != nil {
    60  			log.Println(err)
    61  			return
    62  		}
    63  
    64  		m := make(map[string]string)
    65  		m["thumbnailPath"] = strings.TrimPrefix(videoFilePathWithoutExtension, ".") + "_thumb.png"
    66  		m["videoPath"] = strings.TrimPrefix(videoFilePathWithoutExtension, ".") + ".mp4"
    67  
    68  		RenderTemplate(w, WebAppRoot+"/templates/videopreview.html", m)
    69  
    70  	} else {
    71  		w.Write([]byte("Failed to process uploaded file!"))
    72  	}
    73  }
    74  
    75  func ValidateUploadVideoForm(w http.ResponseWriter, r *http.Request, u *UploadVideoForm) {
    76  
    77  	ProcessUploadVideo(w, r, u)
    78  
    79  }
    80  
    81  func UploadVideoHandler(w http.ResponseWriter, r *http.Request) {
    82  
    83  	u := UploadVideoForm{}
    84  	u.Fields = make(map[string]string)
    85  	u.Errors = make(map[string]string)
    86  
    87  	switch r.Method {
    88  
    89  	case "GET":
    90  		DisplayUploadVideoForm(w, r, &u)
    91  	case "POST":
    92  		ValidateUploadVideoForm(w, r, &u)
    93  	default:
    94  		DisplayUploadVideoForm(w, r, &u)
    95  	}
    96  
    97  }