From: Ruben Beltran del Rio Date: Tue, 4 Feb 2025 09:59:32 +0000 (+0100) Subject: Allow to delete home files / replace files X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/8cd28eb8cede1c6888cbbcd319a9617304bfc4c1 Allow to delete home files / replace files --- diff --git a/hotline/file_transfer.go b/hotline/file_transfer.go index 626cfff..4e71bd6 100644 --- a/hotline/file_transfer.go +++ b/hotline/file_transfer.go @@ -316,15 +316,15 @@ func UploadHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileTransfe // handler should have returned an error to the client indicating there was an existing file present. _, err := os.Stat(fullPath) if err == nil { - return fmt.Errorf("existing file found: %s", fullPath) + // return fmt.Errorf("existing file found: %s", fullPath) } - if errors.Is(err, fs.ErrNotExist) { + // if errors.Is(err, fs.ErrNotExist) { // If not found, open or create a new .incomplete file file, err = os.OpenFile(fullPath+IncompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return err } - } + // } defer file.Close() diff --git a/internal/mobius/friendship_quest_file_extensions.go b/internal/mobius/friendship_quest_file_extensions.go index fb23b79..1ef8ad6 100644 --- a/internal/mobius/friendship_quest_file_extensions.go +++ b/internal/mobius/friendship_quest_file_extensions.go @@ -156,3 +156,18 @@ func HandleUploadFolderWithUserFolders(cc *hotline.ClientConn, t *hotline.Transa updateTransactionPath(t, resolvedPath) return HandleUploadFolder(cc, t) } + +func HandleDeleteFileWithUserFolders(cc *hotline.ClientConn, t *hotline.Transaction) (res []hotline.Transaction) { + requestedPath, err := hotline.ReadPath(cc.FileRoot(), t.GetField(hotline.FieldFilePath).Data, nil) + if err != nil { + return res + } + + resolvedPath, err := ResolveUserPath(cc, requestedPath[len(cc.FileRoot())+1:]) + if err != nil { + return cc.NewErrReply(t, "Cannot upload to non-existent user folder.") + } + + updateTransactionPath(t, resolvedPath) + return HandleUploadFolder(cc, t) +} diff --git a/internal/mobius/transaction_handlers.go b/internal/mobius/transaction_handlers.go index e74056f..32535b2 100644 --- a/internal/mobius/transaction_handlers.go +++ b/internal/mobius/transaction_handlers.go @@ -28,7 +28,7 @@ func RegisterHandlers(srv *hotline.Server) { srv.HandleFunc(hotline.TranChatSend, HandleChatSend) srv.HandleFunc(hotline.TranDelNewsArt, HandleDelNewsArt) srv.HandleFunc(hotline.TranDelNewsItem, HandleDelNewsItem) - srv.HandleFunc(hotline.TranDeleteFile, HandleDeleteFile) + srv.HandleFunc(hotline.TranDeleteFile, HandleDeleteFileWithUserFolders) srv.HandleFunc(hotline.TranDeleteUser, HandleDeleteUser) srv.HandleFunc(hotline.TranDisconnectUser, HandleDisconnectUser) srv.HandleFunc(hotline.TranDownloadFile, HandleDownloadFile) @@ -346,6 +346,13 @@ func HandleDeleteFile(cc *hotline.ClientConn, t *hotline.Transaction) (res []hot fileName := t.GetField(hotline.FieldFileName).Data filePath := t.GetField(hotline.FieldFilePath).Data + var fp hotline.FilePath + if filePath != nil { + if _, err := fp.Write(filePath); err != nil { + return res + } + } + fullFilePath, err := hotline.ReadPath(cc.FileRoot(), filePath, fileName) if err != nil { return res @@ -363,11 +370,11 @@ func HandleDeleteFile(cc *hotline.ClientConn, t *hotline.Transaction) (res []hot switch mode := fi.Mode(); { case mode.IsDir(): - if !cc.Authorize(hotline.AccessDeleteFolder) { + if !cc.Authorize(hotline.AccessDeleteFolder) && !fp.IsUserDir() { return cc.NewErrReply(t, "You are not allowed to delete folders.") } case mode.IsRegular(): - if !cc.Authorize(hotline.AccessDeleteFile) { + if !cc.Authorize(hotline.AccessDeleteFile) && !fp.IsUserDir() { return cc.NewErrReply(t, "You are not allowed to delete files.") } } @@ -1438,7 +1445,9 @@ func HandleUploadFile(cc *hotline.ClientConn, t *hotline.Transaction) (res []hot } if _, err := cc.Server.FS.Stat(fullFilePath); err == nil { - return cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload because there is already a file named \"%v\". Try choosing a different Name.", string(fileName))) + if !fp.IsUserDir() { + return cc.NewErrReply(t, fmt.Sprintf("Cannot accept upload because there is already a file named \"%v\". Try choosing a different Name.", string(fileName))) + } } ft := cc.NewFileTransfer(hotline.FileUpload, cc.FileRoot(), fileName, filePath, transferSize)