package queries import ( models "backend/gen/postgres/postgres/public/model" . "backend/gen/postgres/postgres/public/table" "fmt" "io" "os" "path/filepath" //. "github.com/go-jet/jet/v2/postgres" "github.com/google/uuid" ) // func DeleteFile(id uuid.UUID) error { // stmt := Storage.DELETE().WHERE(Storage.ID.EQ(UUID(id))) // _, err := stmt.Exec(postgres) // if err != nil { // return fmt.Errorf("failed to delete file: %w", err) // } // return nil // } // func DeleteMultFile(ids uuid.UUIDs) error { // if len(ids) == 0 { // return nil // } // var sqlIDs []Expression // for _, id := range ids { // sqlIDs = append(sqlIDs, UUID(id)) // } // stmt := Storage.DELETE().WHERE(Storage.ID.IN(sqlIDs...)) // _, err := stmt.Exec(postgres) // if err != nil { // return fmt.Errorf("failed to delete files: %w", err) // } // return nil // } // func GetStorageByTag(tag string) ([]models.Storage, error) { // var rows []models.Storage // stmt := SELECT(Storage.AllColumns).FROM(Storage).WHERE(Storage.Tag.EQ(Text(tag))) // err := stmt.Query(postgres, &rows) // if err != nil { // return nil, fmt.Errorf("failed to get storage by tag: %w", err) // } // return rows, nil // } func UploadFile(path string, mimeType string, tag string) (uuid.UUID, error) { file, err := os.Open(path) if err != nil { return uuid.UUID{}, fmt.Errorf("failed to open file: %w", err) } defer file.Close() bytes, err := io.ReadAll(file) if err != nil { return uuid.UUID{}, fmt.Errorf("failed to read file: %w", err) } to_insert := models.MediaStorage{ MimeType: mimeType, Tag: &tag, FileData: bytes, Filename: filepath.Base(file.Name()), FileSizeBytes: int32(len(bytes)), } stmt := MediaStorage.INSERT(MediaStorage.MimeType, MediaStorage.Tag, MediaStorage.FileData, MediaStorage.Filename, MediaStorage.FileSizeBytes).MODEL(to_insert).RETURNING(MediaStorage.ID) var newIdStrings []string err = stmt.Query(postgres, &newIdStrings) if err != nil { return uuid.UUID{}, fmt.Errorf("failed to insert file into DB: %w", err) } if len(newIdStrings) == 0 { return uuid.UUID{}, fmt.Errorf("no ID was returned from insert") } actualUUID, err := uuid.Parse(newIdStrings[0]) if err != nil { return uuid.UUID{}, fmt.Errorf("failed to parse UUID: %w", err) } return actualUUID, nil }