-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: download error on wss link(#14), minor fix on notion api, add ch…
…ibi support.
- Loading branch information
Showing
23 changed files
with
431 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package chibi | ||
|
||
import ( | ||
"fmt" | ||
"transfer/apis" | ||
"transfer/utils" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
Backend = new(chibi) | ||
) | ||
|
||
type chibi struct { | ||
apis.Backend | ||
resp string | ||
Commands [][]string | ||
} | ||
|
||
func (b *chibi) SetArgs(cmd *cobra.Command) { | ||
cmd.Long = fmt.Sprintf("Chibisafe/Lolisafe\n\n" + | ||
utils.Spacer(" Repo: https://github.com/WeebDev/chibisafe\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package chibi | ||
|
||
func (b chibi) LinkMatcher(v string) bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package chibi | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type uploadConfig struct { | ||
debug bool | ||
fileName string | ||
fileReader io.Reader | ||
fileSize int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package chibi | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"mime/multipart" | ||
"net/http" | ||
"strconv" | ||
"transfer/apis" | ||
"transfer/utils" | ||
) | ||
|
||
func (b *chibi) DoUpload(name string, size int64, file io.Reader) error { | ||
|
||
body, err := b.newMultipartUpload(uploadConfig{ | ||
fileSize: size, | ||
fileName: name, | ||
fileReader: file, | ||
debug: apis.DebugMode, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("upload returns error: %s", err) | ||
} | ||
|
||
b.resp = string(body) | ||
|
||
return nil | ||
} | ||
|
||
func (b chibi) PostUpload(string, int64) (string, error) { | ||
fmt.Printf("Download Link: %s\n", b.resp) | ||
return b.resp, nil | ||
} | ||
|
||
func (b chibi) newMultipartUpload(config uploadConfig) ([]byte, error) { | ||
if config.debug { | ||
log.Printf("start upload") | ||
} | ||
client := http.Client{} | ||
|
||
byteBuf := &bytes.Buffer{} | ||
writer := multipart.NewWriter(byteBuf) | ||
_ = writer.WriteField("reqtype", "fileupload") | ||
_ = writer.WriteField("userhash", "") | ||
_ = writer.WriteField("u_key", utils.GenRandString(16)) | ||
_, err := writer.CreateFormFile("fileToUpload", config.fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
writerLength := byteBuf.Len() | ||
writerBody := make([]byte, writerLength) | ||
_, _ = byteBuf.Read(writerBody) | ||
_ = writer.Close() | ||
|
||
boundary := byteBuf.Len() | ||
lastBoundary := make([]byte, boundary) | ||
_, _ = byteBuf.Read(lastBoundary) | ||
|
||
totalSize := int64(writerLength) + config.fileSize + int64(boundary) | ||
partR, partW := io.Pipe() | ||
|
||
go func() { | ||
_, _ = partW.Write(writerBody) | ||
for { | ||
buf := make([]byte, 256) | ||
nr, err := io.ReadFull(config.fileReader, buf) | ||
if nr <= 0 { | ||
break | ||
} | ||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { | ||
fmt.Println(err) | ||
break | ||
} | ||
if nr > 0 { | ||
_, _ = partW.Write(buf[:nr]) | ||
} | ||
} | ||
_, _ = partW.Write(lastBoundary) | ||
_ = partW.Close() | ||
}() | ||
|
||
req, err := http.NewRequest("POST", "", partR) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.ContentLength = totalSize | ||
req.Header.Set("content-length", strconv.FormatInt(totalSize, 10)) | ||
req.Header.Set("content-type", fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())) | ||
if config.debug { | ||
log.Printf("header: %v", req.Header) | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
if config.debug { | ||
log.Printf("do requests returns error: %v", err) | ||
} | ||
return nil, err | ||
} | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
if config.debug { | ||
log.Printf("read response returns: %v", err) | ||
} | ||
return nil, err | ||
} | ||
_ = resp.Body.Close() | ||
if config.debug { | ||
log.Printf("returns: %v", string(body)) | ||
} | ||
|
||
return body, nil | ||
} |
Oops, something went wrong.