Skip to content

Commit

Permalink
add notion backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Mar 15, 2021
1 parent 9f2286a commit 4b34af4
Show file tree
Hide file tree
Showing 9 changed files with 478 additions and 4 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ Github Action中有实时构建版本,如有需要可以在Github Action的构
```
airportal(arp), catbox(cat), cowtransfer(cow), fileio(fio),
gofile(gof), lanzous(lzs), litterbox(lit), null(0x0),
wetransfer(wet), vimcn(vim)
wetransfer(wet), vimcn(vim), notion(not)
```

[notion上传相关说明](https://github.com/Mikubill/transfer#notion)

[登陆上传相关说明](https://github.com/Mikubill/transfer#login)

目前支持的图床服务:
Expand Down Expand Up @@ -200,6 +202,29 @@ Note: Crypto mode is not compatible with multi thread download mode, setting par
...
```

### notion

notion的上传需要以下参数

所有参数不带符号,即形如`ce6ad860c0864286a4392d6c2e786e8`即可。

```
-p Page ID
```
必须,即页面链接中的那个一大长串的ID。建议直接使用Workspace的次级页面作为上传目标以便程序能自动获取当前Workspace ID,否则需要通过 -s 参数指定 Space ID。

```
-t token
```
必须,即cookie中的`www.notion.so -> token_v2`项。

```
-s Workspace ID
```
非必须,适用于非次级页面/嵌套的情况,手动设定Workspace ID

上传后默认返回一个自动签名链接,私有页面可以在浏览器登录状态下直接点击下载。对于公开页面的文件链接,可以尝试去掉userid使用,但必须保留id和table两项。

### login

部分backend支持登陆环境下上传,使用时只需要提供对应的cookie即可。
Expand Down
33 changes: 33 additions & 0 deletions apis/public/notion/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package notion

import (
"fmt"
"transfer/apis"
"transfer/utils"

"github.com/spf13/cobra"
)

var (
Backend = new(notion)
)

type notion struct {
apis.Backend
token string
pageID string
resp string
spaceID string
Commands [][]string
}

func (b *notion) SetArgs(cmd *cobra.Command) {
cmd.Flags().StringVarP(&b.token, "token", "t", "", "Your user cookie (token-v2)")
cmd.Flags().StringVarP(&b.pageID, "page", "p", "", "Your page id")
cmd.Flags().StringVarP(&b.spaceID, "space", "s", "", "Your space id")

cmd.Long = fmt.Sprintf("Notion - https://notion.so/\n\n" +
utils.Spacer(" Size Limit: 20M(Free), Unlimit(Pro)\n") +
utils.Spacer(" Upload Service: Amazon S3 US-West\n") +
utils.Spacer(" Download Service: Amazon S3 US-West, Cloudflare\n"))
}
33 changes: 33 additions & 0 deletions apis/public/notion/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package notion

import (
"fmt"
"net/http"
"regexp"
"transfer/apis"
)

var (
matcher = regexp.MustCompile("https://(www\\.notion\\.so/signed/https%3A%2F%2F)?s3-us-west-2\\.amazonaws\\.com.*")
)

func (b notion) DoDownload(link string, config apis.DownConfig) error {
err := apis.DownloadFile(&apis.DownloaderConfig{
Link: link,
Config: config,
Modifier: apis.AddHeaders,
})
if err != nil {
return fmt.Errorf("download failed on %s, returns %s\n", link, err)
}
return nil
}

func (b notion) AddHeaders(req *http.Request) {
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) "+
"Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10")
req.Header.Add("accept-language", "zh-CN,zh;q=0.9,en;")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept-Language", acceptLang)
req.Header.Set("cookie", fmt.Sprintf("token_v2=%v", b.token))
}
59 changes: 59 additions & 0 deletions apis/public/notion/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package notion

import (
"io"

"github.com/kjk/notionapi"
)

type uploadConfig struct {
debug bool
fileName string
fileReader io.Reader
fileSize int64
}

// POST /api/v3/getUploadFileUrl request
type getUploadFileUrlRequest struct {
Bucket string `json:"bucket"`
ContentType string `json:"contentType"`
Name string `json:"name"`
}

// GetUploadFileUrlResponse is a response to POST /api/v3/getUploadFileUrl
type GetUploadFileUrlResponse struct {
URL string `json:"url"`
SignedGetURL string `json:"signedGetUrl"`
SignedPutURL string `json:"signedPutUrl"`

FileID string `json:"-"`

RawJSON map[string]interface{} `json:"-"`
}

type Client struct {
notionapi.Client
}

type submitTransactionRequest struct {
RequestID string `json:"requestId"`
Transaction []Transaction `json:"transactions"`
}

type Transaction struct {
ID string `json:"id"`
SpaceID string `json:"spaceId"`
Operations []*Operation `json:"operations"`
}

type Operation struct {
Point Pointer `json:"pointer"`
Path []string `json:"path"`
Command string `json:"command"`
Args interface{} `json:"args"`
}

type Pointer struct {
ID string `json:"id"`
Table string `json:"table"`
}
Loading

0 comments on commit 4b34af4

Please sign in to comment.