// Code scaffolded by goctl. Safe to edit. // goctl 1.9.2 package file import ( "fmt" "net/http" "strings" "github.com/youruser/base/internal/logic/file" "github.com/youruser/base/internal/storage" "github.com/youruser/base/internal/svc" "github.com/youruser/base/internal/types" "github.com/zeromicro/go-zero/rest/httpx" ) // 获取文件访问URL func GetFileUrlHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.GetFileRequest if err := httpx.Parse(r, &req); err != nil { httpx.ErrorCtx(r.Context(), w, err) return } l := file.NewGetFileUrlLogic(r.Context(), svcCtx) resp, err := l.GetFileUrl(&req) if err != nil { httpx.ErrorCtx(r.Context(), w, err) return } // If the URL starts with "local://", serve the file directly if strings.HasPrefix(resp.Url, "local://") { key := strings.TrimPrefix(resp.Url, "local://") localStorage, ok := svcCtx.Storage.(*storage.LocalStorage) if !ok { httpx.ErrorCtx(r.Context(), w, fmt.Errorf("storage type mismatch")) return } filePath := localStorage.GetFilePath(key) http.ServeFile(w, r, filePath) return } // For non-local storage, return JSON with URL httpx.OkJsonCtx(r.Context(), w, resp) } }