Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO-4017 Set imageKind on file upload #1596

Merged
merged 9 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/application/account_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (s *Service) setAccountAndProfileDetails(ctx context.Context, req *pb.RpcAc
RpcFileUploadRequest: pb.RpcFileUploadRequest{
LocalPath: req.GetAvatarLocalPath(),
Type: model.BlockContentFile_Image,
ImageKind: model.ImageKind_Icon,
},
ObjectOrigin: objectorigin.None(),
})
Expand Down
11 changes: 8 additions & 3 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ func (mw *Middleware) BlockPaste(cctx context.Context, req *pb.RpcBlockPasteRequ
log.Debug("Image requests to upload after paste:", uploadArr)
for _, r := range uploadArr {
r.ContextId = req.ContextId
req := block.UploadRequest{ObjectOrigin: objectorigin.Clipboard(), RpcBlockUploadRequest: r}
if err = bs.UploadBlockFile(nil, req, groupId); err != nil {
req := block.UploadRequest{
RpcBlockUploadRequest: r,
ObjectOrigin: objectorigin.Clipboard(),
ImageKind: model.ImageKind_AutomaticallyAdded,
}
if _, err = bs.UploadBlockFile(ctx, req, groupId, false); err != nil {
return err
}
}
Expand Down Expand Up @@ -309,7 +313,8 @@ func (mw *Middleware) BlockUpload(cctx context.Context, req *pb.RpcBlockUploadRe
}
err := mw.doBlockService(func(bs *block.Service) (err error) {
req := block.UploadRequest{RpcBlockUploadRequest: *req, ObjectOrigin: objectorigin.None()}
return bs.UploadBlockFile(nil, req, "")
_, err = bs.UploadBlockFile(ctx, req, "", false)
return err
})
if err != nil {
return response(pb.RpcBlockUploadResponseError_UNKNOWN_ERROR, err)
Expand Down
2 changes: 1 addition & 1 deletion core/block/bookmark/bookmark_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,6 @@ func (s *service) loadImage(spaceId string, title, url string) (hash string, err
if title != "" {
fileName = title
}
res := uploader.SetName(fileName).SetFile(tmpFile.Name()).Upload(ctx)
res := uploader.SetName(fileName).SetFile(tmpFile.Name()).SetImageKind(model.ImageKind_AutomaticallyAdded).Upload(ctx)
return res.FileObjectId, res.Err
}
69 changes: 17 additions & 52 deletions core/block/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
)

var ErrOptionUsedByOtherObjects = fmt.Errorf("option is used by other objects")
Expand All @@ -40,24 +39,14 @@ type FileUploadRequest struct {
type UploadRequest struct {
pb.RpcBlockUploadRequest
ObjectOrigin objectorigin.ObjectOrigin
ImageKind model.ImageKind
}

type BookmarkFetchRequest struct {
pb.RpcBlockBookmarkFetchRequest
ObjectOrigin objectorigin.ObjectOrigin
}

func (s *Service) MarkArchived(ctx session.Context, id string, archived bool) (err error) {
return cache.Do(s, id, func(b basic.CommonOperations) error {
return b.SetDetails(nil, []*model.Detail{
{
Key: "isArchived",
Value: pbtypes.Bool(archived),
},
}, true)
})
}

func (s *Service) CreateBlock(ctx session.Context, req pb.RpcBlockCreateRequest) (id string, err error) {
err = cache.DoStateCtx(s, ctx, req.ContextId, func(st *state.State, b basic.Creatable) error {
id, err = b.CreateBlock(st, req)
Expand Down Expand Up @@ -349,29 +338,21 @@ func (s *Service) FeaturedRelationRemove(ctx session.Context, contextId string,
})
}

func (s *Service) UploadBlockFile(ctx session.Context, req UploadRequest, groupID string) (err error) {
return cache.Do(s, req.ContextId, func(b file.File) error {
_, err = b.Upload(ctx, req.BlockId, file.FileSource{
Path: req.FilePath,
Url: req.Url,
Bytes: req.Bytes,
GroupID: groupID,
Origin: req.ObjectOrigin,
}, false)
return err
})
}

func (s *Service) UploadBlockFileSync(ctx session.Context, req UploadRequest) (err error) {
return cache.Do(s, req.ContextId, func(b file.File) error {
_, err = b.Upload(ctx, req.BlockId, file.FileSource{
Path: req.FilePath,
Url: req.Url,
Bytes: req.Bytes,
Origin: req.ObjectOrigin,
}, true)
func (s *Service) UploadBlockFile(
ctx session.Context, req UploadRequest, groupID string, isSync bool,
) (fileObjectId string, err error) {
err = cache.Do(s, req.ContextId, func(b file.File) error {
fileObjectId, err = b.Upload(ctx, req.BlockId, file.FileSource{
Path: req.FilePath,
Url: req.Url,
Bytes: req.Bytes,
GroupID: groupID,
Origin: req.ObjectOrigin,
ImageKind: req.ImageKind,
}, isSync)
return err
})
return fileObjectId, err
}

func (s *Service) CreateAndUploadFile(
Expand Down Expand Up @@ -403,6 +384,9 @@ func (s *Service) UploadFile(ctx context.Context, spaceId string, req FileUpload
} else if req.Url != "" {
upl.SetUrl(req.Url)
}
if req.ImageKind != model.ImageKind_Basic {
upl.SetImageKind(req.ImageKind)
}
res := upl.Upload(ctx)
if res.Err != nil {
return "", nil, res.Err
Expand Down Expand Up @@ -430,25 +414,6 @@ func (s *Service) SetFileStyle(
})
}

func (s *Service) UploadFileBlock(
contextID string, req UploadRequest,
) (fileObjectId string, err error) {
err = cache.Do(s, contextID, func(b file.File) error {
fileObjectId, err = b.Upload(nil, req.BlockId, file.FileSource{
Path: req.FilePath,
Url: req.Url,
Bytes: req.Bytes,
GroupID: "",
Origin: req.ObjectOrigin,
}, true)
if err != nil {
return err
}
return nil
})
return fileObjectId, err
}

func (s *Service) Undo(
ctx session.Context, req pb.RpcObjectUndoRequest,
) (info basic.HistoryInfo, err error) {
Expand Down
16 changes: 9 additions & 7 deletions core/block/editor/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func NewClipboard(sb smartblock.SmartBlock, file file.File, tempDirProvider core
type clipboard struct {
smartblock.SmartBlock
file file.File
tempDirProvider core.TempDirProvider
objectStore spaceindex.Store
fileService files.Service
tempDirProvider core.TempDirProvider
objectStore spaceindex.Store
fileService files.Service
fileObjectService fileobject.Service
}

Expand Down Expand Up @@ -524,11 +524,13 @@ func (cb *clipboard) pasteFiles(ctx session.Context, req *pb.RpcBlockPasteReques
},
})
s.Add(b)

if err = cb.file.UploadState(ctx, s, b.Model().Id, file.FileSource{
Bytes: fs.Data,
Path: fs.LocalPath,
Name: fs.Name,
Origin: objectorigin.Clipboard(),
Bytes: fs.Data,
Path: fs.LocalPath,
Name: fs.Name,
Origin: objectorigin.Clipboard(),
ImageKind: model.ImageKind_Basic,
}, false); err != nil {
return
}
Expand Down
21 changes: 13 additions & 8 deletions core/block/editor/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ type File interface {
}

type FileSource struct {
Path string
Url string // nolint:revive
Bytes []byte
Name string
GroupID string
Origin objectorigin.ObjectOrigin
Path string
Url string // nolint:revive
Bytes []byte
Name string
GroupID string
Origin objectorigin.ObjectOrigin
ImageKind model.ImageKind
}

type sfile struct {
Expand Down Expand Up @@ -161,8 +162,9 @@ func (sf *sfile) CreateAndUpload(ctx session.Context, req pb.RpcBlockFileCreateA
return
}
if err = sf.upload(s, newId, FileSource{
Path: req.LocalPath,
Url: req.Url,
Path: req.LocalPath,
Url: req.Url,
ImageKind: req.ImageKind,
}, false).Err; err != nil {
return
}
Expand All @@ -180,6 +182,9 @@ func (sf *sfile) upload(s *state.State, id string, source FileSource, isSync boo
return fileuploader.UploadResult{Err: fmt.Errorf("not a file block")}
}
upl := sf.newUploader(source.Origin).SetBlock(f)
if source.ImageKind != model.ImageKind_Basic {
upl.SetImageKind(source.ImageKind)
}
if source.Path != "" {
upl.SetFile(source.Path)
} else if source.Url != "" {
Expand Down
3 changes: 2 additions & 1 deletion core/block/import/common/syncer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func (s *FileSyncer) Sync(id domain.FullID, newIdsSet map[string]struct{}, b sim
BlockId: b.Model().Id,
}
}
params.ContextId = id.ObjectID
dto := block.UploadRequest{
RpcBlockUploadRequest: params,
ObjectOrigin: origin,
}
_, err := s.service.UploadFileBlock(id.ObjectID, dto)
_, err := s.service.UploadBlockFile(nil, dto, "", false)
if os.IsNotExist(err) {
return anyerror.CleanupError(err)
}
Expand Down
28 changes: 16 additions & 12 deletions core/block/import/common/syncer/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func TestFileSyncer_Sync(t *testing.T) {
simpleBlock := simple.New(block)
service := mock_syncer.NewMockBlockService(t)
params := pb.RpcBlockUploadRequest{
FilePath: "not exist",
BlockId: simpleBlock.Model().GetId(),
ContextId: id.ObjectID,
FilePath: "not exist",
BlockId: simpleBlock.Model().GetId(),
}
dto := block2.UploadRequest{
RpcBlockUploadRequest: params,
ObjectOrigin: objectorigin.Import(model.Import_Pb),
}
service.EXPECT().UploadFileBlock(id.ObjectID, dto).Return("", os.ErrNotExist)
service.EXPECT().UploadBlockFile(nil, dto, "", false).Return("", os.ErrNotExist)
syncer := NewFileSyncer(service, nil)

// when
Expand All @@ -104,14 +105,15 @@ func TestFileSyncer_Sync(t *testing.T) {
simpleBlock := simple.New(block)
service := mock_syncer.NewMockBlockService(t)
params := pb.RpcBlockUploadRequest{
FilePath: "not exist",
BlockId: simpleBlock.Model().GetId(),
ContextId: id.ObjectID,
FilePath: "not exist",
BlockId: simpleBlock.Model().GetId(),
}
dto := block2.UploadRequest{
RpcBlockUploadRequest: params,
ObjectOrigin: objectorigin.Import(model.Import_Pb),
}
service.EXPECT().UploadFileBlock(id.ObjectID, dto).Return("", fmt.Errorf("new error"))
service.EXPECT().UploadBlockFile(nil, dto, "", false).Return("", fmt.Errorf("new error"))
syncer := NewFileSyncer(service, nil)

// when
Expand All @@ -134,14 +136,15 @@ func TestFileSyncer_Sync(t *testing.T) {
simpleBlock := simple.New(block)
service := mock_syncer.NewMockBlockService(t)
params := pb.RpcBlockUploadRequest{
FilePath: "exist",
BlockId: simpleBlock.Model().GetId(),
ContextId: id.ObjectID,
FilePath: "exist",
BlockId: simpleBlock.Model().GetId(),
}
dto := block2.UploadRequest{
RpcBlockUploadRequest: params,
ObjectOrigin: objectorigin.Import(model.Import_Pb),
}
service.EXPECT().UploadFileBlock(id.ObjectID, dto).Return("fileId", nil)
service.EXPECT().UploadBlockFile(nil, dto, "", false).Return("fileId", nil)
syncer := NewFileSyncer(service, nil)

// when
Expand All @@ -163,14 +166,15 @@ func TestFileSyncer_Sync(t *testing.T) {
simpleBlock := simple.New(block)
service := mock_syncer.NewMockBlockService(t)
params := pb.RpcBlockUploadRequest{
Url: "http://example.com",
BlockId: simpleBlock.Model().GetId(),
ContextId: id.ObjectID,
Url: "http://example.com",
BlockId: simpleBlock.Model().GetId(),
}
dto := block2.UploadRequest{
RpcBlockUploadRequest: params,
ObjectOrigin: objectorigin.Import(model.Import_Pb),
}
service.EXPECT().UploadFileBlock(id.ObjectID, dto).Return("fileId", nil)
service.EXPECT().UploadBlockFile(nil, dto, "", false).Return("fileId", nil)
syncer := NewFileSyncer(service, nil)

// when
Expand Down
3 changes: 2 additions & 1 deletion core/block/import/common/syncer/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/localstore/addr"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/anyerror"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func (s *IconSyncer) handleIconImage(spaceId string, newIdsSet map[string]struct
return fileObjectId, nil
}

req := pb.RpcFileUploadRequest{LocalPath: iconImage}
req := pb.RpcFileUploadRequest{LocalPath: iconImage, ImageKind: model.ImageKind_Icon}
if strings.HasPrefix(iconImage, "http://") || strings.HasPrefix(iconImage, "https://") {
req = pb.RpcFileUploadRequest{Url: iconImage}
}
Expand Down
Loading
Loading