forked from quic-go/quic-go
add an upload handler to the demo server
This commit is contained in:
@@ -1,12 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/md5"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -29,6 +32,11 @@ func (b *binds) Set(v string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size is needed by the /demo/upload handler to determine the size of the uploaded file
|
||||||
|
type Size interface {
|
||||||
|
Size() int64
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Small 40x40 png
|
// Small 40x40 png
|
||||||
@@ -58,6 +66,38 @@ func init() {
|
|||||||
}
|
}
|
||||||
w.Write(body)
|
w.Write(body)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// accept file uploads and return the MD5 of the uploaded file
|
||||||
|
// maximum accepted file size is 1 GB
|
||||||
|
http.HandleFunc("/demo/upload", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodPost {
|
||||||
|
var err error
|
||||||
|
err = r.ParseMultipartForm(1 << 30) // 1 GB
|
||||||
|
if err == nil {
|
||||||
|
var file multipart.File
|
||||||
|
file, _, err = r.FormFile("uploadfile")
|
||||||
|
if err == nil {
|
||||||
|
var size int64
|
||||||
|
if sizeInterface, ok := file.(Size); ok {
|
||||||
|
size = sizeInterface.Size()
|
||||||
|
b := make([]byte, size)
|
||||||
|
file.Read(b)
|
||||||
|
md5 := md5.Sum(b)
|
||||||
|
fmt.Fprintf(w, "%x", md5)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = errors.New("Couldn't get uploaded file size.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
utils.Infof("Error receiving upload: %#v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
io.WriteString(w, `<html><body><form action="/demo/upload" method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="uploadfile"><br>
|
||||||
|
<input type="submit">
|
||||||
|
</form></body></html>`)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user