file_js.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2015 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebitenutil
  15. import (
  16. "bytes"
  17. "io"
  18. "net/http"
  19. )
  20. type file struct {
  21. *bytes.Reader
  22. }
  23. func (f *file) Close() error {
  24. return nil
  25. }
  26. // OpenFile opens a file and returns a stream for its data.
  27. //
  28. // The path parts should be separated with slash '/' on any environments.
  29. //
  30. // OpenFile doesn't work on mobiles.
  31. //
  32. // Deprecated: as of v2.4. Use os.Open on desktops and http.Get on browsers instead.
  33. func OpenFile(path string) (ReadSeekCloser, error) {
  34. res, err := http.Get(path)
  35. if err != nil {
  36. return nil, err
  37. }
  38. defer res.Body.Close()
  39. body, err := io.ReadAll(res.Body)
  40. if err != nil {
  41. return nil, err
  42. }
  43. f := &file{bytes.NewReader(body)}
  44. return f, nil
  45. }