Skip to main content

Media Bucket Query Documentation

This code provides functions for querying and manipulating media bucket data in the database. It uses the PocketBase DAO for database interactions and includes functions for adding, replacing, and deleting media files.

Functions

AddMediaFromBase64

func AddMediaFromBase64(app core.App, dao *daos.Dao, req *entity.AddMediaBucketRequest) (*models.Record, error)

Adds a new media file from a base64-encoded string to the media bucket.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • req: A pointer to an AddMediaBucketRequest struct containing the media file data.
  • Returns: A pointer to a models.Record containing the inserted record, or an error if the operation fails.

ReplaceMediaFromBase64

func ReplaceMediaFromBase64(app core.App, dao *daos.Dao, mediaID string, req *entity.AddMediaBucketRequest) (*models.Record, error)

Replaces an existing media file with a new one from a base64-encoded string.

  • app: The PocketBase app instance.
  • dao: The PocketBase DAO.
  • mediaID: The ID of the existing media file.
  • req: A pointer to an AddMediaBucketRequest struct containing the new media file data.
  • Returns: A pointer to a models.Record containing the updated record, or an error if the operation fails.

DeleteMediaByID

func DeleteMediaByID(dao *daos.Dao, mediaID string) error

Deletes a media file by its ID.

  • dao: The PocketBase DAO.
  • mediaID: The ID of the media file to be deleted.
  • Returns: An error if the operation fails.

prepareFileFromBase64

func prepareFileFromBase64(file, mediaName string) (*filesystem.File, string, float64, error)

Prepares a file from a base64-encoded string.

  • file: The base64-encoded string of the file.
  • mediaName: The name of the media file.
  • Returns: A pointer to a filesystem.File, the MIME type of the file, the size of the file in bytes, and an error if the operation fails.

Example Usage

Initializing the DAO

Before using the functions, you need to initialize the PocketBase DAO.

package main

import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())
}

Adding a Media File from Base64

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create a new media bucket request
newMediaRequest := &entity.AddMediaBucketRequest{
File: "base64-encoded-file",
MediaName: "example_media",
}

// Add the new media file
record, err := queries.AddMediaFromBase64(pb, dao, newMediaRequest)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Added Media Record:", record)
}

Replacing a Media File from Base64

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create a new media bucket request
newMediaRequest := &entity.AddMediaBucketRequest{
File: "new-base64-encoded-file",
MediaName: "example_media",
}

// Replace the existing media file
mediaID := "media123"
record, err := queries.ReplaceMediaFromBase64(pb, dao, mediaID, newMediaRequest)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Replaced Media Record:", record)
}

Deleting a Media File by ID

package main

import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Delete the media file by ID
mediaID := "media123"
err := queries.DeleteMediaByID(dao, mediaID)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Media file deleted successfully")
}

This documentation provides an overview of the media bucket query functions, their parameters, and example usage. By following the examples, you can add, replace, and delete media files in the database.