Skip to content

Commit

Permalink
fix: download error on wss link(#14), minor fix on notion api, add ch…
Browse files Browse the repository at this point in the history
…ibi support.
  • Loading branch information
Mikubill committed Feb 8, 2022
1 parent 42abe84 commit 79c42a0
Show file tree
Hide file tree
Showing 23 changed files with 431 additions and 217 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ Beta即为实时构建版本,不一定能正常运行,仅建议用作测试
| CowTransfer | https://www.cowtransfer.com/ | 2GB |
| Fileio | https://file.io/ | 100MB |
| GoFile | https://gofile.io/ | - |
| Vim-cn | https://img.vim-cn.com/ | 100MB |
| Wenshushu | https://wenshushu.cn/ | 2GB |
| WeTransfer | https://wetransfer.com/ | 2GB |
| WhiteCats | http://whitecats.dip.jp/ | 60s/file |
| Transfer.sh | https://transfer.sh/ | - |
| LitterBox | https://litterbox.catbox.moe/ | 1GB |
| Lanzous | https://www.lanzous.com/ | login |
| Notion | https://www.notion.so/ | login |
| 1Fichier | https://www.1fichier.com/ | 300GB |
| Null | https://0x0.st/ | 512M |

已失效或不可用的服务:

| Name | Site |
| ---- | ---- |
| Vim-cn | https://img.vim-cn.com/ |
| WhiteCats | http://whitecats.dip.jp/ |

部分服务仅支持上传;部分服务需要使用beta版本。

Expand Down
1 change: 1 addition & 0 deletions apis/image/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func (s BD) Upload(data []byte) (string, error) {
if r.Message != "Success" {
return "", fmt.Errorf(r.Message)
}

return s.linkBuilder(r.Data.Sign), nil
}
21 changes: 17 additions & 4 deletions apis/image/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package image
import (
"bytes"
"fmt"
"github.com/cheggaaa/pb/v3"
cmap "github.com/orcaman/concurrent-map"
"io"
"io/ioutil"
"mime/multipart"
Expand All @@ -15,6 +13,9 @@ import (
"sync"
"time"
"transfer/utils"

"github.com/cheggaaa/pb/v3"
cmap "github.com/orcaman/concurrent-map"
)

type PicBed interface {
Expand Down Expand Up @@ -66,7 +67,18 @@ func (s picBed) DownloadStream(chan DownloadDataFlow) {
panic("DownloadStream method not implemented")
}

func (s picBed) upload(data []byte, postURL string, fieldName string) ([]byte, error) {
func defaultReqMod(req *http.Request) {
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0")
req.Header.Set("accept", "application/json, text/javascript, */*; q=0.01")
req.Header.Set("accept-language", "en-US,en;q=0.5")
req.Header.Set("origin", req.URL.String())
req.Header.Set("referer", req.URL.String())
req.Header.Set("x-requested-with", "XMLHttpRequest")
req.Header.Set("x-csrf-token", "")
}

func (s picBed) upload(data []byte, postURL string, fieldName string,
reqModifier func(*http.Request)) ([]byte, error) {

if Verbose {
fmt.Println("requesting: " + postURL)
Expand All @@ -87,8 +99,9 @@ func (s picBed) upload(data []byte, postURL string, fieldName string) ([]byte, e
if err != nil {
return nil, err
}
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0")
req.Header.Set("content-type", fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary()))
reqModifier(req)

resp, err := client.Do(req)
if err != nil {
return nil, err
Expand Down
16 changes: 15 additions & 1 deletion apis/image/ccupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package image
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
)

Expand Down Expand Up @@ -35,9 +36,22 @@ func (s CC) linkBuilder(link string) string {
return "https://upload.cc/" + getter.FindString(link) + ".png"
}

func (s CC) ModBuilder(ck *http.Cookie) func(*http.Request) {
return func(req *http.Request) {
defaultReqMod(req)
req.AddCookie(ck)
}
}

func (s CC) Upload(data []byte) (string, error) {
resp, err := http.Head("https://upload.cc/images/loading.gif")
if err != nil {
return "", err
}
defer resp.Body.Close()
cookie := resp.Cookies()[0]

body, err := s.upload(data, "https://upload.cc/image_upload", "uploaded_file[]")
body, err := s.upload(data, "https://upload.cc/image_upload", "uploaded_file[]", s.ModBuilder(cookie))
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/juejin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s JJ) linkBuilder(link string) string {

func (s JJ) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://cdn-ms.juejin.im/v1/upload?bucket=gold-user-assets", "file")
body, err := s.upload(data, "https://cdn-ms.juejin.im/v1/upload?bucket=gold-user-assets", "file", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/netease.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s NT) linkBuilder(link string) string {

func (s NT) Upload(data []byte) (string, error) {

body, err := s.upload(data, "http://you.163.com/xhr/file/upload.json", "file")
body, err := s.upload(data, "http://you.163.com/xhr/file/upload.json", "file", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/prntscr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s PR) linkBuilder(link string) string {

func (s PR) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://prntscr.com/upload.php", "image")
body, err := s.upload(data, "https://prntscr.com/upload.php", "image", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/smms.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s SM) linkBuilder(link string) string {

func (s SM) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://sm.ms/api/v2/upload", "smfile")
body, err := s.upload(data, "https://sm.ms/api/v2/upload", "smfile", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/sogou.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s SG) linkBuilder(link string) string {

func (s SG) Upload(data []byte) (string, error) {

body, err := s.upload(data, "http://pic.sogou.com/pic/upload_pic.jsp", "pic_path")
body, err := s.upload(data, "http://pic.sogou.com/pic/upload_pic.jsp", "pic_path", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/telegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s TG) linkBuilder(link string) string {

func (s TG) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://telegra.ph/upload", "file")
body, err := s.upload(data, "https://telegra.ph/upload", "file", defaultReqMod)
if err != nil || strings.Contains(string(body), "error") {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/toutiao.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s TT) linkBuilder(link string) string {

func (s TT) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://mp.toutiao.com/upload_photo/?type=json", "photo")
body, err := s.upload(data, "https://mp.toutiao.com/upload_photo/?type=json", "photo", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/vimcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s VM) linkBuilder(link string) string {

func (s VM) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://img.vim-cn.com", "name")
body, err := s.upload(data, "https://img.vim-cn.com", "name", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apis/image/xiaomi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s XM) linkBuilder(link string) string {

func (s XM) Upload(data []byte) (string, error) {

body, err := s.upload(data, "https://shopapi.io.mi.com/homemanage/shop/uploadpic", "pic")
body, err := s.upload(data, "https://shopapi.io.mi.com/homemanage/shop/uploadpic", "pic", defaultReqMod)
if err != nil {
return "", err
}
Expand Down
24 changes: 24 additions & 0 deletions apis/public/chibi/api.go
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"))
}
5 changes: 5 additions & 0 deletions apis/public/chibi/download.go
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
}
12 changes: 12 additions & 0 deletions apis/public/chibi/struct.go
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
}
116 changes: 116 additions & 0 deletions apis/public/chibi/upload.go
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
}
Loading

0 comments on commit 79c42a0

Please sign in to comment.