package.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import (
  18. "archive/zip"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "strings"
  24. )
  25. // Manifest object
  26. //
  27. // The manifest object holds all the relevant information supplied with the
  28. // the manifest specified in the package
  29. type Manifest struct {
  30. Entry string
  31. Height, Width int
  32. }
  33. // External package
  34. //
  35. // External package contains the main html file and manifest
  36. type ExtPackage struct {
  37. EntryHtml string
  38. Manifest *Manifest
  39. }
  40. // Read file
  41. //
  42. // Read a given compressed file and returns the read bytes.
  43. // Returns an error otherwise
  44. func ReadFile(f *zip.File) ([]byte, error) {
  45. rc, err := f.Open()
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer rc.Close()
  50. content, err := ioutil.ReadAll(rc)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return content, nil
  55. }
  56. // Reads manifest
  57. //
  58. // Reads and returns a manifest object. Returns error otherwise
  59. func ReadManifest(m []byte) (*Manifest, error) {
  60. var manifest Manifest
  61. dec := json.NewDecoder(strings.NewReader(string(m)))
  62. if err := dec.Decode(&manifest); err == io.EOF {
  63. } else if err != nil {
  64. return nil, err
  65. }
  66. return &manifest, nil
  67. }
  68. // Find file in archive
  69. //
  70. // Returns the index of the given file name if it exists. -1 if file not found
  71. func FindFileInArchive(fn string, files []*zip.File) (index int) {
  72. index = -1
  73. // Find the manifest first
  74. for i, f := range files {
  75. if f.Name == fn {
  76. index = i
  77. }
  78. }
  79. return
  80. }
  81. // Open package
  82. //
  83. // Opens a prepared ethereum package
  84. // Reads the manifest file and determines file contents and returns and
  85. // the external package.
  86. func OpenPackage(fn string) (*ExtPackage, error) {
  87. r, err := zip.OpenReader(fn)
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer r.Close()
  92. manifestIndex := FindFileInArchive("manifest.json", r.File)
  93. if manifestIndex < 0 {
  94. return nil, fmt.Errorf("No manifest file found in archive")
  95. }
  96. f, err := ReadFile(r.File[manifestIndex])
  97. if err != nil {
  98. return nil, err
  99. }
  100. manifest, err := ReadManifest(f)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if manifest.Entry == "" {
  105. return nil, fmt.Errorf("Entry file specified but appears to be empty: %s", manifest.Entry)
  106. }
  107. entryIndex := FindFileInArchive(manifest.Entry, r.File)
  108. if entryIndex < 0 {
  109. return nil, fmt.Errorf("Entry file not found: '%s'", manifest.Entry)
  110. }
  111. f, err = ReadFile(r.File[entryIndex])
  112. if err != nil {
  113. return nil, err
  114. }
  115. extPackage := &ExtPackage{string(f), manifest}
  116. return extPackage, nil
  117. }