Add import code

This commit is contained in:
kolaente 2021-09-03 21:35:49 +02:00
parent cadeaaf8b0
commit 7049644bcc
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 117 additions and 6 deletions

View File

@ -33,7 +33,7 @@ type FileMigratorWeb struct {
func (fw *FileMigratorWeb) RegisterRoutes(g *echo.Group) {
ms := fw.MigrationStruct()
g.GET("/"+ms.Name()+"/status", fw.Status)
g.POST("/"+ms.Name()+"/migrate", fw.Migrate)
g.PUT("/"+ms.Name()+"/migrate", fw.Migrate)
}
// Migrate calls the migration method
@ -46,14 +46,18 @@ func (fw *FileMigratorWeb) Migrate(c echo.Context) error {
return handler.HandleHTTPError(err, c)
}
// Bind user request stuff
err = c.Bind(ms)
file, err := c.FormFile("import")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided: "+err.Error())
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
// Do the migration
err = ms.Migrate(user)
err = ms.Migrate(user, src, file.Size)
if err != nil {
return handler.HandleHTTPError(err, c)
}

View File

@ -45,5 +45,5 @@ type FileMigrator interface {
MigratorName
// Migrate is the interface used to migrate a user's tasks, list and other things from a file to vikunja.
// The user object is the user who's tasks will be migrated.
Migrate(user *user.User, file io.Reader) error
Migrate(user *user.User, file io.ReaderAt, size int64) error
}

Binary file not shown.

View File

@ -0,0 +1,95 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package vikunja_file
import (
"archive/zip"
"bytes"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/migration"
"code.vikunja.io/api/pkg/user"
"encoding/json"
"fmt"
"io"
"strings"
)
type VikunjaFileMigrator struct {
}
func (v *VikunjaFileMigrator) Name() string {
return "vikunja-file"
}
func (v *VikunjaFileMigrator) Migrate(user *user.User, file io.ReaderAt, size int64) error {
r, err := zip.NewReader(file, size)
if err != nil {
return fmt.Errorf("could not open import file: %s", err)
}
var dataFile *zip.File
//var filterFile *zip.File
storedFiles := make(map[string]*zip.File)
for _, f := range r.File {
if strings.HasPrefix(f.Name, "files/") {
fname := strings.ReplaceAll(f.Name, "files/", "")
storedFiles[fname] = f
continue
}
if f.Name == "data.json" {
dataFile = f
continue
}
//if f.Name == "filters.json" {
// filterFile = f
//}
}
// TODO: check we have all files
//////
// Import the bulk of Vikunja data
rc, err := dataFile.Open()
if err != nil {
return fmt.Errorf("could not open data file: %s", err)
}
defer rc.Close()
var buf bytes.Buffer
if _, err := buf.ReadFrom(rc); err != nil {
return fmt.Errorf("could not read data file: %s", err)
}
namespaces := []*models.NamespaceWithListsAndTasks{}
if err := json.Unmarshal(buf.Bytes(), &namespaces); err != nil {
return fmt.Errorf("could not read data: %s", err)
}
// Import files
// TODO
err = migration.InsertFromStructure(namespaces, user)
if err != nil {
return fmt.Errorf("could not insert data: %s", err)
}
///////
// Import filters
//filters
return nil
}

View File

@ -17,6 +17,7 @@
package v1
import (
vikunja_file "code.vikunja.io/api/pkg/modules/migration/vikunja-file"
"net/http"
microsofttodo "code.vikunja.io/api/pkg/modules/migration/microsoft-todo"
@ -91,6 +92,9 @@ func Info(c echo.Context) error {
CaldavEnabled: config.ServiceEnableCaldav.GetBool(),
EmailRemindersEnabled: config.ServiceEnableEmailReminders.GetBool(),
UserDeletionEnabled: config.ServiceEnableUserDeletion.GetBool(),
AvailableMigrators: []string{
(&vikunja_file.VikunjaFileMigrator{}).Name(),
},
Legal: legalInfo{
ImprintURL: config.LegalImprintURL.GetString(),
PrivacyPolicyURL: config.LegalPrivacyURL.GetString(),

View File

@ -47,6 +47,7 @@
package routes
import (
vikunja_file "code.vikunja.io/api/pkg/modules/migration/vikunja-file"
"errors"
"fmt"
"strings"
@ -633,6 +634,13 @@ func registerMigrations(m *echo.Group) {
}
microsoftTodoMigrationHandler.RegisterRoutes(m)
}
vikunjaFileMigrationHandler := &migrationHandler.FileMigratorWeb{
MigrationStruct: func() migration.FileMigrator {
return &vikunja_file.VikunjaFileMigrator{}
},
}
vikunjaFileMigrationHandler.RegisterRoutes(m)
}
func registerCalDavRoutes(c *echo.Group) {