-
Notifications
You must be signed in to change notification settings - Fork 54
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-2746: add export structure #1451
Changes from 1 commit
9cf7e4e
669c280
5e1e9c5
533f2ec
4501c58
b4f8757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,8 +55,14 @@ import ( | |
const CName = "export" | ||
|
||
const ( | ||
tempFileName = "temp_anytype_backup" | ||
spaceDirectory = "spaces" | ||
tempFileName = "temp_anytype_backup" | ||
spaceDirectory = "spaces" | ||
typesDirectory = "types" | ||
objectsDirectory = "objects" | ||
relationsDirectory = "relations" | ||
relationsOptionsDirectory = "relationsOptions" | ||
templatesDirectory = "templates" | ||
filesObjects = "filesObjects" | ||
) | ||
|
||
var log = logging.Logger("anytype-mw-export") | ||
|
@@ -491,7 +497,7 @@ func (e *export) writeDoc(ctx context.Context, req *pb.RpcObjectListExportReques | |
} | ||
conv.SetKnownDocs(docInfo) | ||
result := conv.Convert(b.Type().ToProto()) | ||
filename := e.provideFileName(docID, req.SpaceId, conv, st) | ||
filename := e.provideFileName(docID, req.SpaceId, conv, st, b.Type()) | ||
if req.Format == model.Export_Markdown { | ||
filename = e.provideMarkdownName(st, wr, docID, conv, req.SpaceId) | ||
} | ||
|
@@ -519,15 +525,33 @@ func (e *export) provideMarkdownName(s *state.State, wr writer, docID string, co | |
return wr.Namer().Get(path, docID, name, conv.Ext()) | ||
} | ||
|
||
func (e *export) provideFileName(docID, spaceId string, conv converter.Converter, st *state.State) string { | ||
filename := docID + conv.Ext() | ||
func (e *export) provideFileName(docID, spaceId string, conv converter.Converter, st *state.State, blockType smartblock.SmartBlockType) string { | ||
dir := e.provideFileDirectory(blockType) | ||
filename := filepath.Join(dir, docID+conv.Ext()) | ||
if spaceId == "" { | ||
spaceId := pbtypes.GetString(st.LocalDetails(), bundle.RelationKeySpaceId.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it be the case that spaceId is nil? What happens here, why if spaceId == "" we do something completely different? Maybe It shouldn't be inside this function? Can you explain please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be empty, if user want to export all spaces. We build file directory based on this value, so I guess it makes sense to check it here 🧐 |
||
filename = filepath.Join(spaceDirectory, spaceId, filename) | ||
} | ||
return filename | ||
} | ||
|
||
func (e *export) provideFileDirectory(blockType smartblock.SmartBlockType) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call it just |
||
switch blockType { | ||
case smartblock.SmartBlockTypeRelation: | ||
return relationsDirectory | ||
case smartblock.SmartBlockTypeRelationOption: | ||
return relationsOptionsDirectory | ||
case smartblock.SmartBlockTypeObjectType: | ||
return typesDirectory | ||
case smartblock.SmartBlockTypeTemplate: | ||
return templatesDirectory | ||
case smartblock.SmartBlockTypeFile, smartblock.SmartBlockTypeFileObject: | ||
return filesObjects | ||
default: | ||
return objectsDirectory | ||
} | ||
} | ||
|
||
func (e *export) saveFile(ctx context.Context, wr writer, fileObject sb.SmartBlock, exportAllSpaces bool) (fileName string, err error) { | ||
fullId := domain.FullFileId{ | ||
SpaceId: fileObject.Space().Id(), | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -58,6 +58,13 @@ func (d *dirWriter) Path() string { | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
func (d *dirWriter) WriteFile(filename string, r io.Reader, lastModifiedDate int64) (err error) { | ||||||||||||||||||||||||||||||||||
dir := filepath.Dir(filename) | ||||||||||||||||||||||||||||||||||
if dir != "" { | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the logic here, dir always returns nonempty string, for example |
||||||||||||||||||||||||||||||||||
err = os.MkdirAll(filepath.Join(d.path, dir), 0700) | ||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper error handling for directory creation. The added code correctly checks for a non-empty directory and attempts to create it. However, consider logging the error for better traceability. if dir != "" {
err = os.MkdirAll(filepath.Join(d.path, dir), 0700)
if err != nil {
+ // Log the error for better traceability
+ fmt.Printf("Failed to create directory %s: %v\n", filepath.Join(d.path, dir), err)
return err
}
} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
filename = path.Join(d.path, filename) | ||||||||||||||||||||||||||||||||||
f, err := os.Create(filename) | ||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we decided to use Id everywhere instead of ID