1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 01:51:07 +09:00

GO-3483: Upload image: fallback to file upload if any error occurred

This commit is contained in:
Sergey 2024-05-16 13:23:09 +02:00
parent 1f39746a86
commit d09f9fa2b3
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
6 changed files with 50 additions and 27 deletions

View file

@ -238,7 +238,13 @@ func (ind *indexer) buildDetails(ctx context.Context, id domain.FullFileId) (det
if err != nil {
return nil, "", err
}
if mill.IsImage(file.Info().Media) {
if file.Info().Mill == mill.BlobId {
details, typeKey, err = file.Details(ctx)
if err != nil {
return nil, "", err
}
} else {
image, err := ind.fileService.ImageByHash(ctx, id)
if err != nil {
return nil, "", err
@ -247,13 +253,15 @@ func (ind *indexer) buildDetails(ctx context.Context, id domain.FullFileId) (det
if err != nil {
return nil, "", err
}
typeKey = bundle.TypeKeyImage
} else {
details, typeKey, err = file.Details(ctx)
if err != nil {
return nil, "", err
}
}
// Overwrite typeKey for images in case that image is uploaded as file.
// That can be possible because some images can't be handled properly and wee fall back to
// handling them as files
if mill.IsImage(file.Info().Media) {
typeKey = bundle.TypeKeyImage
}
details.Fields[bundle.RelationKeyFileIndexingStatus.String()] = pbtypes.Int64(int64(model.FileIndexingStatus_Indexed))
return details, typeKey, nil
}

View file

@ -420,7 +420,7 @@ func (s *service) addFileNode(ctx context.Context, spaceID string, mill m.Mill,
res, err := mill.Mill(conf.Reader, conf.Name)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %w", m.ErrProcessing, err)
}
// count the result size after the applied mill

View file

@ -142,20 +142,21 @@ func (ur UploadResult) ToBlock() file.Block {
}
type uploader struct {
spaceId string
fileObjectService fileobject.Service
picker cache.ObjectGetter
block file.Block
getReader func(ctx context.Context) (*fileReader, error)
name string
lastModifiedDate int64
typeDetect bool
forceType bool
smartBlockID string
fileType model.BlockContentFileType
fileStyle model.BlockContentFileStyle
opts []files.AddOption
groupID string
spaceId string
fileObjectService fileobject.Service
picker cache.ObjectGetter
block file.Block
getReader func(ctx context.Context) (*fileReader, error)
name string
lastModifiedDate int64
typeDetect bool
forceType bool
forceUploadingAsFile bool
smartBlockID string
fileType model.BlockContentFileType
fileStyle model.BlockContentFileStyle
opts []files.AddOption
groupID string
tempDirProvider core.TempDirProvider
fileService files.Service
@ -214,6 +215,11 @@ func (u *uploader) SetType(tp model.BlockContentFileType) Uploader {
return u
}
func (u *uploader) ForceUploadingAsFile() Uploader {
u.forceUploadingAsFile = true
return u
}
func (u *uploader) SetStyle(tp model.BlockContentFileStyle) Uploader {
u.fileStyle = tp
return u
@ -440,10 +446,16 @@ func (u *uploader) Upload(ctx context.Context) (result UploadResult) {
}
var addResult *files.AddResult
if u.fileType == model.BlockContentFile_Image {
if !u.forceUploadingAsFile && u.fileType == model.BlockContentFile_Image {
addResult, err = u.fileService.ImageAdd(ctx, u.spaceId, opts...)
if errors.Is(err, image.ErrFormat) || errors.Is(err, mill.ErrFormatSupportNotEnabled) {
return u.SetType(model.BlockContentFile_File).Upload(ctx)
if errors.Is(err, image.ErrFormat) ||
errors.Is(err, mill.ErrFormatSupportNotEnabled) ||
errors.Is(err, mill.ErrProcessing) {
err = nil
if errors.Is(err, mill.ErrProcessing) {
fmt.Println(err)
}
return u.ForceUploadingAsFile().Upload(ctx)
}
if err != nil {
return UploadResult{Err: fmt.Errorf("add image to storage: %w", err)}

View file

@ -40,7 +40,7 @@ type image struct {
func selectAndSortResizeVariants(variants []*storage.FileInfo) []*storage.FileInfo {
onlyResizeVariants := variants[:0]
for _, variant := range variants {
if variant.Mill == mill.ImageResizeId {
if variant.Mill == mill.ImageResizeId || variant.Mill == mill.BlobId {
onlyResizeVariants = append(onlyResizeVariants, variant)
}
}

View file

@ -4,8 +4,10 @@ import "io"
type Blob struct{}
const BlobId = "/blob"
func (m *Blob) ID() string {
return "/blob"
return BlobId
}
func (m *Blob) Pin() bool {

View file

@ -59,6 +59,7 @@ func isImageFormatSupported(format Format) bool {
}
var ErrFormatSupportNotEnabled = errors.New("this image format support is not enabled in this build")
var ErrProcessing = fmt.Errorf("failed to process")
type ImageSize struct {
Width int