github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section3/gopherfaceform/handlers/uploadimage.go (about)

     1  package handlers
     2  
     3  import (
     4  	"image/png"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/nfnt/resize"
    13  )
    14  
    15  type UploadImageForm struct {
    16  	FieldNames []string
    17  	Fields     map[string]string
    18  	Errors     map[string]string
    19  }
    20  
    21  func DisplayUploadImageForm(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    22  	RenderTemplate(w, "./templates/uploadimageform.html", u)
    23  }
    24  
    25  func ProcessUploadImage(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    26  
    27  	file, fileheader, err := r.FormFile("imagefile")
    28  
    29  	if err != nil {
    30  		log.Println("Encountered error when attempting to read uploaded file: ", err)
    31  	}
    32  
    33  	randomFileName := GenerateUUID()
    34  
    35  	if fileheader != nil {
    36  
    37  		extension := filepath.Ext(fileheader.Filename)
    38  		r.ParseMultipartForm(32 << 20)
    39  
    40  		defer file.Close()
    41  
    42  		imageFilePathWithoutExtension := "./static/uploads/images/" + randomFileName
    43  		f, err := os.OpenFile(imageFilePathWithoutExtension+extension, os.O_WRONLY|os.O_CREATE, 0666)
    44  
    45  		if err != nil {
    46  			log.Println(err)
    47  			return
    48  		}
    49  
    50  		defer f.Close()
    51  		io.Copy(f, file)
    52  
    53  		thumbImageFilePath := imageFilePathWithoutExtension + "_thumb.png"
    54  		originalimagefile, err := os.Open(imageFilePathWithoutExtension + extension)
    55  
    56  		if err != nil {
    57  			log.Println(err)
    58  			return
    59  		}
    60  
    61  		img, err := png.Decode(originalimagefile)
    62  
    63  		if err != nil {
    64  			log.Println("Encountered Error while decoding image file: ", err)
    65  		}
    66  
    67  		thumbImage := resize.Resize(270, 0, img, resize.Lanczos3)
    68  		thumbImageFile, err := os.Create(thumbImageFilePath)
    69  
    70  		if err != nil {
    71  			log.Println("Encountered error while resizing image:", err)
    72  		}
    73  
    74  		defer thumbImageFile.Close()
    75  
    76  		png.Encode(thumbImageFile, thumbImage)
    77  
    78  		m := make(map[string]string)
    79  		m["thumbnailPath"] = strings.TrimPrefix(imageFilePathWithoutExtension, ".") + "_thumb.png"
    80  		m["imagePath"] = strings.TrimPrefix(imageFilePathWithoutExtension, ".") + ".png"
    81  
    82  		RenderTemplate(w, "./templates/imagepreview.html", m)
    83  
    84  	} else {
    85  		w.Write([]byte("Failed to process uploaded file!"))
    86  	}
    87  }
    88  
    89  func ValidateUploadImageForm(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    90  
    91  	ProcessUploadImage(w, r, u)
    92  
    93  }
    94  
    95  func UploadImageHandler(w http.ResponseWriter, r *http.Request) {
    96  
    97  	u := UploadImageForm{}
    98  	u.Fields = make(map[string]string)
    99  	u.Errors = make(map[string]string)
   100  
   101  	switch r.Method {
   102  
   103  	case "GET":
   104  		DisplayUploadImageForm(w, r, &u)
   105  	case "POST":
   106  		ValidateUploadImageForm(w, r, &u)
   107  	default:
   108  		DisplayUploadImageForm(w, r, &u)
   109  	}
   110  
   111  }