Start adding the importer

This commit is contained in:
kolaente 2021-08-25 18:09:56 +02:00
parent bd24743640
commit 2ebac142c7
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 132 additions and 4 deletions

View File

@ -21,6 +21,16 @@ import (
"code.vikunja.io/web"
)
// DataExportRequestEvent represents a DataExportRequestEvent event
type DataExportRequestEvent struct {
User *user.User
}
// Name defines the name for DataExportRequestEvent
func (t *DataExportRequestEvent) Name() string {
return "user.export.request"
}
/////////////////
// Task Events //
/////////////////

118
pkg/models/export.go Normal file
View File

@ -0,0 +1,118 @@
// Copyright 2021 Vikunja and contriubtors. All rights reserved.
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"archive/zip"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/modules/dump"
"code.vikunja.io/api/pkg/user"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"xorm.io/xorm"
)
func ExportUserData(u *user.User) (err error) {
exportDir := config.ServiceRootpath.GetString() + "/files/user-export-tmp/"
err = os.MkdirAll(exportDir, 0700)
if err != nil {
return err
}
// Open zip
dumpFile, err := os.Create(exportDir + strconv.FormatInt(u.ID, 64) + "_" + time.Now().Format("2006-01-02_15-03-05") + ".zip")
if err != nil {
return fmt.Errorf("error opening dump file: %s", err)
}
defer dumpFile.Close()
dumpWriter := zip.NewWriter(dumpFile)
defer dumpWriter.Close()
// Get the data
s := db.NewSession()
defer s.Close()
err = exportListsAndTasks(s, u, dumpWriter)
if err != nil {
return err
}
// Buckets
// Task attachment files
// Saved filters
// Subscription Status
// Background files
// Pack it in a zip file and save it
// Send a notification
return nil
}
func exportListsAndTasks(s *xorm.Session, u *user.User, wr *zip.Writer) (err error) {
namespaces := make(map[int64]*NamespaceWithLists)
_, err = getNamespacesWithLists(s, &namespaces, "", true, 0, -1, u.ID)
if err != nil {
return err
}
namespaceIDs, _ := getNamespaceOwnerIDs(namespaces)
if len(namespaceIDs) == 0 {
return nil
}
// Get all lists
lists, err := getListsForNamespaces(s, namespaceIDs, true)
if err != nil {
return err
}
tasks, _, _, err := getTasksForLists(s, lists, u, &taskOptions{
page: 0,
perPage: -1,
})
if err != nil {
return err
}
listMap := make(map[int64]*List)
listIDs := []int64{}
for _, n := range namespaces {
for _, l := range n.Lists {
listMap[l.ID] = l
listIDs = append(listIDs, l.ID)
}
}
for _, t := range tasks {
listMap[t.ListID].Tasks = append(listMap[t.ListID].Tasks, t)
}
data, err := json.Marshal(namespaces)
if err != nil {
return err
}
return dump.WriteBytesToZip("data.json", data, wr)
}

View File

@ -55,7 +55,7 @@ func Dump(filename string) error {
// Version
log.Info("Start dumping version file...")
err = writeBytesToZip("VERSION", []byte(version.Version), dumpWriter)
err = WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
if err != nil {
return fmt.Errorf("error saving version: %s", err)
}
@ -68,7 +68,7 @@ func Dump(filename string) error {
return fmt.Errorf("error saving database data: %s", err)
}
for t, d := range data {
err = writeBytesToZip("database/"+t+".json", d, dumpWriter)
err = WriteBytesToZip("database/"+t+".json", d, dumpWriter)
if err != nil {
return fmt.Errorf("error writing database table %s: %s", t, err)
}
@ -133,7 +133,7 @@ func writeFileToZip(filename string, writer *zip.Writer) error {
return err
}
func writeBytesToZip(filename string, data []byte, writer *zip.Writer) (err error) {
func WriteBytesToZip(filename string, data []byte, writer *zip.Writer) (err error) {
header := &zip.FileHeader{
Name: filename,
Method: compressionUsed,

View File

@ -21,7 +21,7 @@ type CreatedEvent struct {
User *User
}
// TopicName defines the name for CreatedEvent
// Name defines the name for CreatedEvent
func (t *CreatedEvent) Name() string {
return "user.created"
}