Fix import conflict

This commit is contained in:
kolaente 2021-08-29 19:57:38 +02:00
parent 278ffad61e
commit 2e6a69a802
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 79 additions and 54 deletions

View File

@ -21,9 +21,9 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/modules/dump"
"code.vikunja.io/api/pkg/notifications"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/api/pkg/utils"
"code.vikunja.io/api/pkg/version"
"archive/zip"
@ -81,7 +81,7 @@ func ExportUserData(u *user.User) (err error) {
return err
}
// Vikunja Version
err = dump.WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
err = utils.WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
if err != nil {
_ = s.Rollback()
return err
@ -189,7 +189,7 @@ func exportListsAndTasks(s *xorm.Session, u *user.User, wr *zip.Writer) (err err
return err
}
return dump.WriteBytesToZip("data.json", data, wr)
return utils.WriteBytesToZip("data.json", data, wr)
}
func exportTaskAttachments(s *xorm.Session, u *user.User, wr *zip.Writer) (err error) {
@ -227,7 +227,7 @@ func exportTaskAttachments(s *xorm.Session, u *user.User, wr *zip.Writer) (err e
fs[ta.FileID] = ta.File.File
}
return dump.WriteFilesToZip(fs, wr)
return utils.WriteFilesToZip(fs, wr)
}
func exportSavedFilters(s *xorm.Session, u *user.User, wr *zip.Writer) (err error) {
@ -241,7 +241,7 @@ func exportSavedFilters(s *xorm.Session, u *user.User, wr *zip.Writer) (err erro
return err
}
return dump.WriteBytesToZip("filters.json", data, wr)
return utils.WriteBytesToZip("filters.json", data, wr)
}
func exportListBackgrounds(s *xorm.Session, u *user.User, wr *zip.Writer) (err error) {
@ -273,5 +273,5 @@ func exportListBackgrounds(s *xorm.Session, u *user.User, wr *zip.Writer) (err e
fs[l.BackgroundFileID] = bgFile.File
}
return dump.WriteFilesToZip(fs, wr)
return utils.WriteFilesToZip(fs, wr)
}

View File

@ -18,22 +18,17 @@ package dump
import (
"archive/zip"
"fmt"
"io"
"os"
"strconv"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/utils"
"code.vikunja.io/api/pkg/version"
"fmt"
"github.com/spf13/viper"
"io"
"os"
)
// Change to deflate to gain better compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
const compressionUsed = zip.Deflate
// Dump creates a zip file with all vikunja files at filename
func Dump(filename string) error {
dumpFile, err := os.Create(filename)
@ -55,7 +50,7 @@ func Dump(filename string) error {
// Version
log.Info("Start dumping version file...")
err = WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
err = utils.WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
if err != nil {
return fmt.Errorf("error saving version: %s", err)
}
@ -68,7 +63,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 = utils.WriteBytesToZip("database/"+t+".json", d, dumpWriter)
if err != nil {
return fmt.Errorf("error writing database table %s: %s", t, err)
}
@ -82,7 +77,7 @@ func Dump(filename string) error {
return fmt.Errorf("error saving file: %s", err)
}
err = WriteFilesToZip(allFiles, dumpWriter)
err = utils.WriteFilesToZip(allFiles, dumpWriter)
if err != nil {
return err
}
@ -114,7 +109,7 @@ func writeFileToZip(filename string, writer *zip.Writer) error {
}
header.Name = info.Name()
header.Method = compressionUsed
header.Method = utils.CompressionUsed
w, err := writer.CreateHeader(header)
if err != nil {
@ -123,38 +118,3 @@ func writeFileToZip(filename string, writer *zip.Writer) error {
_, err = io.Copy(w, fileToZip)
return err
}
func WriteBytesToZip(filename string, data []byte, writer *zip.Writer) (err error) {
header := &zip.FileHeader{
Name: filename,
Method: compressionUsed,
}
w, err := writer.CreateHeader(header)
if err != nil {
return err
}
_, err = w.Write(data)
return
}
// WriteFilesToZip writes a bunch of files from the db to a zip file. It exprects a map with the file id
// as key and its content as io.ReadCloser.
func WriteFilesToZip(files map[int64]io.ReadCloser, wr *zip.Writer) (err error) {
for fid, file := range files {
header := &zip.FileHeader{
Name: "files/" + strconv.FormatInt(fid, 10),
Method: compressionUsed,
}
w, err := wr.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(w, file)
if err != nil {
return fmt.Errorf("error writing file %d: %s", fid, err)
}
_ = file.Close()
}
return nil
}

View File

@ -33,6 +33,7 @@ import (
"code.vikunja.io/api/pkg/initialize"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/migration"
"src.techknowlogick.com/xormigrate"
)
@ -194,6 +195,7 @@ func Restore(filename string) error {
///////
// Done
log.Infof("Done restoring dump.")
log.Infof("Restart Vikunja to make sure the new configuration file is applied.")
return nil
}

63
pkg/utils/write_to_zip.go Normal file
View File

@ -0,0 +1,63 @@
// 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 utils
import (
"archive/zip"
"fmt"
"io"
"strconv"
)
// Change to deflate to gain better compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
const CompressionUsed = zip.Deflate
func WriteBytesToZip(filename string, data []byte, writer *zip.Writer) (err error) {
header := &zip.FileHeader{
Name: filename,
Method: CompressionUsed,
}
w, err := writer.CreateHeader(header)
if err != nil {
return err
}
_, err = w.Write(data)
return
}
// WriteFilesToZip writes a bunch of files from the db to a zip file. It exprects a map with the file id
// as key and its content as io.ReadCloser.
func WriteFilesToZip(files map[int64]io.ReadCloser, wr *zip.Writer) (err error) {
for fid, file := range files {
header := &zip.FileHeader{
Name: "files/" + strconv.FormatInt(fid, 10),
Method: CompressionUsed,
}
w, err := wr.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(w, file)
if err != nil {
return fmt.Errorf("error writing file %d: %s", fid, err)
}
_ = file.Close()
}
return nil
}