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