From f6c287ba24dbbd6c270fc0b74260e02f281195eb Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 28 Mar 2016 19:55:25 -0400 Subject: [PATCH] curl upload/download --- main.go | 54 +------------------------------------------------ routes.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 53 deletions(-) diff --git a/main.go b/main.go index 6946657..cdcebb3 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,9 @@ package main import ( - "bytes" "flag" "fmt" - "io" "io/ioutil" - "log" "os" "path" @@ -107,56 +104,7 @@ 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") - 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) - fmt.Println("---------------") - fmt.Println(filename) - fmt.Println("---------------") - fmt.Println(buf.String()) - fmt.Println("---------------") - p := WikiData{filename, "", []string{}, []string{}, false, ""} - p.save(buf.String()) - }) + r.PUT("/:title", putFile) r.GET("/:title/*option", everythingElse) r.POST("/:title/*option", encryptionRoute) r.DELETE("/listitem", deleteListItem) diff --git a/routes.go b/routes.go index 8ce9b6b..71153fd 100644 --- a/routes.go +++ b/routes.go @@ -1,9 +1,12 @@ package main import ( + "bytes" "fmt" "html/template" + "io" "io/ioutil" + "log" "net/http" "os" "path" @@ -18,6 +21,57 @@ import ( "github.com/russross/blackfriday" ) +func putFile(c *gin.Context) { + filename := c.Param("title") + 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) + // p := WikiData{filename, "", []string{}, []string{}, false, ""} + // p.save(buf.String()) + var p WikiData + p.load(strings.ToLower(filename)) + p.save(buf.String()) + fmt.Println(c.ClientIP()) + c.Data(200, "text/plain", []byte("File uploaded to http://"+RuntimeArgs.ExternalIP+"/"+filename)) +} + type EncryptionPost struct { Text string `form:"text" json:"text" binding:"required"` Password string `form:"password" json:"password" binding:"required"` @@ -161,11 +215,17 @@ func editNote(c *gin.Context) { version := c.DefaultQuery("version", "-1") versionNum, _ := strconv.Atoi(version) currentText, versions, currentVersion, totalTime, encrypted, locked := getCurrentText(title, versionNum) + if strings.Contains(c.Request.Header.Get("User-Agent"), "curl/") { + c.Data(200, "text/plain", []byte(currentText)) + return + } if encrypted || len(locked) > 0 { c.Redirect(302, "/"+title+"/view") + return } if strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct") { c.Redirect(302, "/"+title+"/view") + return } numRows := len(strings.Split(currentText, "\n")) + 10 totalTimeString := totalTime.String()