thread-master/backend/internal/handler/static/extension_download.go

43 lines
1.3 KiB
Go
Raw Normal View History

package static
import (
"net/http"
"os"
"path/filepath"
)
func ExtensionDownloadHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path, ok := resolveExtensionZipPath()
if !ok {
http.Error(w, "extension package not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", `attachment; filename="haixun-threads-sync.zip"`)
http.ServeFile(w, r, path)
}
}
func resolveExtensionZipPath() (string, bool) {
candidates := []string{
filepath.Join("web", "public", "downloads", "haixun-threads-sync.zip"),
filepath.Join("backend", "web", "public", "downloads", "haixun-threads-sync.zip"),
filepath.Join("..", "web", "public", "downloads", "haixun-threads-sync.zip"),
filepath.Join("extension", "haixun-threads-sync.zip"),
}
if wd, err := os.Getwd(); err == nil {
candidates = append(candidates,
filepath.Join(wd, "web", "public", "downloads", "haixun-threads-sync.zip"),
filepath.Join(wd, "backend", "web", "public", "downloads", "haixun-threads-sync.zip"),
filepath.Join(wd, "..", "extension", "haixun-threads-sync.zip"),
)
}
for _, candidate := range candidates {
if info, err := os.Stat(candidate); err == nil && !info.IsDir() && info.Size() > 0 {
return candidate, true
}
}
return "", false
}