izapple2/resources.go
2019-10-06 01:26:00 +02:00

63 lines
1.2 KiB
Go

package apple2
import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/ivanizag/apple2/romdumps"
)
const (
internalPrefix = "<internal>/"
httpPrefix = "http://"
httpsPrefix = "https://"
)
func isInternalResource(filename string) bool {
return strings.HasPrefix(filename, internalPrefix)
}
func isHTTPResource(filename string) bool {
return strings.HasPrefix(filename, httpPrefix) ||
strings.HasPrefix(filename, httpsPrefix)
}
func loadResource(filename string) ([]uint8, error) {
var file io.Reader
if isInternalResource(filename) {
// load from embedded resource
resource := strings.TrimPrefix(filename, internalPrefix)
resourceFile, err := romdumps.Assets.Open(resource)
if err != nil {
return nil, err
}
defer resourceFile.Close()
file = resourceFile
} else if isHTTPResource(filename) {
response, err := http.Get(filename)
if err != nil {
return nil, err
}
defer response.Body.Close()
file = response.Body
} else {
diskFile, err := os.Open(filename)
if err != nil {
return nil, err
}
defer diskFile.Close()
file = diskFile
}
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return data, nil
}