commit a0370271bfaa81f4187690fe15a9bc01966f4dde
parent c9dd02ebddaa98279a21ba714625456fbc31e54f
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Tue, 31 Jan 2023 01:42:12 -0800
improve handler
Diffstat:
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -4383,18 +4383,34 @@ func FileDropTmpInitHandler(c echo.Context) error {
if err := os.Mkdir(filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID), 0755); err != nil {
logrus.Error(err)
- return c.NoContent(http.StatusInternalServerError)
}
+ metadataPath := filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID, "metadata")
+
fileName := c.Request().PostFormValue("fileName")
fileSize := c.Request().PostFormValue("fileSize")
fileSha256 := c.Request().PostFormValue("fileSha256")
chunkSize := c.Request().PostFormValue("chunkSize")
nbChunks := c.Request().PostFormValue("nbChunks")
data := []byte(fileName + "\n" + fileSize + "\n" + fileSha256 + "\n" + chunkSize + "\n" + nbChunks + "\n")
- if err := os.WriteFile(filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID, "metadata"), data, 0644); err != nil {
- logrus.Error(err)
- return c.NoContent(http.StatusInternalServerError)
+
+ if _, err := os.Stat(metadataPath); err != nil {
+ if err := os.WriteFile(metadataPath, data, 0644); err != nil {
+ logrus.Error(err)
+ return c.String(http.StatusInternalServerError, err.Error())
+ }
+ } else {
+ by, err := os.ReadFile(metadataPath)
+ if err != nil {
+ logrus.Error(err)
+ return c.String(http.StatusInternalServerError, err.Error())
+ }
+ if bytes.Compare(by, data) != 0 {
+ err := errors.New("metadata file already exists with different configuration")
+ logrus.Error(err)
+ return c.String(http.StatusInternalServerError, err.Error())
+ }
}
+
return c.NoContent(http.StatusOK)
}