Bugfixes for file upload

This commit is contained in:
Johannes Bülow 2025-06-12 12:01:31 +02:00
parent 5a87fcdc61
commit 5b0f04c241
Signed by: jmb
GPG key ID: B56971CF7B8F83A6
4 changed files with 27 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package database
import (
"context"
"log/slog"
"encoding/hex"
"github.com/jackc/pgx/v5"
"golang.org/x/crypto/blake2b"
@ -20,19 +21,29 @@ func CreateFile(ctx context.Context, name string, fileBytes []byte) (sqlc.File,
var err error
bl2hash := blake2b.Sum256(fileBytes)
file.Blake2 = bl2hash[:]
file.Size = int64(len(fileBytes))
slog.Debug("calculated Blake2b hash", "file-name", name, "file-bl2", hex.EncodeToString(bl2hash[:]), "file-size", len(fileBytes))
mime, _ := store.GetBytesFileType(fileBytes[:262])
file.Mimetype, _ = store.GetBytesFileType(fileBytes[:262])
file, err = queries.CreateFile(ctx, sqlc.CreateFileParams{})
file, err = queries.CreateFile(ctx, sqlc.CreateFileParams{
Name: name,
Mimetype: mime,
Size: int64(len(fileBytes)),
Blake2: bl2hash[:],
})
if err == pgx.ErrNoRows {
slog.Info("File already exists", "file-uuid", file.ID.String())
file, err := queries.GetFileByBlake2(ctx, bl2hash[:])
if err != nil {
slog.Error("Error saving file to database", "error", err, "file-name", name)
return file, err
}
slog.Debug("File already exists", "file-uuid", file.ID.String(), "file-name", file.Name, "file-bl2", hex.EncodeToString(file.Blake2), "file-size", file.Size, "file-mime", file.Mimetype, "file-description", file.Description.String)
return file, nil
}
if err != nil {
slog.Error("Error saving file to database", "error", err, "file-name", name)
err = nil
} else {
slog.Debug("New file created", "file-uuid", file.ID.String(), "file-name", file.Name, "file-bl2", hex.EncodeToString(file.Blake2), "file-size", file.Size, "file-mime", file.Mimetype, "file-description", file.Description.String)
}
//Using UUIDs instead of the file hash to make switching storage backends easier

View file

@ -2,6 +2,7 @@ package database
import (
"context"
"encoding/hex"
"fmt"
"log/slog"
@ -23,6 +24,10 @@ func GetFileByID(fileID string) (sqlc.File, error) {
func InsertFileProperties(properties sqlc.InsertFilePropertiesParams) error {
query := sqlc.New(pool)
slog.Debug("InsertFileProperties", "file-uuid", properties.ID.String(), "file-sha256",
hex.EncodeToString(properties.Sha256), "file-md5", hex.EncodeToString(properties.Md5),
"file-mime", properties.LibmagicMime.String, "file-extension", properties.LibmagicExtension.String,
"file-apple", properties.LibmagicApple.String)
err := query.InsertFileProperties(context.Background(), properties)
if err != nil {
slog.Error("Unable to add file properties", "file-uuid", properties.ID.String(), "error", err)
@ -49,8 +54,10 @@ func InsertJsonResult(fileID pgtype.UUID, data []byte, table string) error {
}
if err != nil {
slog.Error("Unable to insert DIEC results", "file-uuid", fileID.String(), "error", err)
}
return err
}
slog.Debug("InsertJsonResult", "file-uuid", fileID.String(), "table", table)
return nil
}
// GetFileMime() returns the MimeType for a file

View file

@ -28,7 +28,7 @@ func BasicProcessing(job sqlc.ProcessingJob) error {
}
fileProperties := sqlc.InsertFilePropertiesParams{}
fileProperties.ID.Bytes = job.FileID.Bytes
fileProperties.ID = job.FileID
fileProperties.Md5 = md5sum[:]
fileProperties.Sha256 = sha256sum[:]
fileProperties.LibmagicMime.String = fileCmdResult.MimeType

View file

@ -78,3 +78,4 @@ func TypeFromMime(mimetype string) string {
return TypeOther
}