mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Adding transfer ability
Former-commit-id: c7f6a0db52693be7730b29733b28b54a42237e93 [formerly bf89dab668fa14254af014ff00ccd7edec6c5db8] [formerly a4ba75c6c7241dbf9416a84841e7d7ec1c4e1347 [formerly bca9d95f33cc49f0bebdc78ffe08f02b387fe613 [formerly3a8b6ae64d
]]] Former-commit-id: f56dd635717764f48370bdafef1d32e1b862abf2 [formerly 8042910e988a56fd23454d590afb99ff0cc2ef65] Former-commit-id: bb17fb1d842e9575e041cf2d11537c0489b3501d Former-commit-id:200e2c848e
This commit is contained in:
parent
44c2dcec7c
commit
279bacc9e8
59
main.go
59
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)
|
||||
|
Loading…
Reference in New Issue
Block a user