new release management
This commit is contained in:
+67
-146
@@ -2,7 +2,6 @@ package domain
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -11,185 +10,107 @@ import (
|
||||
)
|
||||
|
||||
type FileRepositoryInterface interface {
|
||||
FileExists(fileName, basePath string) bool
|
||||
CreateDir(dirPath string) error
|
||||
DeleteFile(fileName, basePath string) error
|
||||
MoveFile(srcFileName, destPath, basePath string) error
|
||||
UnzipHTMLContent(zipFilePath, softwareName, version, basePath string) error
|
||||
GetSoftwareDir(softwareName, basePath string) string
|
||||
GetSoftwareVersionDir(softwareName, version, basePath string) string
|
||||
GetFileInSoftwareVersionDir(softwareName, version, fileName, basePath string) string
|
||||
GetHTMLContentDir(softwareName, version, basePath string) string
|
||||
GetCartridgePath(softwareName, version, basePath string) string
|
||||
GetSourcePath(softwareName, version, basePath string) string
|
||||
ReadMetaFromFile(filePath string, basePath string) (map[string]string, error)
|
||||
GetPath(path string) string
|
||||
FileExists(path string) bool
|
||||
CreateDir(path string) error
|
||||
DeleteFile(path string) error
|
||||
MoveFile(srcPath, destPath string) error
|
||||
UnzipFile(path, destPath string) error
|
||||
}
|
||||
|
||||
type FileRepository struct{}
|
||||
type FileRepository struct {
|
||||
fileContainerPath string
|
||||
}
|
||||
|
||||
func NewFileRepository() *FileRepository {
|
||||
return &FileRepository{}
|
||||
fileContainerPath, _ := os.LookupEnv("FILE_CONTAINER_PATH")
|
||||
return &FileRepository{
|
||||
fileContainerPath: fileContainerPath,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *FileRepository) FileExists(fileName, basePath string) bool {
|
||||
var filePath string
|
||||
if filepath.IsAbs(fileName) {
|
||||
filePath = fileName
|
||||
} else if basePath != "" {
|
||||
filePath = filepath.Join(basePath, fileName)
|
||||
} else {
|
||||
filePath = fileName // Assume it's relative to current working dir or absolute
|
||||
}
|
||||
func (fr *FileRepository) GetPath(path string) string {
|
||||
return filepath.Join(fr.fileContainerPath, path)
|
||||
}
|
||||
|
||||
_, err := os.Stat(filePath)
|
||||
func (fr *FileRepository) FileExists(path string) bool {
|
||||
fullPath := fr.GetPath(path)
|
||||
_, err := os.Stat(fullPath)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func (r *FileRepository) CreateDir(dirPath string) error {
|
||||
return os.MkdirAll(dirPath, os.ModePerm)
|
||||
func (fr *FileRepository) CreateDir(path string) error {
|
||||
fullPath := fr.GetPath(path)
|
||||
return os.MkdirAll(fullPath, 0755)
|
||||
}
|
||||
|
||||
func (r *FileRepository) DeleteFile(fileName, basePath string) error {
|
||||
var filePath string
|
||||
if filepath.IsAbs(fileName) {
|
||||
filePath = fileName
|
||||
} else if basePath != "" {
|
||||
filePath = filepath.Join(basePath, fileName)
|
||||
} else {
|
||||
filePath = fileName
|
||||
func (fr *FileRepository) DeleteFile(path string) error {
|
||||
fullPath := fr.GetPath(path)
|
||||
return os.RemoveAll(fullPath)
|
||||
}
|
||||
|
||||
func (fr *FileRepository) MoveFile(srcPath, destPath string) error {
|
||||
fullSrcPath := fr.GetPath(srcPath)
|
||||
fullDestPath := fr.GetPath(destPath)
|
||||
|
||||
destDir := filepath.Dir(fullDestPath)
|
||||
if err := os.MkdirAll(destDir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create destination directory: %w", err)
|
||||
}
|
||||
return os.Remove(filePath)
|
||||
|
||||
return os.Rename(fullSrcPath, fullDestPath)
|
||||
}
|
||||
|
||||
func (r *FileRepository) MoveFile(srcFileName, destPath, basePath string) error {
|
||||
var srcPath string
|
||||
if filepath.IsAbs(srcFileName) {
|
||||
srcPath = srcFileName
|
||||
} else if basePath != "" {
|
||||
srcPath = filepath.Join(basePath, srcFileName)
|
||||
} else {
|
||||
srcPath = srcFileName
|
||||
}
|
||||
return os.Rename(srcPath, destPath)
|
||||
}
|
||||
func (fr *FileRepository) UnzipFile(path, destPath string) error {
|
||||
fullPath := fr.GetPath(path)
|
||||
fullDestPath := fr.GetPath(destPath)
|
||||
|
||||
func (r *FileRepository) UnzipHTMLContent(zipFilePath, softwareName, version, basePath string) error {
|
||||
destDir := r.GetHTMLContentDir(softwareName, version, basePath)
|
||||
|
||||
fmt.Printf("FileRepository: Unzipping %s to %s\n", zipFilePath, destDir)
|
||||
|
||||
reader, err := zip.OpenReader(zipFilePath)
|
||||
reader, err := zip.OpenReader(fullPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to open zip file: %w", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
destDir, err = filepath.Abs(destDir)
|
||||
if err != nil {
|
||||
return err
|
||||
if err := os.MkdirAll(fullDestPath, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create destination directory: %w", err)
|
||||
}
|
||||
|
||||
for _, f := range reader.File {
|
||||
fpath := filepath.Join(destDir, f.Name)
|
||||
|
||||
if !strings.HasPrefix(fpath, destDir) {
|
||||
return fmt.Errorf("%s: illegal file path", fpath)
|
||||
}
|
||||
|
||||
if f.FileInfo().IsDir() {
|
||||
os.MkdirAll(fpath, os.ModePerm)
|
||||
continue
|
||||
}
|
||||
|
||||
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
outFile.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
outFile.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(outFile, rc)
|
||||
outFile.Close()
|
||||
rc.Close()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
for _, file := range reader.File {
|
||||
if err := extractZipFile(file, fullDestPath); err != nil {
|
||||
return fmt.Errorf("failed to extract %s: %w", file.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetSoftwareDir(softwareName, basePath string) string {
|
||||
return filepath.Join(basePath, softwareName)
|
||||
}
|
||||
func extractZipFile(file *zip.File, destPath string) error {
|
||||
filePath := filepath.Join(destPath, file.Name)
|
||||
|
||||
func (r *FileRepository) GetSoftwareVersionDir(softwareName, version, basePath string) string {
|
||||
return filepath.Join(r.GetSoftwareDir(softwareName, basePath), version)
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetFileInSoftwareVersionDir(softwareName, version, fileName, basePath string) string {
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fileName)
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetHTMLContentDir(softwareName, version, basePath string) string {
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), "html")
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetCartridgePath(softwareName, version, basePath string) string {
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.tic", softwareName))
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetSourcePath(softwareName, version, basePath string) string {
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.lua", softwareName))
|
||||
}
|
||||
|
||||
func (r *FileRepository) ReadMetaFromFile(filePath string, basePath string) (map[string]string, error) {
|
||||
var fullPath string
|
||||
if filepath.IsAbs(filePath) {
|
||||
fullPath = filePath
|
||||
} else if basePath != "" {
|
||||
fullPath = filepath.Join(basePath, filePath)
|
||||
} else {
|
||||
fullPath = filePath
|
||||
if !strings.HasPrefix(filePath, filepath.Clean(destPath)+string(os.PathSeparator)) {
|
||||
return fmt.Errorf("invalid file path: %s", file.Name)
|
||||
}
|
||||
|
||||
file, err := os.Open(fullPath)
|
||||
if file.FileInfo().IsDir() {
|
||||
return os.MkdirAll(filePath, file.Mode())
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcFile, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
defer srcFile.Close()
|
||||
|
||||
metaData := make(map[string]string)
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "--") {
|
||||
parts := strings.SplitN(strings.TrimPrefix(line, "--"), ":", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.TrimSpace(parts[1])
|
||||
metaData[strings.ToLower(key)] = val
|
||||
} else {
|
||||
break
|
||||
}
|
||||
destFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return metaData, nil
|
||||
_, err = io.Copy(destFile, srcFile)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user