Skip to content

Commit

Permalink
fix(webdav): nil pointer error (close #749)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 17, 2022
1 parent 6db09a2 commit f4f61a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
32 changes: 11 additions & 21 deletions server/webdav/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ import (

type FileSystem struct{}

var upFileMap = make(map[string]*model.File)

func (fs *FileSystem) File(rawPath string) (*model.File, error) {
rawPath = utils.ParsePath(rawPath)
if f, ok := upFileMap[rawPath]; ok {
return f, nil
}
if model.AccountsCount() > 1 && rawPath == "/" {
now := time.Now()
return &model.File{
Expand Down Expand Up @@ -156,31 +151,26 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error
return operate.MakeDir(driver, account, path_, true)
}

func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) (FileInfo, error) {
rawPath = utils.ParsePath(rawPath)
if model.AccountsCount() > 1 && rawPath == "/" {
return ErrNotImplemented
return nil, ErrNotImplemented
}
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return err
return nil, err
}
//fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64)
fileSize := uint64(r.ContentLength)
//if err != nil {
// return err
//}
filePath, fileName := filepath.Split(path_)
now := time.Now()
fi := &model.File{
Name: fileName,
Size: 0,
UpdatedAt: &now,
}
if fileSize == 0 {
upFileMap[rawPath] = &model.File{
Name: fileName,
Size: 0,
UpdatedAt: &now,
}
return nil
} else {
delete(upFileMap, rawPath)
// 如果文件大小为0,默认成功
return fi, nil
}
fileData := model.FileStream{
MIMEType: r.Header.Get("Content-Type"),
Expand All @@ -189,7 +179,7 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin
Name: fileName,
ParentPath: filePath,
}
return operate.Upload(driver, account, &fileData, true)
return fi, operate.Upload(driver, account, &fileData, true)
}

func (fs *FileSystem) Delete(rawPath string) error {
Expand Down
16 changes: 3 additions & 13 deletions server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *FileSyst
}
}

// OK
func (h *Handler) lock(now time.Time, root string, fs *FileSystem) (token string, status int, err error) {
token, err = h.LockSystem.Create(now, LockDetails{
Root: root,
Expand All @@ -110,7 +109,6 @@ func (h *Handler) lock(now time.Time, root string, fs *FileSystem) (token string
return token, 0, nil
}

// ok
func (h *Handler) confirmLocks(r *http.Request, src, dst string, fs *FileSystem) (release func(), status int, err error) {
hdr := r.Header.Get("If")
if hdr == "" {
Expand Down Expand Up @@ -189,7 +187,6 @@ func (h *Handler) confirmLocks(r *http.Request, src, dst string, fs *FileSystem)
return nil, http.StatusPreconditionFailed, ErrLocked
}

//OK
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
Expand All @@ -213,7 +210,6 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request, fs *File
return 0, nil
}

// OK
func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {

reqPath, status, err := h.stripPrefix(r.URL.Path)
Expand Down Expand Up @@ -248,7 +244,6 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
return 0, nil
}

// OK
func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {

reqPath, status, err := h.stripPrefix(r.URL.Path)
Expand All @@ -267,7 +262,6 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request, fs *FileS
return http.StatusNoContent, nil
}

// OK
func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
Expand All @@ -283,12 +277,13 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSyst
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = fs.Upload(ctx, r, reqPath)
fi, err := fs.Upload(ctx, r, reqPath)
if err != nil {
return http.StatusMethodNotAllowed, err
}

_, fi := isPathExist(ctx, fs, reqPath)
//_, fi := isPathExist(ctx, fs, reqPath)
// 为防止有些网盘上传有延时,这里认为上传没有报错就已经成功了,不再去判断是否存在
etag, err := findETag(ctx, fs, h.LockSystem, reqPath, fi)
if err != nil {
return http.StatusInternalServerError, err
Expand All @@ -297,7 +292,6 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request, fs *FileSyst
return http.StatusCreated, nil
}

// OK
func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
Expand Down Expand Up @@ -325,7 +319,6 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request, fs *FileSy
return http.StatusCreated, nil
}

// OK
func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {

hdr := r.Header.Get("Destination")
Expand Down Expand Up @@ -410,7 +403,6 @@ func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request, fs *Fil
return moveFiles(ctx, fs, src, dst, r.Header.Get("Overwrite") == "T")
}

// OK
func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *FileSystem) (retStatus int, retErr error) {

duration, err := parseTimeout(r.Header.Get("Timeout"))
Expand Down Expand Up @@ -506,7 +498,6 @@ func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request, fs *FileSys
return 0, nil
}

// OK
func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {

// http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
Expand All @@ -531,7 +522,6 @@ func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request, fs *FileS
}
}

// OK
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request, fs *FileSystem) (status int, err error) {
reqPath, status, err := h.stripPrefix(r.URL.Path)
if err != nil {
Expand Down

0 comments on commit f4f61a5

Please sign in to comment.