-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
295 lines (257 loc) · 8.18 KB
/
Copy pathserver.go
File metadata and controls
295 lines (257 loc) · 8.18 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package lcme
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/GomdimApps/lcme/system"
"github.com/GomdimApps/lcme/system/compressfiles"
"github.com/GomdimApps/lcme/system/threads"
"github.com/GomdimApps/lcme/system/utils"
)
// GetInfoServer collects detailed information about the server, including
// system distribution, RAM usage, disk space, CPU information,
// network and hardware information.
func GetInfoServer() system.ServerInfo {
distroInfo, _ := system.GetDistroInfo()
return system.ServerInfo{
Distribution: distroInfo,
RAM: system.GetRAMInfo(),
Disk: system.GetDiskInfo("/"),
CPU: system.GetCPUInfo(),
Network: system.GetNetworkInfo(),
Hardware: system.GetHardwareInfo(),
}
}
// Shell executes a command in the terminal and returns the result as a string,
// along with an error if one occurs.
func Shell(command string) (string, error) {
return utils.Cexec(command)
}
// Log returns a log function that writes messages to a specified .log file.
// If the file does not have the .log extension, it displays an error.
func Log(filePath string) func(string) {
if !strings.HasSuffix(filePath, ".log") {
fmt.Println("Error: The file must have a .log extension")
return func(value string) {
fmt.Println("Error: The file must have a .log extension")
}
}
return func(value string) {
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening the file:", err)
return
}
defer file.Close()
if _, err := file.WriteString(value + "\n"); err != nil {
fmt.Println("Error writing to the file:", err)
}
}
}
// GetFolderSize returns the size of the specified folder in bytes.
func GetFolderSize(path string) (uint64, error) {
size, err := system.GetFolderSize(path)
if err != nil {
return 0, fmt.Errorf("error getting folder size: %v", err)
}
return size, nil
}
// GetFileInfo returns information about the specified files in the given directory.
// If only one file is specified, it returns a slice with one element.
func GetFileInfo(dir string, files ...string) ([]system.FileInfo, error) {
var fileInfos []system.FileInfo
// Check if the directory is accessible
if _, err := os.Stat(dir); os.IsNotExist(err) {
return nil, fmt.Errorf("directory not found: %v", err)
} else if err != nil {
return nil, fmt.Errorf("unable to access directory: %v", err)
}
for _, file := range files {
filePath := filepath.Join(dir, file)
info, err := os.Stat(filePath)
if os.IsNotExist(err) {
return nil, fmt.Errorf("file not found: %s", file)
} else if err != nil {
return nil, fmt.Errorf("error capturing file information: %v", err)
}
extension := strings.TrimPrefix(filepath.Ext(file), ".")
if extension == "" {
extension = ""
}
fileData, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading file data: %v", err)
}
var buffer bytes.Buffer
buffer.Write(fileData)
fileInfos = append(fileInfos, system.FileInfo{
FileName: info.Name(),
FileSize: info.Size() / 1024,
FileLastChange: info.ModTime(),
FileUserPermisson: info.Mode(),
FileExtension: extension,
FileData: string(fileData),
FileDataBuffer: buffer,
FileDir: dir,
})
}
return fileInfos, nil
}
// MonitorNetworkRates continuously calculates and returns the download and upload rates.
func MonitorNetworkRates() chan system.NetworkInfo {
ratesChan := make(chan system.NetworkInfo)
go func() {
for {
initialStats, err := utils.GetNetworkStats()
if err != nil {
fmt.Println("Error getting initial network stats:", err)
continue
}
interfaceName, err := utils.GetActiveInterface(initialStats)
if err != nil {
fmt.Println("Error getting active interface:", err)
continue
}
downloadRate, uploadRate, err := utils.CalculateNetworkRates(initialStats, interfaceName)
if err != nil {
fmt.Println("Error calculating network rates:", err)
continue
}
ratesChan <- system.NetworkInfo{
Download: downloadRate,
Upload: uploadRate,
}
}
}()
return ratesChan
}
// ScaleFork accepts a task function and manages its execution using the Engine.
func ScaleFork(task threads.Task) {
engine := threads.NewEngine(runtime.NumCPU())
engine.Start()
engine.AddTask(task)
engine.Stop()
}
// ZipFiles cria um arquivo ZIP contendo os arquivos especificados na slice files.
// Os caminhos salvos no ZIP serão apenas os nomes base dos arquivos.
func ZipFiles(zipFilename string, files []string) error {
if !strings.HasSuffix(zipFilename, ".zip") {
return fmt.Errorf("extensão incorreta para arquivo ZIP: %s", zipFilename)
}
// Cria o diretório de saída, se necessário.
outputDir := filepath.Dir(zipFilename)
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
newZipFile, err := os.Create(zipFilename)
if err != nil {
return err
}
defer newZipFile.Close()
zipWriter := zip.NewWriter(newZipFile)
defer zipWriter.Close()
for _, file := range files {
// Aqui não passamos baseFolder para usar somente o nome base.
if err := compressfiles.AddFileToZip(zipWriter, file, ""); err != nil {
return err
}
}
return nil
}
// TarGzFiles cria um arquivo TAR.GZ contendo os arquivos especificados na slice files.
// Os caminhos salvos serão apenas os nomes base dos arquivos.
func TarGzFiles(tarGzFilename string, files []string) error {
if !strings.HasSuffix(tarGzFilename, ".tar.gz") {
return fmt.Errorf("extensão incorreta para arquivo TAR.GZ: %s", tarGzFilename)
}
outputDir := filepath.Dir(tarGzFilename)
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
tarFile, err := os.Create(tarGzFilename)
if err != nil {
return err
}
defer tarFile.Close()
gzipWriter := gzip.NewWriter(tarFile)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
for _, file := range files {
// Aqui também não passamos baseFolder para manter apenas o nome base.
if err := compressfiles.AddFileToTar(tarWriter, file, ""); err != nil {
return err
}
}
return nil
}
// ZipFolder cria um arquivo ZIP contendo todos os arquivos (recursivamente)
// dentro da pasta especificada, preservando a estrutura de diretórios.
func ZipFolder(zipFilename, folder string) error {
if !strings.HasSuffix(zipFilename, ".zip") {
return fmt.Errorf("extensão incorreta para arquivo ZIP: %s", zipFilename)
}
outputDir := filepath.Dir(zipFilename)
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
zipFile, err := os.Create(zipFilename)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Caminha pela pasta e adiciona cada arquivo (ignora diretórios).
err = filepath.Walk(folder, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if info.IsDir() {
return nil
}
// Aqui usamos folder como baseFolder para preservar o caminho relativo.
return compressfiles.AddFileToZip(zipWriter, path, folder)
})
return err
}
// TarGzFolder cria um arquivo TAR.GZ contendo todos os arquivos (recursivamente)
// dentro da pasta especificada, preservando a estrutura de diretórios.
func TarGzFolder(tarGzFilename, folder string) error {
if !strings.HasSuffix(tarGzFilename, ".tar.gz") {
return fmt.Errorf("extensão incorreta para arquivo TAR.GZ: %s", tarGzFilename)
}
outputDir := filepath.Dir(tarGzFilename)
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
}
tarGzFile, err := os.Create(tarGzFilename)
if err != nil {
return err
}
defer tarGzFile.Close()
gzipWriter := gzip.NewWriter(tarGzFile)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
err = filepath.Walk(folder, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if info.IsDir() {
return nil
}
// Check the file size before adding it to the TAR
if info.Size() > (1<<33)-1 { // 8GB - 1 byte
return fmt.Errorf("arquivo muito grande para o formato TAR: %s", path)
}
return compressfiles.AddFileToTar(tarWriter, path, folder)
})
return err
}