Skip to content

Commit

Permalink
chore: Merge pull request #1167 from Xhofe/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe authored May 28, 2022
2 parents e1ccc0b + 323dad2 commit c35d54d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 46 deletions.
7 changes: 7 additions & 0 deletions bootstrap/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func InitSettings() {
Access: model.PUBLIC,
Group: model.FRONT,
},
{
Key: "pdf viewer url",
Type: "string",
Value: "https://alist-org.github.io/pdf.js/web/viewer.html?file=$url",
Access: model.PUBLIC,
Group: model.FRONT,
},
{
Key: "autoplay video",
Value: "false",
Expand Down
2 changes: 1 addition & 1 deletion conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
"go", "sh", "c", "cpp", "h", "hpp", "tsx", "vtt", "srt", "ass"}
DProxyTypes = []string{"m3u8"}
OfficeTypes = []string{"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"}
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb", "webm", "flv"}
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb", "webm", "flv", "m4v"}
AudioTypes = []string{"mp3", "flac", "ogg", "m4a", "wav", "opus"}
ImageTypes = []string{"jpg", "tiff", "jpeg", "png", "gif", "bmp", "svg", "ico", "swf", "webp"}
)
Expand Down
59 changes: 44 additions & 15 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package _23

import (
"bytes"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
Expand Down Expand Up @@ -68,6 +70,12 @@ func (driver Pan123) Items() []base.Item {
Required: true,
Default: "asc",
},
{
Name: "bool_1",
Label: "stream upload",
Type: base.TypeBool,
Description: "io stream upload (test)",
},
}
}

Expand Down Expand Up @@ -302,23 +310,44 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
return base.ErrNotFolder
}

tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
const DEFAULT int64 = 10485760
var uploadFile io.Reader
h := md5.New()
if _, err = io.Copy(io.MultiWriter(tempFile, h), file); err != nil {
return err
}
etag := hex.EncodeToString(h.Sum(nil))
if account.Bool1 && file.GetSize() > uint64(DEFAULT) {
// 只计算前10MIB
buf := bytes.NewBuffer(make([]byte, 0, DEFAULT))
if n, err := io.CopyN(io.MultiWriter(buf, h), file, DEFAULT); err != io.EOF && n == 0 {
return err
}
// 增加额外参数防止MD5碰撞
h.Write([]byte(file.Name))
num := make([]byte, 8)
binary.BigEndian.PutUint64(num, file.Size)
h.Write(num)
// 拼装
uploadFile = io.MultiReader(buf, file)
} else {
// 计算完整文件MD5
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()

_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
if _, err = io.Copy(io.MultiWriter(tempFile, h), file); err != nil {
return err
}

_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}
uploadFile = tempFile
}
etag := hex.EncodeToString(h.Sum(nil))
data := base.Json{
"driveId": 0,
"duplicate": 2, // 2->覆盖 1->重命名 0->默认
Expand Down Expand Up @@ -352,7 +381,7 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
input := &s3manager.UploadInput{
Bucket: &resp.Data.Bucket,
Key: &resp.Data.Key,
Body: tempFile,
Body: uploadFile,
}
_, err = uploader.Upload(input)
if err != nil {
Expand Down
21 changes: 7 additions & 14 deletions drivers/189/189.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"io"
"math"
"net/http"
"net/http/cookiejar"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -94,18 +93,11 @@ type LoginResp struct {

// Login refer to PanIndex
func (driver Cloud189) Login(account *model.Account) error {
client, ok := client189Map[account.Name]
if !ok {
//cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client = resty.New()
//client.SetCookieJar(cookieJar)
client.SetTimeout(base.DefaultTimeout)
client.SetRetryCount(3)
client.SetHeader("Referer", "https://cloud.189.cn/")
}
// clear cookie
jar, _ := cookiejar.New(nil)
client.SetCookieJar(jar)
client := resty.New()
//client.SetCookieJar(cookieJar)
client.SetTimeout(base.DefaultTimeout)
client.SetRetryCount(3)
client.SetHeader("Referer", "https://cloud.189.cn/")
url := "https://cloud.189.cn/api/portal/loginUrl.action?redirectURL=https%3A%2F%2Fcloud.189.cn%2Fmain.action"
b := ""
lt := ""
Expand All @@ -131,7 +123,8 @@ func (driver Cloud189) Login(account *model.Account) error {
}
}
if lt == "" {
return fmt.Errorf("get page: %s \nstatus: %d \nrequest url: %s", b, res.StatusCode(), res.RawResponse.Request.URL.String())
return fmt.Errorf("get page: %s \nstatus: %d \nrequest url: %s\nredirect url: %s",
b, res.StatusCode(), res.RawResponse.Request.URL.String(), res.Header().Get("location"))
}
captchaToken := regexp.MustCompile(`captchaToken' value='(.+?)'`).FindStringSubmatch(b)[1]
returnUrl := regexp.MustCompile(`returnUrl = '(.+?)'`).FindStringSubmatch(b)[1]
Expand Down
23 changes: 14 additions & 9 deletions drivers/189pc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (driver Cloud189) CommonUpload(file *model.FileStream, parentFile *model.Fi

uploadData := uploadUrl.UploadUrls[fmt.Sprint("partNumber_", i)]
req, _ := http.NewRequest(http.MethodPut, uploadData.RequestURL, byteData)
req.Header.Del("User-Agent")
req.Header.Set("User-Agent", "")
for k, v := range ParseHttpHeader(uploadData.RequestHeader) {
req.Header.Set(k, v)
}
Expand Down Expand Up @@ -650,9 +650,10 @@ func (driver Cloud189) FastUpload(file *model.FileStream, parentFile *model.File
if err != nil {
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())

defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
// 初始化上传
state := GetState(account)

Expand Down Expand Up @@ -718,7 +719,7 @@ func (driver Cloud189) FastUpload(file *model.FileStream, parentFile *model.File
for i := 1; i <= count; i++ {
uploadData := uploadUrls.UploadUrls[fmt.Sprint("partNumber_", i)]
req, _ := http.NewRequest(http.MethodPut, uploadData.RequestURL, io.NewSectionReader(tempFile, int64(i-1)*DEFAULT, DEFAULT))
req.Header.Del("User-Agent")
req.Header.Set("User-Agent", "")
for k, v := range ParseHttpHeader(uploadData.RequestHeader) {
req.Header.Set(k, v)
}
Expand Down Expand Up @@ -756,8 +757,10 @@ func (driver Cloud189) uploadFamily(file *model.FileStream, parentFile *model.Fi
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
fileMd5 := md5.New()
if _, err = io.Copy(io.MultiWriter(fileMd5, tempFile), file); err != nil {
Expand Down Expand Up @@ -805,8 +808,10 @@ func (driver Cloud189) uploadPerson(file *model.FileStream, parentFile *model.Fi
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
fileMd5 := md5.New()
if _, err = io.Copy(io.MultiWriter(fileMd5, tempFile), file); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions drivers/alidrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,10 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return err
}

defer tempFile.Close()
defer os.Remove(tempFile.Name())
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()

delete(reqBody, "pre_hash")
h := sha1.New()
Expand Down
4 changes: 4 additions & 0 deletions drivers/base/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"strings"
)

func KeyCache(path string, account *model.Account) string {
Expand All @@ -15,6 +16,9 @@ func KeyCache(path string, account *model.Account) string {
}

func SaveSearchFiles[T model.ISearchFile](key string, obj []T) {
if strings.Contains(key, ".balance") {
return
}
err := model.DeleteSearchFilesByPath(key)
if err != nil {
log.Errorln("failed create search files", err)
Expand Down
2 changes: 1 addition & 1 deletion drivers/sftp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (driver SFTP) Delete(path string, account *model.Account) error {
if err != nil {
return err
}
return client.Remove(utils.Join(account.RootFolder, path))
return client.remove(utils.Join(account.RootFolder, path))
}

func (driver SFTP) Upload(file *model.FileStream, account *model.Account) error {
Expand Down
2 changes: 1 addition & 1 deletion drivers/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (client *Client) Files(remotePath string) ([]os.FileInfo, error) {
return client.ReadDir(remotePath)
}

func (client *Client) Remove(remotePath string) error {
func (client *Client) remove(remotePath string) error {
f, err := client.Stat(remotePath)
if err != nil {
return nil
Expand Down
6 changes: 4 additions & 2 deletions drivers/xunlei/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,10 @@ func (driver XunLeiCloud) Upload(file *model.FileStream, account *model.Account)
return err
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
gcid, err := getGcid(io.TeeReader(file, tempFile), int64(file.Size))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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") {
} else if strings.HasPrefix(c.Request.URL.Path, "/debug/pprof") && conf.Debug {
pprof.Index(c.Writer, c.Request)
} else {
_, _ = c.Writer.WriteString(conf.IndexHtml)
Expand Down

0 comments on commit c35d54d

Please sign in to comment.