|
| 1 | +// Copyright 2016 The Linux Foundation |
| 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 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "io" |
| 20 | + "io/ioutil" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/opencontainers/image-spec/schema" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +// supported autodetection types |
| 29 | +const ( |
| 30 | + typeImageLayout = "imageLayout" |
| 31 | + typeImage = "image" |
| 32 | + typeManifest = "manifest" |
| 33 | + typeManifestList = "manifestList" |
| 34 | + typeConfig = "config" |
| 35 | +) |
| 36 | + |
| 37 | +// autodetect detects the validation type for the given path |
| 38 | +// or an error if the validation type could not be resolved. |
| 39 | +func autodetect(path string) (string, error) { |
| 40 | + fi, err := os.Stat(path) |
| 41 | + if err != nil { |
| 42 | + return "", errors.Wrapf(err, "unable to access path") // err from os.Stat includes path name |
| 43 | + } |
| 44 | + |
| 45 | + if fi.IsDir() { |
| 46 | + return typeImageLayout, nil |
| 47 | + } |
| 48 | + |
| 49 | + f, err := os.Open(path) |
| 50 | + if err != nil { |
| 51 | + return "", errors.Wrap(err, "unable to open file") // os.Open includes the filename |
| 52 | + } |
| 53 | + defer f.Close() |
| 54 | + |
| 55 | + buf, err := ioutil.ReadAll(io.LimitReader(f, 512)) // read some initial bytes to detect content |
| 56 | + if err != nil { |
| 57 | + return "", errors.Wrap(err, "unable to read") |
| 58 | + } |
| 59 | + |
| 60 | + mimeType := http.DetectContentType(buf) |
| 61 | + |
| 62 | + switch mimeType { |
| 63 | + case "application/x-gzip": |
| 64 | + return typeImage, nil |
| 65 | + |
| 66 | + case "application/octet-stream": |
| 67 | + return typeImage, nil |
| 68 | + |
| 69 | + case "text/plain; charset=utf-8": |
| 70 | + // might be a JSON file, will be handled below |
| 71 | + |
| 72 | + default: |
| 73 | + return "", errors.New("unknown file type") |
| 74 | + } |
| 75 | + |
| 76 | + if _, err := f.Seek(0, os.SEEK_SET); err != nil { |
| 77 | + return "", errors.Wrap(err, "unable to seek") |
| 78 | + } |
| 79 | + |
| 80 | + header := struct { |
| 81 | + SchemaVersion int `json:"schemaVersion"` |
| 82 | + MediaType string `json:"mediaType"` |
| 83 | + Config interface{} `json:"config"` |
| 84 | + }{} |
| 85 | + |
| 86 | + if err := json.NewDecoder(f).Decode(&header); err != nil { |
| 87 | + return "", errors.Wrap(err, "unable to parse JSON") |
| 88 | + } |
| 89 | + |
| 90 | + switch { |
| 91 | + case header.MediaType == string(schema.MediaTypeManifest): |
| 92 | + return typeManifest, nil |
| 93 | + |
| 94 | + case header.MediaType == string(schema.MediaTypeManifestList): |
| 95 | + return typeManifestList, nil |
| 96 | + |
| 97 | + case header.MediaType == "" && header.SchemaVersion == 0 && header.Config != nil: |
| 98 | + // config files don't have mediaType/schemaVersion header |
| 99 | + return typeConfig, nil |
| 100 | + } |
| 101 | + |
| 102 | + return "", errors.New("unknown media type") |
| 103 | +} |
0 commit comments