|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "dingospeed/internal/service" |
| 8 | + "dingospeed/pkg/util" |
| 9 | + |
| 10 | + "github.com/labstack/echo/v4" |
| 11 | +) |
| 12 | + |
| 13 | +// ModelscopeHandler 模型代理请求处理器 |
| 14 | +type ModelscopeHandler struct { |
| 15 | + modelscopeService *service.ModelscopeService |
| 16 | +} |
| 17 | + |
| 18 | +// NewModelscopeHandler 创建模型代理处理器实例 |
| 19 | +func NewModelscopeHandler(modelscopeService *service.ModelscopeService) *ModelscopeHandler { |
| 20 | + return &ModelscopeHandler{ |
| 21 | + modelscopeService: modelscopeService, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// ModelInfoHandler 处理模型信息查询请求 |
| 26 | +func (m *ModelscopeHandler) ModelInfoHandler(c echo.Context) error { |
| 27 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 28 | + |
| 29 | + if len(parts) < 5 { |
| 30 | + err := fmt.Errorf("请求路径格式非法") |
| 31 | + return util.ResponseError(c, err) |
| 32 | + } |
| 33 | + |
| 34 | + org, repo, repoType := parts[3], parts[4], parts[2] |
| 35 | + if err := m.modelscopeService.ForwardModelInfo(c, org, repo, repoType); err != nil { |
| 36 | + return util.ResponseError(c, err) |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// RevisionsHandler 处理模型版本查询请求 |
| 42 | +func (m *ModelscopeHandler) RevisionsHandler(c echo.Context) error { |
| 43 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 44 | + |
| 45 | + if len(parts) < 5 { |
| 46 | + err := fmt.Errorf("请求路径格式非法") |
| 47 | + return util.ResponseError(c, err) |
| 48 | + } |
| 49 | + |
| 50 | + org, repo, repoType := parts[3], parts[4], parts[2] |
| 51 | + if err := m.modelscopeService.ForwardRevisions(c, org, repo, repoType); err != nil { |
| 52 | + return util.ResponseError(c, err) |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// FileListHandler 处理模型文件列表请求 |
| 58 | +func (m *ModelscopeHandler) FileListHandler(c echo.Context) error { |
| 59 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 60 | + |
| 61 | + if len(parts) < 5 { |
| 62 | + err := fmt.Errorf("请求路径格式非法") |
| 63 | + return util.ResponseError(c, err) |
| 64 | + } |
| 65 | + |
| 66 | + org, repo, repoType := parts[3], parts[4], parts[2] |
| 67 | + if err := m.modelscopeService.ForwardFileList(c, org, repo, repoType); err != nil { |
| 68 | + return util.ResponseError(c, err) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// FileDownloadHandler 处理模型文件下载请求(支持续传) |
| 74 | +func (m *ModelscopeHandler) FileDownloadHandler(c echo.Context) error { |
| 75 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 76 | + |
| 77 | + if len(parts) < 5 { |
| 78 | + err := fmt.Errorf("请求路径格式非法") |
| 79 | + return util.ResponseError(c, err) |
| 80 | + } |
| 81 | + |
| 82 | + org, repo, repoType := parts[3], parts[4], parts[2] |
| 83 | + if err := m.modelscopeService.HandleFileDownload(c, org, repo, repoType); err != nil { |
| 84 | + return util.ResponseError(c, err) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// FileTreeHandler 处理数据集文件列表请求 |
| 90 | +func (m *ModelscopeHandler) FileTreeHandler(c echo.Context) error { |
| 91 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 92 | + |
| 93 | + if len(parts) < 5 { |
| 94 | + err := fmt.Errorf("请求路径格式非法") |
| 95 | + return util.ResponseError(c, err) |
| 96 | + } |
| 97 | + |
| 98 | + org, repo, repoType := parts[3], parts[4], parts[2] |
| 99 | + if err := m.modelscopeService.ForwardRepoTree(c, org, repo, repoType); err != nil { |
| 100 | + return util.ResponseError(c, err) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// DatasetFileTreeHandler 处理数据集文件列表请求 |
| 106 | +func (m *ModelscopeHandler) DatasetFileTreeHandler(c echo.Context) error { |
| 107 | + parts := strings.Split(strings.Trim(c.Request().URL.Path, "/"), "/") |
| 108 | + |
| 109 | + if len(parts) < 4 { |
| 110 | + err := fmt.Errorf("请求路径格式非法") |
| 111 | + return util.ResponseError(c, err) |
| 112 | + } |
| 113 | + |
| 114 | + datasetId := parts[3] |
| 115 | + if err := m.modelscopeService.ForwardRepoTreeByDatasetId(c, datasetId); err != nil { |
| 116 | + return util.ResponseError(c, err) |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments