github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section4/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  		return
    32  	}
    33  
    34  	randomFileName := 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  		imageFilePathWithoutExtension := "./static/uploads/images/" + randomFileName
    44  		f, err := os.OpenFile(imageFilePathWithoutExtension+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 := imageFilePathWithoutExtension + "_thumb.png"
    55  		originalimagefile, err := os.Open(imageFilePathWithoutExtension + extension)
    56  
    57  		if err != nil {
    58  			log.Println(err)
    59  			return
    60  		}
    61  
    62  		img, err := png.Decode(originalimagefile)
    63  
    64  		if err != nil {
    65  			log.Println("Encountered Error while decoding image file: ", err)
    66  			return
    67  		}
    68  
    69  		thumbImage := resize.Resize(270, 0, img, resize.Lanczos3)
    70  		thumbImageFile, err := os.Create(thumbImageFilePath)
    71  
    72  		if err != nil {
    73  			log.Println("Encountered error while resizing image:", err)
    74  			return
    75  		}
    76  
    77  		defer thumbImageFile.Close()
    78  
    79  		png.Encode(thumbImageFile, thumbImage)
    80  
    81  		m := make(map[string]string)
    82  		m["thumbnailPath"] = strings.TrimPrefix(imageFilePathWithoutExtension, ".") + "_thumb.png"
    83  		m["imagePath"] = strings.TrimPrefix(imageFilePathWithoutExtension, ".") + ".png"
    84  
    85  		RenderTemplate(w, "./templates/imagepreview.html", m)
    86  
    87  	} else {
    88  		w.Write([]byte("Failed to process uploaded file!"))
    89  	}
    90  }
    91  
    92  func ValidateUploadImageForm(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    93  
    94  	ProcessUploadImage(w, r, u)
    95  
    96  }
    97  
    98  func UploadImageHandler(w http.ResponseWriter, r *http.Request) {
    99  
   100  	u := UploadImageForm{}
   101  	u.Fields = make(map[string]string)
   102  	u.Errors = make(map[string]string)
   103  
   104  	switch r.Method {
   105  
   106  	case "GET":
   107  		DisplayUploadImageForm(w, r, &u)
   108  	case "POST":
   109  		ValidateUploadImageForm(w, r, &u)
   110  	default:
   111  		DisplayUploadImageForm(w, r, &u)
   112  	}
   113  
   114  }