-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
file.go
133 lines (108 loc) · 3.07 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2023, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package backup
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
)
func splitFile(inputPath string, chunkSize int64) ([]string, error) {
inputFile, err := os.Open(inputPath)
if err != nil {
return nil, err
}
defer inputFile.Close()
chunkSize = 1024 * 1024 * chunkSize
fileInfo, err := os.Stat(inputPath)
if err != nil {
return nil, err
}
fileSize := fileInfo.Size()
numChunks := (fileSize + chunkSize - 1) / chunkSize
tmpDir := path.Join(os.TempDir(), "smart_home")
if err = os.MkdirAll(tmpDir, 0755); err != nil {
return nil, err
}
var fileList []string
fileName := strings.ReplaceAll(fileInfo.Name(), ".zip", "")
for i := int64(0); i < numChunks; i++ {
outputPath := fmt.Sprintf("%s_part%d.dat", fileName, i+1)
outputPath = path.Join(tmpDir, outputPath)
// Создаем новый файл для части
outputFile, err := os.Create(outputPath)
if err != nil {
return nil, err
}
defer outputFile.Close()
// Читаем кусок данных из исходного файла
bufferSize := chunkSize
if i == numChunks-1 {
bufferSize = fileSize - i*chunkSize
}
buffer := make([]byte, bufferSize)
_, err = inputFile.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
// Записываем кусок в новый файл
_, err = outputFile.Write(buffer)
if err != nil {
return nil, err
}
fileList = append(fileList, outputPath)
}
return fileList, nil
}
func joinFiles(inputPattern, dir string) (string, error) {
matches, err := filepath.Glob(inputPattern)
if err != nil {
return "", err
}
if len(matches) == 0 || len(matches) == 1 {
return "", errors.New("no matches or less then 2")
}
sort.Strings(matches)
fmt.Println(matches)
params := strings.Split(matches[0], "_part")
if len(params) < 2 {
return "", err
}
fileName := path.Join(dir, filepath.Base(fmt.Sprintf("%s.zip", params[0])))
outputFile, err := os.Create(fileName)
if err != nil {
return "", err
}
defer outputFile.Close()
for _, match := range matches {
inputFile, err := os.Open(match)
if err != nil {
return "", err
}
defer inputFile.Close()
_, err = io.Copy(outputFile, inputFile)
if err != nil {
return "", err
}
}
return fileName, nil
}