-
Notifications
You must be signed in to change notification settings - Fork 38
/
dataset.go
43 lines (36 loc) · 961 Bytes
/
dataset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package http
import (
"net/http"
"path"
"strings"
"github.com/labstack/echo/v4"
"github.com/reearth/reearth/server/internal/adapter"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/rerror"
)
func ExportDataset() echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()
u := adapter.Usecases(ctx)
param := c.Param("datasetSchemaId")
ext := path.Ext(param)
name := param[:len(param)-len(ext)]
if ext == "" {
ext = ".csv"
}
dsid, err := id.DatasetSchemaIDFrom(name)
if err != nil {
return rerror.ErrNotFound
}
res := c.Response()
if err := u.Dataset.Export(ctx, dsid, strings.TrimPrefix(ext, "."), res, func(name, contentType string) {
res.Header().Set(echo.HeaderContentType, contentType)
res.Header().Set("Content-Disposition", "attachment;filename="+name)
res.WriteHeader(http.StatusOK)
}); err != nil {
return err
}
res.Flush()
return nil
}
}