Skip to content

Commit

Permalink
Merge branch 'main' into feat/workspace_setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTongci authored Oct 9, 2024
2 parents d5825d3 + 1d52c95 commit bc56bf1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 53 deletions.
23 changes: 19 additions & 4 deletions server/internal/app/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package app

import (
"fmt"
"io"
"mime"
"net/http"
"path"
"time"

"github.com/labstack/echo/v4"
"github.com/reearth/reearth/server/internal/usecase/gateway"
Expand All @@ -24,6 +26,7 @@ func serveFiles(
return func(ctx echo.Context) error {
reader, filename, err := handler(ctx)
if err != nil {
fmt.Printf("file handler err: %s\n", err.Error())
return err
}
ct := "application/octet-stream"
Expand All @@ -50,13 +53,25 @@ func serveFiles(
"/export/:filename",
fileHandler(func(ctx echo.Context) (io.Reader, string, error) {
filename := ctx.Param("filename")

r, err := repo.ReadExportProjectZip(ctx.Request().Context(), filename)
if err != nil {
return nil, "", rerror.ErrNotFound
return nil, "", err
}
// download and then delete
err = repo.RemoveExportProjectZip(ctx.Request().Context(), filename)
return r, filename, err
fmt.Printf("download: %s \n", filename)

go func() {
// download and then delete
time.Sleep(3 * time.Second)
err := repo.RemoveExportProjectZip(ctx.Request().Context(), filename)
if err != nil {
fmt.Printf("delete err: %s \n", err.Error())
} else {
fmt.Printf("file deleted: %s \n", filename)
}
}()

return r, filename, nil
}),
)

Expand Down
58 changes: 9 additions & 49 deletions server/internal/infrastructure/gcs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (f *fileRepo) RemoveAsset(ctx context.Context, u *url.URL) error {
func (f *fileRepo) ReadPluginFile(ctx context.Context, pid id.PluginID, filename string) (io.ReadCloser, error) {
sn := sanitize.Path(filename)
if sn == "" {
return nil, rerror.ErrNotFound
return nil, gateway.ErrInvalidFile
}
return f.read(ctx, path.Join(gcsPluginBasePath, pid.String(), sn))
}
Expand All @@ -133,7 +133,7 @@ func (f *fileRepo) RemovePlugin(ctx context.Context, pid id.PluginID) error {

func (f *fileRepo) ReadBuiltSceneFile(ctx context.Context, name string) (io.ReadCloser, error) {
if name == "" {
return nil, rerror.ErrNotFound
return nil, gateway.ErrInvalidFile
}
return f.read(ctx, path.Join(gcsMapBasePath, sanitize.Path(name)+".json"))
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (f *fileRepo) RemoveBuiltScene(ctx context.Context, name string) error {

func (f *fileRepo) ReadStoryFile(ctx context.Context, name string) (io.ReadCloser, error) {
if name == "" {
return nil, rerror.ErrNotFound
return nil, gateway.ErrInvalidFile
}
return f.read(ctx, path.Join(gcsStoryBasePath, sanitize.Path(name)+".json"))
}
Expand Down Expand Up @@ -208,20 +208,15 @@ func (f *fileRepo) RemoveStory(ctx context.Context, name string) error {
func (f *fileRepo) ReadExportProjectZip(ctx context.Context, name string) (io.ReadCloser, error) {
sn := sanitize.Path(name)
if sn == "" {
return nil, rerror.ErrNotFound
return nil, gateway.ErrInvalidFile
}
bucket, err := f.bucket(ctx)
r, err := f.read(ctx, path.Join(gcsExportBasePath, sn))
if err != nil {
log.Errorfc(ctx, "gcs: read bucket err: %+v\n", err)
return nil, rerror.ErrInternalByWithContext(ctx, err)
}
if ok := checkPrefix(ctx, bucket, gcsExportBasePath); !ok {
return nil, errors.New("Prefix not exist")
}
if ok := checkObject(ctx, bucket, gcsExportBasePath, path.Join(gcsExportBasePath, sn)); !ok {
return nil, errors.New("Object not exist")
if errors.Is(err, rerror.ErrNotFound) {
r, err = f.read(ctx, path.Join(gcsExportBasePath, name))
}
}
return f.read(ctx, path.Join(gcsExportBasePath, sn))
return r, err
}

func (f *fileRepo) UploadExportProjectZip(ctx context.Context, zipFile afero.File) error {
Expand Down Expand Up @@ -415,38 +410,3 @@ func newAssetID() string {
// TODO: replace
return id.NewAssetID().String()
}

func checkPrefix(ctx context.Context, bucket *storage.BucketHandle, prefix string) bool {
it := bucket.Objects(ctx, &storage.Query{
Delimiter: "/",
})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
fmt.Printf("attrs.Prefix %s \n", attrs.Prefix)
if attrs.Prefix == prefix+"/" {
return true
}
}
return false
}

func checkObject(ctx context.Context, bucket *storage.BucketHandle, prefix string, objectName string) bool {
it := bucket.Objects(ctx, &storage.Query{
Prefix: prefix + "/",
Delimiter: "/",
})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
fmt.Printf("attrs.Name %s \n", attrs.Name)
if attrs.Name == objectName {
return true
}
}
return false
}

0 comments on commit bc56bf1

Please sign in to comment.