FAQ
package main

import (
     "fmt"
     "net/http"
     "log"
     "io/ioutil"
)

var types = map[string]string {
     "png": "image/png",
     "jpeg": "image/jpeg",
}

// hello world, the web server
func server(w http.ResponseWriter, req *http.Request) {


     file, handler, err := req.FormFile("file")
     if err != nil {
             fmt.Println(err)
     }


     if handler.Header["Content-Type"] == types["jpeg"] {
         fmt.Println("works")
     } else {
         fmt.Println("doesn't work")
     }


     data, err := ioutil.ReadAll(file)
     if err != nil {
             fmt.Println(err)
     }


     err = ioutil.WriteFile(handler.Filename, data, 0777)
     if err != nil {
             fmt.Println(err)
     }


     fmt.Println(handler.Header["Content-Type"])

}


func main() {
     http.HandleFunc("/upload", server)

     err := http.ListenAndServe(":8080", nil)
     if err != nil {
         log.Fatal("ListenAndServe: ", err)
     }
}

I want to validate a files content-type before uploading it, like checking
if it's a png or not. Maybe I should just use the DetectContentType
function instead?

If I can make a string to []string then everything should be solved I guess.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Konstantin Khomoutov at Dec 17, 2014 at 8:07 pm

    On Wed, 17 Dec 2014 11:44:55 -0800 (PST) skarflacka232@gmail.com wrote: [...]
    var types = map[string]string {
    "png": "image/png",
    "jpeg": "image/jpeg",
    } [...]
    if handler.Header["Content-Type"] == types["jpeg"] {
    fmt.Println("works")
    } else {
    fmt.Println("doesn't work")
    }
    [...]

    Please see <http://golang.org/pkg/net/http/#Header>

    Since HTTP allows several headers with the same key ("Content-Type") in
    your case to be present in the request, Go's net/http returns whatever
    number of values it has received keyed by the given string.

    So you have to rewrite your code like this (pseudo-code):

    var vals := handler.Header["Content-Type"]
    if len(vals) != 1 {
       FailTheProcessing()
       return
    }
    if vals[0] == types["jpeg"] {
       // continue as before
    ...

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tamás Gulácsi at Dec 18, 2014 at 5:36 am
    Or just Header.Get("Content-Type") - this returns the first such header, or "" if none found.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • DisposaBoy at Dec 19, 2014 at 6:08 am
    I think others have explained the typing issue... Yes you should use DetectContentType to check the type of the *content*. The client can send you any header it likes and trusting it could potentially leave your server/service vulnerable.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 17, '14 at 7:44p
activeDec 19, '14 at 6:08a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase