-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1911 from anyproto/go-4270-add-virtual-file-blocks
GO-4270 - Add virtual file blocks
- Loading branch information
Showing
7 changed files
with
316 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package fileblocks | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/types" | ||
|
||
"github.com/anyproto/anytype-heart/core/block/editor/state" | ||
"github.com/anyproto/anytype-heart/core/block/editor/template" | ||
"github.com/anyproto/anytype-heart/core/block/simple" | ||
fileblock "github.com/anyproto/anytype-heart/core/block/simple/file" | ||
"github.com/anyproto/anytype-heart/core/domain" | ||
"github.com/anyproto/anytype-heart/pkg/lib/bundle" | ||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model" | ||
"github.com/anyproto/anytype-heart/util/pbtypes" | ||
) | ||
|
||
func InitEmptyFileState(st *state.State) { | ||
template.InitTemplate(st, | ||
template.WithEmpty, | ||
template.WithTitle, | ||
template.WithDefaultFeaturedRelations, | ||
template.WithFeaturedRelations, | ||
template.WithAllBlocksEditsRestricted, | ||
) | ||
} | ||
|
||
func AddFileBlocks(st *state.State, details *types.Struct, objectId string) error { | ||
fname := pbtypes.GetString(details, bundle.RelationKeyName.String()) | ||
fileType := fileblock.DetectTypeByMIME(fname, pbtypes.GetString(details, bundle.RelationKeyFileMimeType.String())) | ||
|
||
if fileType == model.BlockContentFile_Image { | ||
st.SetDetailAndBundledRelation(bundle.RelationKeyIconImage, pbtypes.String(objectId)) | ||
} | ||
|
||
blocks := buildFileBlocks(details, objectId, fname, fileType) | ||
|
||
for _, b := range blocks { | ||
if st.Exists(b.Id) { | ||
st.Set(simple.New(b)) | ||
} else { | ||
st.Add(simple.New(b)) | ||
err := st.InsertTo(st.RootId(), model.Block_Inner, b.Id) | ||
if err != nil { | ||
return fmt.Errorf("failed to insert file block: %w", err) | ||
} | ||
} | ||
} | ||
template.WithAllBlocksEditsRestricted(st) | ||
return nil | ||
} | ||
|
||
func buildFileBlocks(details *types.Struct, objectId, fname string, fileType model.BlockContentFileType) []*model.Block { | ||
var blocks []*model.Block | ||
blocks = append(blocks, &model.Block{ | ||
Id: "file", | ||
Content: &model.BlockContentOfFile{ | ||
File: &model.BlockContentFile{ | ||
Name: fname, | ||
Mime: pbtypes.GetString(details, bundle.RelationKeyFileMimeType.String()), | ||
TargetObjectId: objectId, | ||
Type: fileType, | ||
Size_: int64(pbtypes.GetFloat64(details, bundle.RelationKeySizeInBytes.String())), | ||
State: model.BlockContentFile_Done, | ||
AddedAt: int64(pbtypes.GetFloat64(details, bundle.RelationKeyAddedDate.String())), | ||
}, | ||
}}, makeFileInfoBlock(), makeRelationBlock(bundle.RelationKeyFileExt)) | ||
|
||
switch fileType { | ||
case model.BlockContentFile_Image: | ||
for _, relKey := range []domain.RelationKey{ | ||
bundle.RelationKeyWidthInPixels, | ||
bundle.RelationKeyHeightInPixels, | ||
bundle.RelationKeyCamera, | ||
bundle.RelationKeyMediaArtistName, | ||
bundle.RelationKeyMediaArtistURL, | ||
} { | ||
if notEmpty(details, relKey) { | ||
blocks = append(blocks, makeRelationBlock(relKey)) | ||
} | ||
} | ||
case model.BlockContentFile_Audio: | ||
for _, relKey := range []domain.RelationKey{ | ||
bundle.RelationKeyArtist, | ||
bundle.RelationKeyAudioAlbum, | ||
bundle.RelationKeyAudioAlbumTrackNumber, | ||
bundle.RelationKeyAudioGenre, | ||
bundle.RelationKeyAudioLyrics, | ||
bundle.RelationKeyReleasedYear, | ||
} { | ||
if notEmpty(details, relKey) { | ||
blocks = append(blocks, makeRelationBlock(relKey)) | ||
} | ||
} | ||
case model.BlockContentFile_Video: | ||
for _, relKey := range []domain.RelationKey{ | ||
bundle.RelationKeyWidthInPixels, | ||
bundle.RelationKeyHeightInPixels, | ||
bundle.RelationKeyCamera, | ||
bundle.RelationKeyCameraIso, | ||
bundle.RelationKeyAperture, | ||
bundle.RelationKeyExposure, | ||
} { | ||
if notEmpty(details, relKey) { | ||
blocks = append(blocks, makeRelationBlock(relKey)) | ||
} | ||
} | ||
} | ||
|
||
for _, relKey := range []domain.RelationKey{ | ||
bundle.RelationKeySizeInBytes, | ||
bundle.RelationKeyOrigin, | ||
bundle.RelationKeyImportType, | ||
bundle.RelationKeyAddedDate, | ||
} { | ||
if pbtypes.GetInt64(details, relKey.String()) != 0 { | ||
blocks = append(blocks, makeRelationBlock(relKey)) | ||
} | ||
} | ||
|
||
return blocks | ||
} | ||
|
||
func makeFileInfoBlock() *model.Block { | ||
return &model.Block{ | ||
Id: "info", | ||
Content: &model.BlockContentOfText{ | ||
Text: &model.BlockContentText{ | ||
Text: "File Information", | ||
Marks: &model.BlockContentTextMarks{ | ||
Marks: []*model.BlockContentTextMark{{ | ||
Range: &model.Range{ | ||
From: 0, | ||
To: 16, | ||
}, | ||
Type: model.BlockContentTextMark_Bold, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func notEmpty(details *types.Struct, relKey domain.RelationKey) bool { | ||
return pbtypes.GetInt64(details, relKey.String()) != 0 || pbtypes.GetString(details, relKey.String()) != "" | ||
} | ||
|
||
func makeRelationBlock(relationKey domain.RelationKey) *model.Block { | ||
return &model.Block{ | ||
Id: relationKey.String(), | ||
Content: &model.BlockContentOfRelation{ | ||
Relation: &model.BlockContentRelation{ | ||
Key: relationKey.String(), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package fileblocks | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gogo/protobuf/types" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/anyproto/anytype-heart/core/block/editor/state" | ||
"github.com/anyproto/anytype-heart/core/block/simple" | ||
"github.com/anyproto/anytype-heart/core/domain" | ||
"github.com/anyproto/anytype-heart/pkg/lib/bundle" | ||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model" | ||
"github.com/anyproto/anytype-heart/util/pbtypes" | ||
) | ||
|
||
func TestAddFileBlocks(t *testing.T) { | ||
id := "some_file" | ||
|
||
for _, tc := range []struct { | ||
name string | ||
details *types.Struct | ||
expectedRelations []domain.RelationKey | ||
}{ | ||
{ | ||
"image", | ||
&types.Struct{Fields: map[string]*types.Value{ | ||
bundle.RelationKeyName.String(): pbtypes.String("photo.jpeg"), | ||
bundle.RelationKeyFileMimeType.String(): pbtypes.String("image/jpeg"), | ||
bundle.RelationKeyWidthInPixels.String(): pbtypes.Int64(400), | ||
bundle.RelationKeyHeightInPixels.String(): pbtypes.Int64(600), | ||
bundle.RelationKeyAddedDate.String(): pbtypes.Int64(time.Now().Unix()), | ||
}}, | ||
[]domain.RelationKey{bundle.RelationKeyFileExt, bundle.RelationKeyWidthInPixels, bundle.RelationKeyHeightInPixels, bundle.RelationKeyAddedDate}, | ||
}, | ||
{ | ||
"plain file", | ||
&types.Struct{Fields: map[string]*types.Value{ | ||
bundle.RelationKeyName.String(): pbtypes.String("txt.txt"), | ||
bundle.RelationKeySizeInBytes.String(): pbtypes.Int64(24000), | ||
bundle.RelationKeyOrigin.String(): pbtypes.Int64(int64(model.ObjectOrigin_dragAndDrop)), | ||
bundle.RelationKeyAddedDate.String(): pbtypes.Int64(time.Now().Unix()), | ||
}}, | ||
[]domain.RelationKey{bundle.RelationKeyFileExt, bundle.RelationKeySizeInBytes, bundle.RelationKeyOrigin, bundle.RelationKeyAddedDate}, | ||
}, | ||
{ | ||
"audio", | ||
&types.Struct{Fields: map[string]*types.Value{ | ||
bundle.RelationKeyName.String(): pbtypes.String("song.mp3"), | ||
bundle.RelationKeyFileMimeType.String(): pbtypes.String("audio/mp3"), | ||
bundle.RelationKeySizeInBytes.String(): pbtypes.Int64(2400000), | ||
bundle.RelationKeyAudioAlbum.String(): pbtypes.String("Never mind"), | ||
bundle.RelationKeyAudioAlbumTrackNumber.String(): pbtypes.Int64(13), | ||
bundle.RelationKeyOrigin.String(): pbtypes.Int64(int64(model.ObjectOrigin_clipboard)), | ||
bundle.RelationKeyImportType.String(): pbtypes.Int64(2), | ||
}}, | ||
[]domain.RelationKey{bundle.RelationKeyFileExt, bundle.RelationKeySizeInBytes, bundle.RelationKeyAudioAlbum, bundle.RelationKeyAudioAlbumTrackNumber, bundle.RelationKeyOrigin, bundle.RelationKeyImportType}, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("add file blocks: %s", tc.name), func(t *testing.T) { | ||
// given | ||
st := state.NewDoc(id, map[string]simple.Block{ | ||
id: simple.New(&model.Block{Id: id}), | ||
}).NewState() | ||
|
||
// when | ||
err := AddFileBlocks(st, tc.details, id) | ||
|
||
// then | ||
assert.NoError(t, err) | ||
assertBlocks(t, st.Blocks(), tc.expectedRelations) | ||
}) | ||
} | ||
} | ||
|
||
func assertBlocks(t *testing.T, blocks []*model.Block, relations []domain.RelationKey) { | ||
counter := 0 | ||
var txtFound, fileFound bool | ||
for _, block := range blocks { | ||
rb := block.GetRelation() | ||
if rb != nil { | ||
assert.Contains(t, relations, domain.RelationKey(rb.Key)) | ||
counter++ | ||
continue | ||
} | ||
|
||
txt := block.GetText() | ||
if txt != nil { | ||
assert.Equal(t, "File Information", txt.GetText()) | ||
txtFound = true | ||
continue | ||
} | ||
|
||
file := block.GetFile() | ||
if file != nil { | ||
fileFound = true | ||
} | ||
} | ||
assert.Equal(t, counter, len(relations)) | ||
assert.True(t, txtFound) | ||
assert.True(t, fileFound) | ||
} |
Oops, something went wrong.