|
| 1 | +/* |
| 2 | +Copyright 2025 API Testing Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package apispec |
| 18 | + |
| 19 | +import ( |
| 20 | + "archive/tar" |
| 21 | + "compress/gzip" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "github.com/linuxsuren/api-testing/pkg/downloader" |
| 25 | + "github.com/linuxsuren/api-testing/pkg/util/home" |
| 26 | + "io" |
| 27 | + "net/http" |
| 28 | + "os" |
| 29 | + "path/filepath" |
| 30 | + "strings" |
| 31 | +) |
| 32 | + |
| 33 | +func DownloadSwaggerData(output string, dw downloader.PlatformAwareOCIDownloader) (err error) { |
| 34 | + dw.WithKind("data") |
| 35 | + dw.WithOS("") |
| 36 | + |
| 37 | + var reader io.Reader |
| 38 | + if reader, err = dw.Download("swagger", "", ""); err != nil { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + extFile := dw.GetTargetFile() |
| 43 | + |
| 44 | + if output == "" { |
| 45 | + output = home.GetUserDataDir() |
| 46 | + } |
| 47 | + if err = os.MkdirAll(filepath.Dir(output), 0755); err != nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + targetFile := filepath.Base(extFile) |
| 52 | + fmt.Println("start to save", filepath.Join(output, targetFile)) |
| 53 | + if err = downloader.WriteTo(reader, output, targetFile); err == nil { |
| 54 | + err = decompressData(filepath.Join(output, targetFile)) |
| 55 | + } |
| 56 | + return |
| 57 | +} |
| 58 | + |
| 59 | +func SwaggersHandler(w http.ResponseWriter, _ *http.Request, |
| 60 | + _ map[string]string) { |
| 61 | + swaggers := GetSwaggerList() |
| 62 | + if data, err := json.Marshal(swaggers); err == nil { |
| 63 | + _, _ = w.Write(data) |
| 64 | + } else { |
| 65 | + w.WriteHeader(http.StatusInternalServerError) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func GetSwaggerList() (swaggers []string) { |
| 70 | + dataDir := home.GetUserDataDir() |
| 71 | + _ = filepath.WalkDir(dataDir, func(path string, d os.DirEntry, err error) error { |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if !d.IsDir() && filepath.Ext(path) == ".json" { |
| 77 | + swaggers = append(swaggers, filepath.Base(path)) |
| 78 | + } |
| 79 | + return nil |
| 80 | + }) |
| 81 | + return |
| 82 | +} |
| 83 | + |
| 84 | +func decompressData(dataFile string) (err error) { |
| 85 | + var file *os.File |
| 86 | + file, err = os.Open(dataFile) |
| 87 | + if err != nil { |
| 88 | + return |
| 89 | + } |
| 90 | + defer file.Close() |
| 91 | + |
| 92 | + var gzipReader *gzip.Reader |
| 93 | + gzipReader, err = gzip.NewReader(file) |
| 94 | + if err != nil { |
| 95 | + return |
| 96 | + } |
| 97 | + defer gzipReader.Close() |
| 98 | + |
| 99 | + tarReader := tar.NewReader(gzipReader) |
| 100 | + |
| 101 | + for { |
| 102 | + header, err := tarReader.Next() |
| 103 | + if err == io.EOF { |
| 104 | + break // 退出循环 |
| 105 | + } |
| 106 | + if err != nil { |
| 107 | + panic(err) |
| 108 | + } |
| 109 | + |
| 110 | + // Ensure the file path does not contain directory traversal sequences |
| 111 | + if strings.Contains(header.Name, "..") { |
| 112 | + fmt.Printf("Skipping entry with unsafe path: %s\n", header.Name) |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + destPath := filepath.Join(filepath.Dir(dataFile), filepath.Base(header.Name)) |
| 117 | + |
| 118 | + switch header.Typeflag { |
| 119 | + case tar.TypeReg: |
| 120 | + destFile, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY, os.FileMode(header.Mode)) |
| 121 | + if err != nil { |
| 122 | + panic(err) |
| 123 | + } |
| 124 | + defer destFile.Close() |
| 125 | + |
| 126 | + if _, err := io.Copy(destFile, tarReader); err != nil { |
| 127 | + panic(err) |
| 128 | + } |
| 129 | + default: |
| 130 | + fmt.Printf("Skipping entry type %c: %s\n", header.Typeflag, header.Name) |
| 131 | + } |
| 132 | + } |
| 133 | + return |
| 134 | +} |
0 commit comments