2015-01-02 08:48:07 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
2014-12-24 03:04:10 +01:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-12-17 12:03:26 +01:00
|
|
|
|
|
|
|
package ebitenutil
|
|
|
|
|
|
|
|
import (
|
2015-01-02 08:48:07 +01:00
|
|
|
"bytes"
|
2022-09-14 19:45:36 +02:00
|
|
|
"io"
|
2020-08-12 06:37:38 +02:00
|
|
|
"net/http"
|
2014-12-17 12:03:26 +01:00
|
|
|
)
|
|
|
|
|
2016-03-06 17:29:12 +01:00
|
|
|
type file struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-04 09:23:46 +02:00
|
|
|
// OpenFile opens a file and returns a stream for its data.
|
|
|
|
//
|
|
|
|
// The path parts should be separated with slash '/' on any environments.
|
|
|
|
//
|
|
|
|
// OpenFile doesn't work on mobiles.
|
|
|
|
//
|
|
|
|
// Deprecated: as of v2.4. Use os.Open on desktops and http.Get on browsers instead.
|
2016-03-29 16:52:11 +02:00
|
|
|
func OpenFile(path string) (ReadSeekCloser, error) {
|
2020-08-12 06:37:38 +02:00
|
|
|
res, err := http.Get(path)
|
2014-12-17 12:03:26 +01:00
|
|
|
if err != nil {
|
2016-03-06 17:29:12 +01:00
|
|
|
return nil, err
|
2014-12-17 12:03:26 +01:00
|
|
|
}
|
2024-04-29 02:43:11 +02:00
|
|
|
defer res.Body.Close()
|
2022-09-14 19:45:36 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2020-08-12 06:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := &file{bytes.NewReader(body)}
|
2016-03-06 17:29:12 +01:00
|
|
|
return f, nil
|
2014-12-17 12:03:26 +01:00
|
|
|
}
|