chore: Merge pull request #1079 from foxxorcat/dev

pull/1093/head
Noe Hsu 2022-05-10 22:35:59 +08:00 committed by GitHub
commit 279111a8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 102 additions and 19 deletions

View File

@ -448,6 +448,7 @@ func (driver Cloud139) Upload(file *model.FileStream, account *model.Account) er
return err
}
log.Debugf("%+v", res)
res.Body.Close()
start += byteSize
}
return nil

View File

@ -577,6 +577,7 @@ func (driver Cloud189) NewUpload(file *model.FileStream, account *model.Account)
r, err := base.HttpClient.Do(req)
log.Debugf("%+v %+v", r, r.Request.Header)
r.Body.Close()
if err != nil {
return err
}

View File

@ -619,8 +619,10 @@ func (driver Cloud189) CommonUpload(file *model.FileStream, parentFile *model.Fi
}
if r.StatusCode != http.StatusOK {
data, _ := io.ReadAll(r.Body)
r.Body.Close()
return fmt.Errorf(string(data))
}
r.Body.Close()
}
fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
@ -727,8 +729,10 @@ func (driver Cloud189) FastUpload(file *model.FileStream, parentFile *model.File
}
if r.StatusCode != http.StatusOK {
data, _ := io.ReadAll(r.Body)
r.Body.Close()
return fmt.Errorf(string(data))
}
r.Body.Close()
}
}

View File

@ -424,10 +424,17 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
}
if account.Bool1 {
buf := make([]byte, 1024)
n, _ := file.Read(buf[:])
reqBody["pre_hash"] = utils.GetSHA1Encode(string(buf[:n]))
file.File = io.NopCloser(io.MultiReader(bytes.NewReader(buf[:n]), file.File))
buf := bytes.NewBuffer(make([]byte, 0, 1024))
io.CopyN(buf, file, 1024)
reqBody["pre_hash"] = utils.GetSHA1Encode(buf.String())
// 把头部拼接回去
file.File = struct {
io.Reader
io.Closer
}{
Reader: io.MultiReader(buf, file.File),
Closer: file.File,
}
} else {
reqBody["content_hash_name"] = "none"
reqBody["proof_version"] = "v1"
@ -454,7 +461,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return fmt.Errorf("%s", e.Message)
}
if e.Code == "PreHashMatched" && account.Bool1 {
if account.Bool1 && e.Code == "PreHashMatched" {
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
@ -499,6 +506,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return nil
}
// 秒传失败
if _, err = tempFile.Seek(0, io.SeekStart); err != nil {
return err
}
@ -515,6 +523,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return err
}
log.Debugf("%+v", res)
res.Body.Close()
//res, err := base.BaseClient.R().
// SetHeader("Content-Type","").
// SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)

View File

