izapple2/resources.go

63 lines
1.2 KiB
Go
Raw Normal View History

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)
}
2019-10-05 23:26:00 +00:00
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 {
2019-10-05 23:26:00 +00:00
return nil, err
}
defer resourceFile.Close()
file = resourceFile
} else if isHTTPResource(filename) {
response, err := http.Get(filename)
if err != nil {
2019-10-05 23:26:00 +00:00
return nil, err
}
defer response.Body.Close()
file = response.Body
} else {
diskFile, err := os.Open(filename)
if err != nil {
2019-10-05 23:26:00 +00:00
return nil, err
}
defer diskFile.Close()
file = diskFile
}
data, err := ioutil.ReadAll(file)
if err != nil {
2019-10-05 23:26:00 +00:00
return nil, err
}
2019-10-05 23:26:00 +00:00
return data, nil
}