From 5b2d21825c81db2a755457b65923e9a570e5b6f4 Mon Sep 17 00:00:00 2001 From: arzumify Date: Sun, 20 Oct 2024 09:31:59 +0100 Subject: [PATCH] Update storeFile to be able to remove the older file from the database when writing new information to it Signed-off-by: arzumify --- services-src/storage/main.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/services-src/storage/main.go b/services-src/storage/main.go index 33c53bf..f1e3b78 100644 --- a/services-src/storage/main.go +++ b/services-src/storage/main.go @@ -105,7 +105,8 @@ func storeFile(file nucleusLibrary.File, serviceID uuid.UUID, information librar } // Check if the user has enough space to store the file - quota, err := getQuota(file.User, information) + // Get the user's used space + used, err := getUsed(file.User, information) if err != nil { // First contact the logger service logFunc(err.Error(), 2, information) @@ -120,7 +121,15 @@ func storeFile(file nucleusLibrary.File, serviceID uuid.UUID, information librar } } - used, err := getUsed(file.User, information) + // Check if the file already exists + stats, err := os.Stat(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name)) + if err == nil { + // The file already exists, subtract the old file size from the user's used space + used -= stats.Size() + } + + // Get the user's quota + quota, err := getQuota(file.User, information) if err != nil { // First contact the logger service logFunc(err.Error(), 2, information)