@ -87,7 +87,7 @@ func (driver Baidu) GetAllFile(account *model.Account) (files []File, err error)
for {
var resp FileListResp
_, err = driver.Request(http.MethodGet, FILE_API_URL+"/list", func(r *resty.Request) {
_, err = driver.Request(http.MethodGet, FILE_API_URL_V1+"/list", func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"need_thumbnail": "1",
"need_filter_hidden": "0",

View File

@ -43,6 +43,14 @@ func (driver Baidu) Items() []base.Item {
Label: "album_id",
Type: base.TypeString,
},
{
Name: "internal_type",
Label: "download api",
Type: base.TypeSelect,
Required: true,
Values: "file,album",
Default: "album",
},
{
Name: "client_id",
Label: "client id",
@ -156,6 +164,13 @@ func (driver Baidu) Files(path string, account *model.Account) ([]model.File, er
}
func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, error) {
if account.InternalType == "file" {
return driver.LinkFile(args, account)
}
return driver.LinkAlbum(args, account)
}
func (driver Baidu) LinkAlbum(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
@ -190,6 +205,42 @@ func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, er
}, nil
}
func (driver Baidu) LinkFile(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
if !IsAlbumFile(file) {
return nil, base.ErrNotSupport
}
album, err := driver.File(utils.Dir(utils.ParsePath(args.Path)), account)
if err != nil {
return nil, err
}
// 拷贝到根目录
cfile, err := driver.CopyAlbumFile(album.Id, account, file.Id)
if err != nil {
return nil, err
}
res, err := driver.Request(http.MethodGet, FILE_API_URL_V2+"/download", func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"fsid": fmt.Sprint(cfile.Fsid),
})
}, account)
if err != nil {
return nil, err
}
return &base.Link{
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
},
Url: utils.Json.Get(res.Body(), "dlink").ToString(),
}, nil
}
func (driver Baidu) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
file, err := driver.File(path, account)
@ -402,7 +453,7 @@ func (driver Baidu) Upload(file *model.FileStream, account *model.Account) error
// 预上传
var precreateResp PrecreateResp
_, err = driver.Request(http.MethodPost, FILE_API_URL+"/precreate", func(r *resty.Request) {
_, err = driver.Request(http.MethodPost, FILE_API_URL_V1+"/precreate", func(r *resty.Request) {
r.SetFormData(params)
r.SetResult(&precreateResp)
}, account)
@ -431,7 +482,7 @@ func (driver Baidu) Upload(file *model.FileStream, account *model.Account) error
fallthrough
case 2: // 创建文件
params["uploadid"] = precreateResp.UploadID
_, err = driver.Request(http.MethodPost, FILE_API_URL+"/create", func(r *resty.Request) {
_, err = driver.Request(http.MethodPost, FILE_API_URL_V1+"/create", func(r *resty.Request) {
r.SetFormData(params)
r.SetResult(&precreateResp)
}, account)

View File

@ -97,7 +97,7 @@ type (
type (
UploadFile struct {
FsID int64 `json:"fs_id"`
Size int `json:"size"`
Size int64 `json:"size"`
Md5 string `json:"md5"`
ServerFilename string `json:"server_filename"`
Path string `json:"path"`

View File

@ -13,9 +13,10 @@ import (
)
const (
API_URL = "https://photo.baidu.com/youai"
ALBUM_API_URL = API_URL + "/album/v1"
FILE_API_URL = API_URL + "/file/v1"
API_URL = "https://photo.baidu.com/youai"
ALBUM_API_URL = API_URL + "/album/v1"
FILE_API_URL_V1 = API_URL + "/file/v1"
FILE_API_URL_V2 = API_URL + "/file/v2"
)
var (

View File

@ -307,8 +307,10 @@ func (driver Onedrive) UploadBig(file *model.FileStream, account *model.Account)
res, err := base.HttpClient.Do(req)
if res.StatusCode != 201 && res.StatusCode != 202 {
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
return errors.New(string(data))
}
res.Body.Close()
}
return nil
}

View File

@ -105,7 +105,7 @@ func (driver XunLeiCloud) Items() []base.Item {
Label: "device id",
Default: utils.GetMD5Encode(uuid.NewString()),
Type: base.TypeString,
Required: false,
Required: true,
},
}
}
@ -118,8 +118,10 @@ func (driver XunLeiCloud) Save(account *model.Account, old *model.Account) error
client := GetClient(account)
// 指定验证通过的captchaToken
if client.captchaToken != "" {
client.Lock()
client.captchaToken = account.CaptchaToken
account.CaptchaToken = ""
client.Unlock()
}
if client.token == "" {

View File

@ -73,6 +73,8 @@ func (c *Client) requestCaptchaToken(action string, meta map[string]string) erro
SetBody(&param).
SetError(&e).
SetResult(&resp).
SetHeader("X-Device-Id", c.deviceID).
SetQueryParam("client_id", c.clientID).
Post(XLUSER_API_URL + "/shield/captcha/init")
if err != nil {
return err
@ -133,6 +135,8 @@ func (c *Client) Login(account *model.Account) (err error) {
Username: account.Username,
Password: account.Password,
}).
SetHeader("X-Device-Id", c.deviceID).
SetQueryParam("client_id", c.clientID).
Post(url)
if err != nil {
return err
@ -184,6 +188,8 @@ func (c *Client) RefreshToken() error {
"client_id": c.clientID,
"client_secret": c.clientSecret,
}).
SetHeader("X-Device-Id", c.deviceID).
SetQueryParam("client_id", c.clientID).
Post(XLUSER_API_URL + "/auth/token")
if err != nil {
return err
@ -211,7 +217,8 @@ func (c *Client) Request(method string, url string, callback func(*resty.Request
"X-Captcha-Token": c.captchaToken,
"User-Agent": c.userAgent,
"client_id": c.clientID,
}).SetQueryParam("client_id", c.clientID)
}).
SetQueryParam("client_id", c.clientID)
if callback != nil {
callback(req)
}

View File

@ -213,7 +213,8 @@ func (driver Yandex) Upload(file *model.FileStream, account *model.Account) erro
}
req.Header.Set("Content-Length", strconv.FormatUint(file.Size, 10))
req.Header.Set("Content-Type", "application/octet-stream")
_, err = base.HttpClient.Do(req)
res, err := base.HttpClient.Do(req)
res.Body.Close()
//res, err := base.RestyClient.R().
// SetHeader("Content-Length", strconv.FormatUint(file.Size, 10)).
// SetBody(file).Put(resp.Href)

View File

@ -1,14 +1,16 @@
package server
import (
"io/fs"
"io/ioutil"
"net/http"
"net/http/pprof"
"strings"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/public"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"io/fs"
"io/ioutil"
"net/http"
"strings"
)
func InitIndex() {
@ -48,6 +50,8 @@ func Static(r *gin.Engine) {
c.Status(200)
if strings.HasPrefix(c.Request.URL.Path, "/@manage") {
_, _ = c.Writer.WriteString(conf.ManageHtml)
} else if strings.HasPrefix(c.Request.URL.Path, "/debug/pprof") {
pprof.Index(c.Writer, c.Request)
} else {
_, _ = c.Writer.WriteString(conf.IndexHtml)
}