conjure-os/lib/provider/file-provider.go

54 lines
941 B
Go

package provider
import (
"conjure-os/lib/config"
"mime"
"net/http"
"os"
"path/filepath"
)
func LoadImage(gameId string, imageSrc string) *FileBlob {
imagePath := filepath.Join(config.GetDefaultConjureGamesDirectory(), gameId, imageSrc)
blob, err := GetFileBlob(imagePath)
if err != nil {
return nil
}
return blob
}
type FileBlob struct {
Name string
MimeType string
Data []byte
}
func GetFileBlob(path string) (*FileBlob, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// Try to guess MIME type from content
mimeType := http.DetectContentType(data)
// Fallback to extension-based detection
if mimeType == "application/octet-stream" {
ext := filepath.Ext(path)
if ext != "" {
if t := mime.TypeByExtension(ext); t != "" {
mimeType = t
}
}
}
name := filepath.Base(path)
return &FileBlob{
Name: name,
MimeType: mimeType,
Data: data,
}, nil
}