doodle/pkg/wallpaper/ingest.go
Noah Petherbridge 640e75ba4d Custom Wallpapers for Levels
* You can now browse for a custom wallpaper image to use with your
  levels. A platform-native file picker dialog is used (no WASM support)
* In the New/Edit Level Properties dialog, the Wallpaper drop-down
  includes an option to browse for a custom map.
* When editing an existing level: the wallpaper takes effect immediately
  in your level once the file is picked. For NEW levels, the wallpaper
  will appear once the "Continue" button is pressed.
* All common image types supported: png, jpeg, gif.
* The wallpaper is embedded in the level using the filepath
  "assets/wallpapers/custom.b64img" as a Base64-encoded blob of the
  image data.
* The `doodad show` command will list the names and sizes of files
  embedded in levels. `doodad show --attachment <name>` will get an
  attachment and print it to the console window.
* To extract a wallpaper image from a level:
  `doodad show -a assets/wallpapers/custom.b64img | base64 -d > out.png`
2021-06-06 18:59:04 -07:00

33 lines
583 B
Go

package wallpaper
import (
"os"
"io/ioutil"
"encoding/base64"
)
/*
Functions related to the ingest of custom Wallpaper images for user levels.
*/
// FileToB64 loads an image file from disk and returns the Base64 encoded
// file data, if it is a valid image and so on.
func FileToB64(filename string) (string, error) {
fh, err := os.Open(filename)
if err != nil {
return "", err
}
defer fh.Close()
bin, err := ioutil.ReadAll(fh)
if err != nil {
return "", err
}
b64 := base64.StdEncoding.EncodeToString(bin)
if err != nil {
return "", err
}
return b64, nil
}