1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

GO-2078: fix tests and linter

Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
AnastasiaShemyakinskaya 2023-12-06 13:14:02 +03:00
parent 4667703224
commit 4a9698bb06
No known key found for this signature in database
GPG key ID: CCD60ED83B103281
5 changed files with 46 additions and 28 deletions

View file

@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"image"
"io"
@ -24,6 +25,7 @@ import (
"github.com/anyproto/anytype-heart/pkg/lib/core"
"github.com/anyproto/anytype-heart/pkg/lib/mill"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/constant"
oserror "github.com/anyproto/anytype-heart/util/os"
"github.com/anyproto/anytype-heart/util/uri"
)
@ -383,33 +385,16 @@ func (u *uploader) Upload(ctx context.Context) (result UploadResult) {
opts = append(opts, u.opts...)
}
if u.fileType == model.BlockContentFile_Image && !(filepath.Ext(u.name) == files.SvgExt) {
im, e := u.fileService.ImageAdd(ctx, u.spaceID, opts...)
if e == image.ErrFormat || e == mill.ErrFormatSupportNotEnabled {
e = nil
return u.SetType(model.BlockContentFile_File).Upload(ctx)
}
if e != nil {
err = e
if u.fileType == model.BlockContentFile_Image && !(filepath.Ext(u.name) == constant.SvgExt) {
err = u.addImage(ctx, opts, &result)
if err != nil {
return
}
result.Hash = im.Hash()
orig, _ := im.GetOriginalFile(ctx)
if orig != nil {
result.MIME = orig.Meta().Media
result.Size = orig.Meta().Size
}
} else {
fl, e := u.fileService.FileAdd(ctx, u.spaceID, opts...)
if e != nil {
err = e
err = u.addFile(ctx, opts, &result)
if err != nil {
return
}
result.Hash = fl.Hash()
if meta := fl.Meta(); meta != nil {
result.MIME = meta.Media
result.Size = meta.Size
}
}
// Touch the file to activate indexing
@ -434,6 +419,39 @@ func (u *uploader) Upload(ctx context.Context) (result UploadResult) {
return
}
func (u *uploader) addFile(ctx context.Context, opts []files.AddOption, result *UploadResult) error {
fl, err := u.fileService.FileAdd(ctx, u.spaceID, opts...)
if err != nil {
return err
}
result.Hash = fl.Hash()
if meta := fl.Meta(); meta != nil {
result.MIME = meta.Media
result.Size = meta.Size
}
return nil
}
func (u *uploader) addImage(ctx context.Context, opts []files.AddOption, result *UploadResult) error {
im, err := u.fileService.ImageAdd(ctx, u.spaceID, opts...)
if errors.Is(err, image.ErrFormat) || errors.Is(err, mill.ErrFormatSupportNotEnabled) {
return nil
}
if err != nil {
return err
}
result.Hash = im.Hash()
orig, err := im.GetOriginalFile(ctx)
if err != nil {
log.Errorf("failed to get original image: %v", err)
}
if orig != nil {
result.MIME = orig.Meta().Media
result.Size = orig.Meta().Size
}
return nil
}
func (u *uploader) detectType(buf *fileReader) model.BlockContentFileType {
b, err := buf.Peek(8192)
if err != nil && err != io.EOF {

View file

@ -8,10 +8,10 @@ import (
"github.com/anyproto/anytype-heart/core/block/simple"
"github.com/anyproto/anytype-heart/core/block/simple/base"
"github.com/anyproto/anytype-heart/core/files"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/mill"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/constant"
"github.com/anyproto/anytype-heart/util/pbtypes"
)
@ -208,7 +208,7 @@ func (f *File) FillFileHashes(hashes []string) []string {
}
func DetectTypeByMIME(name, mime string) model.BlockContentFileType {
if filepath.Ext(name) == files.SvgExt {
if filepath.Ext(name) == constant.SvgExt {
return model.BlockContentFile_Image
}
if mill.IsImage(mime) {

View file

@ -1,3 +0,0 @@
package files
const SvgExt = ".svg"

View file

@ -23,6 +23,7 @@ import (
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/localstore/objectstore"
"github.com/anyproto/anytype-heart/pkg/lib/logging"
"github.com/anyproto/anytype-heart/util/constant"
"github.com/anyproto/anytype-heart/util/netutil"
)
@ -353,7 +354,7 @@ func (g *gateway) handleSVGFile(ctx context.Context, id domain.FullID, err error
if fErr != nil {
return nil, nil, fmt.Errorf("get image by hash: %w", err)
}
if filepath.Ext(file.Info().Name) == files.SvgExt {
if filepath.Ext(file.Info().Name) == constant.SvgExt {
reader, err := file.Reader(ctx)
file.Info().Media = "image/svg+xml"
return file, reader, err

View file

@ -1,3 +1,5 @@
package constant
const ProfileFile = "profile"
const SvgExt = ".svg"