feat: implement file renaming
This commit is contained in:
@ -93,7 +93,7 @@ func sendFileChunk(w http.ResponseWriter, saveFolder string, file *models.File,
|
||||
endOffset := end % chunkSize
|
||||
|
||||
for i := startChunk; i <= endChunk; i++ {
|
||||
chunkPath := filepath.Join(saveFolder, file.Name, fmt.Sprintf("chunk_%d", i))
|
||||
chunkPath := filepath.Join(saveFolder, fmt.Sprintf("chunk_%d", i))
|
||||
chunkFile, err := os.Open(chunkPath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error opening chunk: %v", err), http.StatusInternalServerError)
|
||||
|
@ -23,7 +23,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
var filesData []types.FileData
|
||||
|
||||
for _, file := range files {
|
||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String())
|
||||
|
||||
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||
chunkFiles, err := filepath.Glob(pattern)
|
||||
|
65
handler/file/rename/rename.go
Normal file
65
handler/file/rename/rename.go
Normal file
@ -0,0 +1,65 @@
|
||||
package renameFileHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func PATCH(w http.ResponseWriter, r *http.Request) {
|
||||
fileID := r.PathValue("id")
|
||||
newName := r.URL.Query().Get("name")
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
|
||||
file, err := app.Server.Database.GetFile(fileID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if userSession.UserID != file.OwnerID {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if newName == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
newFile, err := app.Server.Database.RenameFile(fileID, newName)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String())
|
||||
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||
chunkFiles, err := filepath.Glob(pattern)
|
||||
|
||||
missingChunk := err != nil || len(chunkFiles) != int(file.TotalChunk)
|
||||
|
||||
fileData := types.FileData{
|
||||
ID: newFile.ID.String(),
|
||||
Name: newFile.Name,
|
||||
Size: utils.ConvertFileSize(newFile.Size),
|
||||
IsPrivate: newFile.IsPrivate,
|
||||
Type: newFile.Type,
|
||||
Done: !missingChunk,
|
||||
Downloaded: strconv.FormatUint(newFile.Downloaded, 10),
|
||||
}
|
||||
|
||||
component := fileView.JustFile(fileData)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fileView.JustFile(fileData)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
@ -53,7 +53,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
basePath := filepath.Join(currentDir, uploadDir)
|
||||
cleanBasePath := filepath.Clean(basePath)
|
||||
|
||||
saveFolder := filepath.Join(cleanBasePath, userSession.UserID.String(), file.ID.String(), file.Name)
|
||||
saveFolder := filepath.Join(cleanBasePath, userSession.UserID.String(), file.ID.String())
|
||||
|
||||
cleanSaveFolder := filepath.Clean(saveFolder)
|
||||
|
||||
|
@ -32,7 +32,7 @@ func PUT(w http.ResponseWriter, r *http.Request) {
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String())
|
||||
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||
chunkFiles, err := filepath.Glob(pattern)
|
||||
|
||||
|
Reference in New Issue
Block a user