|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "html/template" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +func loggingMiddleware(next http.Handler) http.Handler { |
| 17 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + start := time.Now() |
| 19 | + next.ServeHTTP(w, r) |
| 20 | + duration := time.Since(start) |
| 21 | + log.Printf("Method: %s, URL: %s, Duration: %s\n", r.Method, r.URL.Path, duration) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +func indexHandler(w http.ResponseWriter, r *http.Request) { |
| 26 | + if config.Dynamic == "true" { |
| 27 | + tmpl := template.Must(template.New("index").Parse(indexDynamicTemplate)) |
| 28 | + tmpl.Execute(w, config) |
| 29 | + } else { |
| 30 | + type Tmp struct { |
| 31 | + Category []Category |
| 32 | + Config Config |
| 33 | + } |
| 34 | + var tmp = Tmp{ |
| 35 | + Category: categoryCache, // 使用缓存数据 |
| 36 | + Config: config, |
| 37 | + } |
| 38 | + tmpl := template.Must(template.New("index").Parse(indexTemplate)) |
| 39 | + tmpl.Execute(w, tmp) |
| 40 | + } |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +func categoryHandler(w http.ResponseWriter, r *http.Request) { |
| 45 | + // category := r.URL.Path[len("/category/"):] |
| 46 | + // category := filepath.FromSlash(r.URL.Path[len("/category/"):]) |
| 47 | + encodedCategory := filepath.FromSlash(r.URL.Path[len("/category/"):]) |
| 48 | + category, _ := url.PathUnescape(encodedCategory) |
| 49 | + imagePath := filepath.Join(config.ImageDir, category) |
| 50 | + cleanImageDir := filepath.Clean(config.ImageDir) |
| 51 | + absImageDir, _ := filepath.Abs(cleanImageDir) |
| 52 | + absPath, _ := filepath.Abs(imagePath) |
| 53 | + if !strings.HasPrefix(absPath, absImageDir) { |
| 54 | + http.Error(w, "无效路径", http.StatusBadRequest) |
| 55 | + return |
| 56 | + } |
| 57 | + entries, err := os.ReadDir(imagePath) |
| 58 | + if err != nil { |
| 59 | + http.Error(w, "无法读取图片目录", http.StatusInternalServerError) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + type Image struct { |
| 64 | + Name string |
| 65 | + Type string |
| 66 | + } |
| 67 | + |
| 68 | + var imageList []Image |
| 69 | + for _, entry := range entries { |
| 70 | + if entry.IsDir() { |
| 71 | + continue |
| 72 | + } |
| 73 | + ext := strings.ToLower(filepath.Ext(entry.Name())) |
| 74 | + if imageExtensions[ext] { |
| 75 | + imageList = append(imageList, Image{ |
| 76 | + Name: entry.Name(), |
| 77 | + // 添加类型字段供模板使用(可选) |
| 78 | + Type: strings.TrimPrefix(ext, "."), |
| 79 | + }) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + data := struct { |
| 84 | + Category string |
| 85 | + Images []Image |
| 86 | + Config Config |
| 87 | + }{ |
| 88 | + Category: category, |
| 89 | + Images: imageList, |
| 90 | + Config: config, |
| 91 | + } |
| 92 | + |
| 93 | + if config.Dynamic == "true" { |
| 94 | + tmpl := template.Must(template.New("category").Parse(categoryDynamicTemplate)) |
| 95 | + tmpl.Execute(w, data) |
| 96 | + } else { |
| 97 | + tmpl := template.Must(template.New("category").Parse(categoryTemplate)) |
| 98 | + tmpl.Execute(w, data) |
| 99 | + |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func indexJson(w http.ResponseWriter, r *http.Request) { |
| 104 | + // 获取分页参数 |
| 105 | + pageStr := r.URL.Query().Get("page") |
| 106 | + limitStr := r.URL.Query().Get("limit") |
| 107 | + page, err := strconv.Atoi(pageStr) |
| 108 | + if err != nil || page < 1 { |
| 109 | + page = 1 |
| 110 | + } |
| 111 | + limit, err := strconv.Atoi(limitStr) |
| 112 | + if err != nil || limit < 1 { |
| 113 | + limit = 20 |
| 114 | + } |
| 115 | + |
| 116 | + // 使用缓存的分类信息 |
| 117 | + totalCategories := len(categoryCache) |
| 118 | + totalPages := (totalCategories + limit - 1) / limit |
| 119 | + start := (page - 1) * limit |
| 120 | + end := start + limit |
| 121 | + if start > totalCategories { |
| 122 | + start = totalCategories |
| 123 | + } |
| 124 | + if end > totalCategories { |
| 125 | + end = totalCategories |
| 126 | + } |
| 127 | + currentCategories := categoryCache[start:end] |
| 128 | + |
| 129 | + w.Header().Set("Content-Type", "application/json") |
| 130 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 131 | + "categories": currentCategories, |
| 132 | + "page": page, |
| 133 | + "limit": limit, |
| 134 | + "total": totalCategories, |
| 135 | + "pages": totalPages, |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +func categoryJson(w http.ResponseWriter, r *http.Request) { |
| 140 | + encodedCategory := filepath.FromSlash(r.URL.Path[len("/api/category/"):]) |
| 141 | + category, _ := url.PathUnescape(encodedCategory) |
| 142 | + imagePath := filepath.Join(config.ImageDir, category) |
| 143 | + cleanImageDir := filepath.Clean(config.ImageDir) |
| 144 | + absImageDir, _ := filepath.Abs(cleanImageDir) |
| 145 | + absPath, _ := filepath.Abs(imagePath) |
| 146 | + if !strings.HasPrefix(absPath, absImageDir) { |
| 147 | + http.Error(w, "无效路径", http.StatusBadRequest) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + // 获取分页参数 |
| 152 | + pageStr := r.URL.Query().Get("page") |
| 153 | + limitStr := r.URL.Query().Get("limit") |
| 154 | + page, err := strconv.Atoi(pageStr) |
| 155 | + if err != nil || page < 1 { |
| 156 | + page = 1 |
| 157 | + } |
| 158 | + limit, err := strconv.Atoi(limitStr) |
| 159 | + if err != nil || limit < 1 { |
| 160 | + limit = 20 |
| 161 | + } |
| 162 | + |
| 163 | + entries, err := os.ReadDir(imagePath) |
| 164 | + if err != nil { |
| 165 | + http.Error(w, "无法读取图片目录", http.StatusInternalServerError) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + type Image struct { |
| 170 | + Name string |
| 171 | + Type string |
| 172 | + } |
| 173 | + |
| 174 | + var imageList []Image |
| 175 | + for _, entry := range entries { |
| 176 | + if entry.IsDir() { |
| 177 | + continue |
| 178 | + } |
| 179 | + ext := strings.ToLower(filepath.Ext(entry.Name())) |
| 180 | + if imageExtensions[ext] { |
| 181 | + imageList = append(imageList, Image{ |
| 182 | + Name: entry.Name(), |
| 183 | + Type: strings.TrimPrefix(ext, "."), |
| 184 | + }) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + totalImages := len(imageList) |
| 189 | + totalPages := (totalImages + limit - 1) / limit |
| 190 | + start := (page - 1) * limit |
| 191 | + end := start + limit |
| 192 | + if start > totalImages { |
| 193 | + start = totalImages |
| 194 | + } |
| 195 | + if end > totalImages { |
| 196 | + end = totalImages |
| 197 | + } |
| 198 | + currentImages := imageList[start:end] |
| 199 | + |
| 200 | + w.Header().Set("Content-Type", "application/json") |
| 201 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 202 | + "category": category, |
| 203 | + "images": currentImages, |
| 204 | + "page": page, |
| 205 | + "limit": limit, |
| 206 | + "total": totalImages, |
| 207 | + "pages": totalPages, |
| 208 | + }) |
| 209 | +} |
0 commit comments