commit b50b4d62bec967932299e11a7bdbbef29f5efe32
parent e37f42ff973aa2cf549c01a0a163e9cf07908f94
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 2 Feb 2023 19:36:05 -0800
filedrop reconstruct to encrypt file on disk
Diffstat:
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -6,6 +6,7 @@ import (
dutils "dkforest/pkg/database/utils"
"dkforest/pkg/hashset"
pubsub2 "dkforest/pkg/pubsub"
+ "dkforest/pkg/utils/crypto"
v1 "dkforest/pkg/web/handlers/api/v1"
"encoding/base64"
"encoding/csv"
@@ -4314,7 +4315,7 @@ func FileDropHandler(c echo.Context) error {
func FileDropTmpReconstructHandler(c echo.Context) error {
filedropUUID := c.Param("uuid")
- _, err := database.GetFiledropByUUID(filedropUUID)
+ filedrop, err := database.GetFiledropByUUID(filedropUUID)
if err != nil {
return c.Redirect(http.StatusFound, "/")
}
@@ -4341,10 +4342,10 @@ func FileDropTmpReconstructHandler(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}
lines := strings.Split(string(metadata), "\n")
- fileName := lines[0]
+ origFileName := lines[0]
fileSha256 := lines[2]
- f, err := os.OpenFile(filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID, fileName), os.O_CREATE|os.O_WRONLY, 0644)
+ f, err := os.OpenFile(filepath.Join(config.Global.ProjectFiledropPath(), filedrop.FileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
logrus.Error(err)
return c.NoContent(http.StatusInternalServerError)
@@ -4352,18 +4353,24 @@ func FileDropTmpReconstructHandler(c echo.Context) error {
defer f.Close()
h := sha256.New()
+ stream, _, iv, err := crypto.NewCtrStram([]byte(config.Global.MasterKey()))
+ written := int64(0)
for _, fileName := range fileNames {
by, err := os.ReadFile(filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID, fileName))
if err != nil {
logrus.Error(err)
return c.NoContent(http.StatusInternalServerError)
}
+
+ dst := make([]byte, len(by))
_, _ = h.Write(by)
- _, err = f.Write(by)
+ stream.XORKeyStream(dst, by)
+ _, err = f.Write(dst)
if err != nil {
logrus.Error(err)
return c.NoContent(http.StatusInternalServerError)
}
+ written += int64(len(by))
}
newFileSha256 := hex.EncodeToString(h.Sum(nil))
@@ -4373,6 +4380,11 @@ func FileDropTmpReconstructHandler(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}
+ filedrop.IV = iv
+ filedrop.OrigFileName = origFileName
+ filedrop.FileSize = written
+ filedrop.DoSave()
+
return c.NoContent(http.StatusOK)
}