From 278ffad61e99cb1273f2aa260121983a90cd140a Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 29 Aug 2021 19:50:47 +0200 Subject: [PATCH] Add saving export with user --- pkg/migration/20210829194722.go | 43 +++++++++++++++++++++++++++++++++ pkg/models/export.go | 37 ++++++++++++++++++++++++++++ pkg/user/user.go | 2 ++ 3 files changed, 82 insertions(+) create mode 100644 pkg/migration/20210829194722.go diff --git a/pkg/migration/20210829194722.go b/pkg/migration/20210829194722.go new file mode 100644 index 000000000..111ce7043 --- /dev/null +++ b/pkg/migration/20210829194722.go @@ -0,0 +1,43 @@ +// 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 . + +package migration + +import ( + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" +) + +type users20210829194722 struct { + ExportFileID int64 `xorm:"bigint null" json:"-"` +} + +func (users20210829194722) TableName() string { + return "users" +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20210829194722", + Description: "Add data export file id to users", + Migrate: func(tx *xorm.Engine) error { + return tx.Sync2(users20210829194722{}) + }, + Rollback: func(tx *xorm.Engine) error { + return nil + }, + }) +} diff --git a/pkg/models/export.go b/pkg/models/export.go index ff5338ee1..5711e61eb 100644 --- a/pkg/models/export.go +++ b/pkg/models/export.go @@ -59,25 +59,62 @@ func ExportUserData(u *user.User) (err error) { err = exportListsAndTasks(s, u, dumpWriter) if err != nil { + _ = s.Rollback() return err } // Task attachment files err = exportTaskAttachments(s, u, dumpWriter) if err != nil { + _ = s.Rollback() return err } // Saved filters err = exportSavedFilters(s, u, dumpWriter) if err != nil { + _ = s.Rollback() return err } // Background files err = exportListBackgrounds(s, u, dumpWriter) if err != nil { + _ = s.Rollback() return err } // Vikunja Version err = dump.WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter) + if err != nil { + _ = s.Rollback() + return err + } + + stat, err := dumpFile.Stat() + if err != nil { + _ = s.Rollback() + return err + } + + exportFile, err := files.Create(dumpFile, dumpFile.Name(), uint64(stat.Size()), u) + if err != nil { + _ = s.Rollback() + return err + } + + // Save the file id with the user + u.ExportFileID = exportFile.ID + _, err = s.Cols("export_file_id").Update(u) + if err != nil { + _ = s.Rollback() + return + } + + err = s.Commit() + if err != nil { + _ = s.Rollback() + return err + } + + // Remove the old file + err = os.Remove(dumpFile.Name()) if err != nil { return err } diff --git a/pkg/user/user.go b/pkg/user/user.go index cec27b5a6..ac1786d98 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -98,6 +98,8 @@ type User struct { DeletionScheduledAt time.Time `xorm:"datetime null" json:"-"` DeletionLastReminderSent time.Time `xorm:"datetime null" json:"-"` + ExportFileID int64 `xorm:"bigint null" json:"-"` + // A timestamp when this task was created. You cannot change this value. Created time.Time `xorm:"created not null" json:"created"` // A timestamp when this task was last updated. You cannot change this value.