From 279bacc9e881f0e2408ae107e18e2a96a34897d7 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 28 Mar 2016 10:24:48 -0400 Subject: [PATCH] Adding transfer ability Former-commit-id: c7f6a0db52693be7730b29733b28b54a42237e93 [formerly bf89dab668fa14254af014ff00ccd7edec6c5db8] [formerly a4ba75c6c7241dbf9416a84841e7d7ec1c4e1347 [formerly bca9d95f33cc49f0bebdc78ffe08f02b387fe613 [formerly 3a8b6ae64da342c83d906af3ada0751573cd528e]]] Former-commit-id: f56dd635717764f48370bdafef1d32e1b862abf2 [formerly 8042910e988a56fd23454d590afb99ff0cc2ef65] Former-commit-id: bb17fb1d842e9575e041cf2d11537c0489b3501d Former-commit-id: 200e2c848e9e09a820c877dc1aa6f5236623f075 --- main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/main.go b/main.go index 136947f..e072102 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "bytes" "flag" "fmt" + "io" "io/ioutil" + "log" "os" "path" @@ -35,6 +38,8 @@ var RuntimeArgs struct { } var VersionNum string +const _24K = (1 << 20) * 24 + func main() { VersionNum = "0.94" // _, executableFile, _, _ := runtime.Caller(0) // get full path of this file @@ -102,6 +107,60 @@ Options:`) r.GET("/", newNote) r.HEAD("/", func(c *gin.Context) { c.Status(200) }) r.GET("/:title", editNote) + r.PUT("/:title", func(c *gin.Context) { + filename := c.Param("title") + fmt.Println(filename) + fmt.Println(c.Request.Body) + fmt.Println(c.Request.ContentLength) + fmt.Println(c.Request.Header) + contentLength := c.Request.ContentLength + var reader io.Reader + reader = c.Request.Body + if contentLength == -1 { + // queue file to disk, because s3 needs content length + var err error + var f io.Reader + + f = reader + + var b bytes.Buffer + + n, err := io.CopyN(&b, f, _24K+1) + if err != nil && err != io.EOF { + log.Printf("%s", err.Error()) + } + + if n > _24K { + file, err := ioutil.TempFile("./", "transfer-") + if err != nil { + log.Printf("%s", err.Error()) + } + + defer file.Close() + + n, err = io.Copy(file, io.MultiReader(&b, f)) + if err != nil { + os.Remove(file.Name()) + log.Printf("%s", err.Error()) + } + + reader, err = os.Open(file.Name()) + } else { + reader = bytes.NewReader(b.Bytes()) + } + + contentLength = n + } + buf := new(bytes.Buffer) + buf.ReadFrom(reader) + s := buf.String() // Does a complete copy of the bytes in the buffer. + fmt.Println("---------------") + fmt.Println(s) + fmt.Println("---------------") + fmt.Println(c.ContentType()) + fmt.Println(c.Request.Header) + fmt.Println("---------------") + }) r.GET("/:title/*option", everythingElse) r.POST("/:title/*option", encryptionRoute) r.DELETE("/listitem", deleteListItem)