Add support for importing task attachments

This commit is contained in:
kolaente 2021-09-03 23:12:27 +02:00
parent 1773fdf73d
commit 0e35f0f5fb
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 21 additions and 5 deletions

View File

@ -26,6 +26,7 @@ import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
)
@ -44,11 +45,15 @@ func (v *VikunjaFileMigrator) Migrate(user *user.User, file io.ReaderAt, size in
var dataFile *zip.File
var filterFile *zip.File
storedFiles := make(map[string]*zip.File)
storedFiles := make(map[int64]*zip.File)
for _, f := range r.File {
if strings.HasPrefix(f.Name, "files/") {
fname := strings.ReplaceAll(f.Name, "files/", "")
storedFiles[fname] = f
id, err := strconv.ParseInt(fname, 10, 64)
if err != nil {
return fmt.Errorf("could not convert file id: %s", err)
}
storedFiles[id] = f
continue
}
if f.Name == "data.json" {
@ -89,13 +94,24 @@ func (v *VikunjaFileMigrator) Migrate(user *user.User, file io.ReaderAt, size in
for _, comment := range t.Comments {
comment.ID = 0
}
for _, attachment := range t.Attachments {
af, err := storedFiles[attachment.File.ID].Open()
if err != nil {
return fmt.Errorf("could not open attachment %d for reading: %s", attachment.ID, err)
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(af); err != nil {
return fmt.Errorf("could not read attachment %d: %s", attachment.ID, err)
}
attachment.ID = 0
attachment.File.ID = 0
attachment.File.FileContent = buf.Bytes()
}
}
}
}
// Import files
// TODO
err = migration.InsertFromStructure(namespaces, user)
if err != nil {
return fmt.Errorf("could not insert data: %s", err)