Updated fulgens library and used a better files api for blob storage

Signed-off-by: Arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-10-16 18:05:52 +01:00
parent 8943808264
commit 5aa3082315
3 changed files with 23 additions and 73 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.23.1
require ( require (
git.ailur.dev/ailur/fg-library/v2 v2.0.1 git.ailur.dev/ailur/fg-library/v2 v2.0.1
git.ailur.dev/ailur/fg-nucleus-library v1.0.0 git.ailur.dev/ailur/fg-nucleus-library v1.0.2
git.ailur.dev/ailur/pow v1.0.0 git.ailur.dev/ailur/pow v1.0.0
github.com/cespare/xxhash/v2 v2.3.0 github.com/cespare/xxhash/v2 v2.3.0
github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/chi/v5 v5.1.0

6
go.sum
View File

@ -1,9 +1,7 @@
git.ailur.dev/ailur/fg-library/v2 v2.0.0 h1:NanDV52W+NBu96v/HPDPGqH8NOxLp6MRrRdXLPEsgYw=
git.ailur.dev/ailur/fg-library/v2 v2.0.0/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8=
git.ailur.dev/ailur/fg-library/v2 v2.0.1 h1:ltPYXf/Om0hnMD8gr1K5bkYrfHqKPSbb0hxa0wtTnZ0= git.ailur.dev/ailur/fg-library/v2 v2.0.1 h1:ltPYXf/Om0hnMD8gr1K5bkYrfHqKPSbb0hxa0wtTnZ0=
git.ailur.dev/ailur/fg-library/v2 v2.0.1/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8= git.ailur.dev/ailur/fg-library/v2 v2.0.1/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8=
git.ailur.dev/ailur/fg-nucleus-library v1.0.0 h1:TT1V4cfka+uUpvV1zU7bc4KXFkgnsI/sIvaZDDxXk+k= git.ailur.dev/ailur/fg-nucleus-library v1.0.2 h1:EWfeab+wJKaxx/Qg5TKpvZHicA0V/NilUv2g6W97rtg=
git.ailur.dev/ailur/fg-nucleus-library v1.0.0/go.mod h1:m4gNSEypfgrUV8bXaR8NLB8zchUM59y0ellV1wp/C+I= git.ailur.dev/ailur/fg-nucleus-library v1.0.2/go.mod h1:T2mdUiXlZqb917CkNB2vwujkD/QhJDpCHLRvKuskBpY=
git.ailur.dev/ailur/pow v1.0.0 h1:eCJiZSbskcmzmwR4Nv4YrYpsZci5kfoGM9ihkXAHHoU= git.ailur.dev/ailur/pow v1.0.0 h1:eCJiZSbskcmzmwR4Nv4YrYpsZci5kfoGM9ihkXAHHoU=
git.ailur.dev/ailur/pow v1.0.0/go.mod h1:BHl7H6B6uN+q2cCCUlno6JMhqLa2A52wkbAdJbq2izA= git.ailur.dev/ailur/pow v1.0.0/go.mod h1:BHl7H6B6uN+q2cCCUlno6JMhqLa2A52wkbAdJbq2izA=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

View File

@ -4,10 +4,10 @@ import (
"database/sql" "database/sql"
"errors" "errors"
library "git.ailur.dev/ailur/fg-library/v2" library "git.ailur.dev/ailur/fg-library/v2"
nucleusLibrary "git.ailur.dev/ailur/fg-nucleus-library"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"path/filepath" "path/filepath"
"io"
"os" "os"
"time" "time"
@ -15,22 +15,6 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
type InsertFile struct {
File File `validate:"required"`
Stream io.Reader `validate:"required"`
}
type ReadFile struct {
File File `validate:"required"`
Stream io.Writer `validate:"required"`
}
type File struct {
Name string `validate:"required"`
Size int64 `validate:"required"`
User uuid.UUID `validate:"required"`
}
var ServiceInformation = library.Service{ var ServiceInformation = library.Service{
Name: "Storage", Name: "Storage",
Permissions: library.Permissions{ Permissions: library.Permissions{
@ -103,9 +87,9 @@ func logFunc(message string, messageType uint64, information library.ServiceInit
} }
} }
func storeFile(file InsertFile, serviceID uuid.UUID, information library.ServiceInitializationInformation) { func storeFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
// Create a folder for the user if it doesn't exist // Create a folder for the user if it doesn't exist
err := os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.File.User.String()), 0755) err := os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.User.String()), 0755)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -121,7 +105,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
// Check if the user has enough space to store the file // Check if the user has enough space to store the file
quota, err := getQuota(file.File.User, information) quota, err := getQuota(file.User, information)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -136,7 +120,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
} }
used, err := getUsed(file.File.User, information) used, err := getUsed(file.User, information)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -152,7 +136,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
// Check if the user has enough space to store the file // Check if the user has enough space to store the file
if used+file.File.Size > quota { if used+int64(len(file.Bytes)) > quota {
// Then send the error message to the requesting service // Then send the error message to the requesting service
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID, ServiceID: ServiceInformation.ServiceID,
@ -164,7 +148,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
// Create a folder within that for the service if it doesn't exist // Create a folder within that for the service if it doesn't exist
err = os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.File.User.String(), serviceID.String()), 0755) err = os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String()), 0755)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -180,7 +164,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
// Store the file // Store the file
fileStream, err := os.OpenFile(filepath.Join(information.Configuration["path"].(string), file.File.User.String(), serviceID.String(), file.File.Name), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) fileStream, err := os.OpenFile(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -196,7 +180,7 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
// Write the file // Write the file
_, err = io.Copy(fileStream, file.Stream) _, err = fileStream.Write(file.Bytes)
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -237,9 +221,9 @@ func storeFile(file InsertFile, serviceID uuid.UUID, information library.Service
} }
} }
func readFile(file ReadFile, serviceID uuid.UUID, information library.ServiceInitializationInformation) { func readFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
// Open the file // Open the file
fileStream, err := os.Open(filepath.Join(information.Configuration["path"].(string), file.File.User.String(), serviceID.String(), file.File.Name)) fileStream, err := os.Open(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name))
if err != nil { if err != nil {
// First contact the logger service // First contact the logger service
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -254,49 +238,17 @@ func readFile(file ReadFile, serviceID uuid.UUID, information library.ServiceIni
} }
} }
// Read the file // Return the reader
_, err = io.Copy(file.Stream, fileStream)
if err != nil {
// First contact the logger service
logFunc(err.Error(), 2, information)
// Then send the error message to the requesting service
information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID,
ForServiceID: serviceID,
MessageType: 1, // An error that's not your fault
SentAt: time.Now(),
Message: err.Error(),
}
}
// Close the file
err = fileStream.Close()
if err != nil {
// First contact the logger service
logFunc(err.Error(), 2, information)
// Then send the error message to the requesting service
information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID,
ForServiceID: serviceID,
MessageType: 1, // An error that's not your fault
SentAt: time.Now(),
Message: err.Error(),
}
}
// Report success
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID, ServiceID: ServiceInformation.ServiceID,
ForServiceID: serviceID, ForServiceID: serviceID,
MessageType: 0, // Success MessageType: 0, // Success
SentAt: time.Now(), SentAt: time.Now(),
Message: nil, Message: fileStream,
} }
} }
func removeFile(file File, serviceID uuid.UUID, information library.ServiceInitializationInformation) { func removeFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
// Remove the file // Remove the file
err := os.Remove(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name)) err := os.Remove(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name))
if err != nil { if err != nil {
@ -337,7 +289,7 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
case 0: case 0:
// Insert file // Insert file
validate := validator.New() validate := validator.New()
err := validate.Struct(message.Message.(InsertFile)) err := validate.Struct(message.Message.(nucleusLibrary.File))
if err != nil { if err != nil {
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID, ServiceID: ServiceInformation.ServiceID,
@ -348,12 +300,12 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
} }
} else { } else {
// Store file // Store file
storeFile(message.Message.(InsertFile), message.ServiceID, information) storeFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
} }
case 1: case 1:
// Read file // Read file
validate := validator.New() validate := validator.New()
err := validate.Struct(message.Message.(ReadFile)) err := validate.Struct(message.Message.(nucleusLibrary.File))
if err != nil { if err != nil {
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID, ServiceID: ServiceInformation.ServiceID,
@ -364,12 +316,12 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
} }
} else { } else {
// Read file // Read file
readFile(message.Message.(ReadFile), message.ServiceID, information) readFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
} }
case 2: case 2:
// Remove file // Remove file
validate := validator.New() validate := validator.New()
err := validate.Struct(message.Message.(File)) err := validate.Struct(message.Message.(nucleusLibrary.File))
if err != nil { if err != nil {
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID, ServiceID: ServiceInformation.ServiceID,
@ -380,7 +332,7 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
} }
} else { } else {
// Remove file // Remove file
removeFile(message.Message.(File), message.ServiceID, information) removeFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
} }
} }
} }