add webdav download file action

This commit is contained in:
tickstep 2021-12-31 00:21:53 +08:00
parent a4a74f062e
commit f70a5b2fa0
2 changed files with 80 additions and 3 deletions

View File

@ -7,6 +7,8 @@ import (
"github.com/tickstep/aliyunpan/internal/config"
"github.com/tickstep/library-go/expires"
"github.com/tickstep/library-go/expires/cachemap"
"github.com/tickstep/library-go/requester"
"net/http"
"os"
"path"
"strings"
@ -199,4 +201,66 @@ func (p *PanClientProxy) Move(oldpath, newpath string) error {
p.deleteOneFilesDirectoriesListCache(path.Dir(newpath))
return nil
}
func (p *PanClientProxy) DownloadFilePart(fileId string, offset int64, buffer []byte) (int, error) {
urlResult, err1 := p.PanUser.PanClient().GetFileDownloadUrl(&aliyunpan.GetFileDownloadUrlParam{
DriveId: p.PanDriveId,
FileId: fileId,
})
if err1 != nil {
return 0, err1
}
var resp *http.Response
var err error
var client = requester.NewHTTPClient()
apierr := p.PanUser.PanClient().DownloadFileData(
urlResult.Url,
aliyunpan.FileDownloadRange{
Offset: offset,
End: offset + int64(len(buffer)),
},
func(httpMethod, fullUrl string, headers map[string]string) (*http.Response, error) {
resp, err = client.Req(httpMethod, fullUrl, nil, headers)
if err != nil {
return nil, err
}
return resp, err
})
if apierr != nil {
return 0, apierr
}
// close socket defer
if resp != nil {
defer func() {
resp.Body.Close()
}()
}
switch resp.StatusCode {
case 200, 206:
// do nothing, continue
break
case 416: //Requested Range Not Satisfiable
fallthrough
case 403: // Forbidden
fallthrough
case 406: // Not Acceptable
return 0, apierror.NewFailedApiError("")
case 404:
return 0, apierror.NewFailedApiError("")
case 429, 509: // Too Many Requests
return 0, apierror.NewFailedApiError("")
default:
return 0, apierror.NewApiErrorWithError(fmt.Errorf("unexpected http status code, %d, %s", resp.StatusCode, resp.Status))
}
readByteCount, readErr := resp.Body.Read(buffer)
if readErr != nil && readErr.Error() != "EOF"{
return 0, readErr
}
return readByteCount, nil
}

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/tickstep/aliyunpan-api/aliyunpan"
"github.com/tickstep/library-go/logger"
"io"
"mime"
"os"
"path"
@ -166,11 +167,18 @@ type WebDavFile struct {
}
func (f WebDavFile) Close() error {
f.readPos = 0
f.writePos = 0
return nil
}
func (f WebDavFile) Read(p []byte) (int, error) {
return 0, nil
count, err := f.panClientProxy.DownloadFilePart(f.nameSnapshot.fileId, f.readPos, p)
if err != nil {
return 0, err
}
f.readPos += int64(count)
return count, nil
}
// Readdir 获取文件目录
@ -205,8 +213,13 @@ func (f WebDavFile) Readdir(count int) (fis []os.FileInfo, err error) {
}
func (f WebDavFile) Seek(off int64, whence int) (int64, error) {
f.readPos += off
return f.readPos, nil
if whence == io.SeekEnd {
return f.nameSnapshot.size - f.readPos, nil
} else if whence == io.SeekStart{
f.readPos += off
return f.readPos, nil
}
return 0, nil
}
func (f WebDavFile) Stat() (os.FileInfo, error) {