github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section5/gopherfacedb/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/EngineerKamesh/gofullstack/volume2/section5/gopherfacedb/common/utility"
    13  
    14  	"github.com/nfnt/resize"
    15  )
    16  
    17  type UploadImageForm struct {
    18  	FieldNames []string
    19  	Fields     map[string]string
    20  	Errors     map[string]string
    21  }
    22  
    23  func DisplayUploadImageForm(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    24  	RenderTemplate(w, "./templates/uploadimageform.html", u)
    25  }
    26  
    27  func ProcessUploadImage(w http.ResponseWriter, r *http.Request, u *UploadImageForm) {
    28  
    29  	file, fileheader, err := r.FormFile("imagefile")
    30  
    31  	if err != nil {
    32  		log.Println("Encountered error when attempting to read uploaded file: ", err)
    33  	}
    34  
    35  	randomFileName := utility.GenerateUUID()
    36  
    37  	if fileheader != nil {
    38  
    39  		extension := filepath.Ext(fileheader.Filename)
    40  		r.ParseMultipartForm(32 << 20)
    41  
    42  		defer file.Close()
    43  
    44  		imageFilePathWithoutExtension := "./static/uploads/images/" + randomFileName
    45  		f, err := os.OpenFile(imageFilePathWithoutExtension+extension, os.O_WRONLY|os.O_CREATE, 0666)
    46  
    47  		if err != nil {
    48  			log.Println(err)
    49  			return
    50  		}
    51  
    52  		defer f.Close()
    53  		io.Copy(f, file)
    54  
    55  		thumbImageFilePath := imageFilePathWithoutExtension + "_thumb.png"
    56  		originalimagefile, err := os.Open(imageFilePathWithoutExtension + extension)
    57  
    58  		if err != nil {
    59  			log.Println(err)
    60  			return
    61  		}
    62  
    63  		img, err := png.Decode(originalimagefile)
    64  
    65  		if err != nil {
    66  			log.Println("Encountered Error while decoding image file: ", err)
    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  		}
    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  }