mirror of
https://github.com/shlldev/miniws.git
synced 2025-09-02 19:00:59 +02:00
added compress.go file and added logs compression when too large
This commit is contained in:
79
miniws/compress.go
Normal file
79
miniws/compress.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package miniws
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type compress struct{}
|
||||
|
||||
// takes a file path, compresses the file into .tar.gz format
|
||||
// creating the archive in the same directory with the same base name.
|
||||
func (c *compress) CompressFile(filePath string) error {
|
||||
srcFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening source file: %w", err)
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
// destination file path: same dir, same base name with .tar.gz
|
||||
dir := filepath.Dir(filePath)
|
||||
base := filepath.Base(filePath)
|
||||
destName := base + ".tar.gz"
|
||||
destPath := filepath.Join(dir, destName)
|
||||
|
||||
destFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating dest file: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if cerr := destFile.Close(); cerr != nil && err == nil {
|
||||
err = fmt.Errorf("closing dest file: %w", cerr)
|
||||
}
|
||||
}()
|
||||
|
||||
// gzip writer
|
||||
gzWriter := gzip.NewWriter(destFile)
|
||||
defer func() {
|
||||
if cerr := gzWriter.Close(); cerr != nil && err == nil {
|
||||
err = fmt.Errorf("closing gzip writer: %w", cerr)
|
||||
}
|
||||
}()
|
||||
|
||||
// tar writer
|
||||
tarWriter := tar.NewWriter(gzWriter)
|
||||
defer func() {
|
||||
if cerr := tarWriter.Close(); cerr != nil && err == nil {
|
||||
err = fmt.Errorf("closing tar writer: %w", cerr)
|
||||
}
|
||||
}()
|
||||
|
||||
// Get file info for header
|
||||
fileInfo, err := srcFile.Stat()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting file info: %w", err)
|
||||
}
|
||||
|
||||
// Prepare tar header
|
||||
head := &tar.Header{
|
||||
Name: base,
|
||||
Size: fileInfo.Size(),
|
||||
Mode: int64(fileInfo.Mode().Perm()),
|
||||
ModTime: fileInfo.ModTime(),
|
||||
}
|
||||
if err := tarWriter.WriteHeader(head); err != nil {
|
||||
return fmt.Errorf("writing tar header: %w", err)
|
||||
}
|
||||
|
||||
// Copy file data into tar
|
||||
if _, err := io.Copy(tarWriter, srcFile); err != nil {
|
||||
return fmt.Errorf("writing file data to tar: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -2,14 +2,13 @@ package miniws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
FLAGS_LOG_OPEN int = os.O_APPEND | os.O_WRONLY | os.O_CREATE
|
||||
FLAGS_LOG_OPEN int = os.O_APPEND | os.O_RDWR | os.O_CREATE
|
||||
FLAGS_CONFIG_OPEN int = os.O_RDONLY | os.O_CREATE
|
||||
PERMS_LOG_OPEN os.FileMode = os.ModeType | os.ModePerm
|
||||
PERMS_CONFIG_OPEN os.FileMode = os.ModeType | os.ModePerm
|
||||
@@ -56,8 +55,8 @@ func (l *Logger) logError(str string) {
|
||||
func (l *Logger) writeToLogFileAndRenameIfBig(filename, content string) {
|
||||
file, err := os.OpenFile(ensureSlashSuffix(l.logFolder)+filename, FLAGS_LOG_OPEN, PERMS_LOG_OPEN)
|
||||
|
||||
if err != nil {
|
||||
log.Println("couldn't open log access file at", ensureSlashSuffix(l.logFolder)+filename)
|
||||
if l.logIfError(err) {
|
||||
return
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
@@ -65,14 +64,30 @@ func (l *Logger) writeToLogFileAndRenameIfBig(filename, content string) {
|
||||
|
||||
fileinfo, err := file.Stat()
|
||||
|
||||
if err != nil {
|
||||
log.Println("Error reading access file at", ensureSlashSuffix(l.logFolder)+filename)
|
||||
if l.logIfError(err) {
|
||||
return
|
||||
}
|
||||
|
||||
if fileinfo.Size() > l.maxLogBytes {
|
||||
defer os.Rename(ensureSlashSuffix(
|
||||
l.logFolder)+fileinfo.Name(),
|
||||
ensureSlashSuffix(l.logFolder)+fileinfo.Name()+"."+uuid.NewString(),
|
||||
|
||||
var renamedFiledPath string = ensureSlashSuffix(l.logFolder) + fileinfo.Name() + "." + uuid.NewString()
|
||||
|
||||
err_rename := os.Rename(
|
||||
ensureSlashSuffix(l.logFolder)+fileinfo.Name(),
|
||||
renamedFiledPath,
|
||||
)
|
||||
|
||||
if l.logIfError(err_rename) {
|
||||
return
|
||||
}
|
||||
|
||||
compress := &compress{}
|
||||
err_compress := compress.CompressFile(renamedFiledPath)
|
||||
|
||||
if l.logIfError(err_compress) {
|
||||
return
|
||||
}
|
||||
|
||||
defer os.Remove(renamedFiledPath)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user