diff --git a/backend/terabox/api.go b/backend/terabox/api.go index b4d14cc05f..663702c747 100644 --- a/backend/terabox/api.go +++ b/backend/terabox/api.go @@ -709,6 +709,44 @@ func (f *Fs) apiFileLocateUpload(ctx context.Context) error { return nil } +func (f *Fs) apiShare(ctx context.Context, path string, expire fs.Duration) (string, error) { + // 0, 1, 7, and 30 are the only valid periods, with 0 being permanent + var period int + hours := time.Duration(expire).Hours() + if hours <= 24 { + period = 1 + } else if hours <= 168 { + period = 7 + } else if hours <= 720 { + period = 30 + } + + data := url.Values{} + data.Set("schannel", "0") + data.Set("channel_list", "[0]") + data.Set("period", fmt.Sprintf("%d", period)) + data.Set("path_list", fmt.Sprintf("[\"%s\"]", TBPath(path))) + // fid_list is set here officially, but seems to be optional. + // it has been excluded, otherwise we would have to use apiItemInfo. + data.Set("pwd", "") + data.Set("public", "1") + + opt := NewRequest(http.MethodPost, "/share/pset") + opt.ContentType = "application/x-www-form-urlencoded" + opt.Body = strings.NewReader(data.Encode()) + + var res api.ResponseShare + err := f.apiExec(ctx, opt, &res) + if err != nil { + return "", err + } + if res.Err() != nil { + return "", res.Err() + } + + return res.Link, nil +} + func (f *Fs) _apiFileUploadPrecreate(ctx context.Context, path string, size int64, modTime time.Time) (*api.ResponsePrecreate, error) { opt := NewRequest(http.MethodPost, "/api/precreate") diff --git a/backend/terabox/api/types.go b/backend/terabox/api/types.go index 5fa054e949..4a8185ac47 100644 --- a/backend/terabox/api/types.go +++ b/backend/terabox/api/types.go @@ -76,6 +76,12 @@ type ResponseDownload struct { } `json:"file_info"` } +// ResponseShare contain Share link +type ResponseShare struct { + ErrorAPI + Link string `json:"link"` +} + // ResponseHomeInfo download file sign type ResponseHomeInfo struct { ErrorAPI diff --git a/backend/terabox/terabox.go b/backend/terabox/terabox.go index 845c9eccec..d4aecca1a4 100644 --- a/backend/terabox/terabox.go +++ b/backend/terabox/terabox.go @@ -39,6 +39,7 @@ import ( var ( _ fs.Fs = (*Fs)(nil) _ fs.Abouter = (*Fs)(nil) + _ fs.PublicLinker = (*Fs)(nil) _ fs.Copier = (*Fs)(nil) _ fs.Mover = (*Fs)(nil) _ fs.DirMover = (*Fs)(nil) @@ -288,6 +289,16 @@ func (f *Fs) About(ctx context.Context) (*fs.Usage, error) { return &fs.Usage{Total: &info.Total, Used: &info.Used, Free: &free}, nil } +// +// fs.PublicLinker Interface implementation [optional] +//------------------------------------------------------------------------------------------------------------------------ + +// PublicLink returns a link for downloading without an account. +func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, unlink bool) (string, error) { + debug(f.opt, 1, "PublicLink %s; %s;", remote, expire.String()) + return f.apiShare(ctx, f.opt.Enc.FromStandardPath(libPath.Join(f.root, remote)), expire) +} + // // fs.Fs interface implementation //------------------------------------------------------------------------------------------------------------------------