mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
curl upload/download
This commit is contained in:
parent
4ffcc1abd5
commit
f6c287ba24
54
main.go
54
main.go
@ -1,12 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
@ -107,56 +104,7 @@ Options:`)
|
|||||||
r.GET("/", newNote)
|
r.GET("/", newNote)
|
||||||
r.HEAD("/", func(c *gin.Context) { c.Status(200) })
|
r.HEAD("/", func(c *gin.Context) { c.Status(200) })
|
||||||
r.GET("/:title", editNote)
|
r.GET("/:title", editNote)
|
||||||
r.PUT("/:title", func(c *gin.Context) {
|
r.PUT("/:title", putFile)
|
||||||
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.GET("/:title/*option", everythingElse)
|
r.GET("/:title/*option", everythingElse)
|
||||||
r.POST("/:title/*option", encryptionRoute)
|
r.POST("/:title/*option", encryptionRoute)
|
||||||
r.DELETE("/listitem", deleteListItem)
|
r.DELETE("/listitem", deleteListItem)
|
||||||
|
60
routes.go
60
routes.go
@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -18,6 +21,57 @@ import (
|
|||||||
"github.com/russross/blackfriday"
|
"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 {
|
type EncryptionPost struct {
|
||||||
Text string `form:"text" json:"text" binding:"required"`
|
Text string `form:"text" json:"text" binding:"required"`
|
||||||
Password string `form:"password" json:"password" binding:"required"`
|
Password string `form:"password" json:"password" binding:"required"`
|
||||||
@ -161,11 +215,17 @@ func editNote(c *gin.Context) {
|
|||||||
version := c.DefaultQuery("version", "-1")
|
version := c.DefaultQuery("version", "-1")
|
||||||
versionNum, _ := strconv.Atoi(version)
|
versionNum, _ := strconv.Atoi(version)
|
||||||
currentText, versions, currentVersion, totalTime, encrypted, locked := getCurrentText(title, versionNum)
|
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 {
|
if encrypted || len(locked) > 0 {
|
||||||
c.Redirect(302, "/"+title+"/view")
|
c.Redirect(302, "/"+title+"/view")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct") {
|
if strings.Contains(currentText, "self-destruct\n") || strings.Contains(currentText, "\nself-destruct") {
|
||||||
c.Redirect(302, "/"+title+"/view")
|
c.Redirect(302, "/"+title+"/view")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
numRows := len(strings.Split(currentText, "\n")) + 10
|
numRows := len(strings.Split(currentText, "\n")) + 10
|
||||||
totalTimeString := totalTime.String()
|
totalTimeString := totalTime.String()
|
||||||
|
Loading…
Reference in New Issue
Block a user