more fixes
This commit is contained in:
+58
-15
@@ -15,11 +15,14 @@ type FileRepositoryInterface interface {
|
||||
CreateDir(dirPath string) error
|
||||
DeleteFile(fileName, basePath string) error
|
||||
MoveFile(srcFileName, destPath, basePath string) error
|
||||
UnzipHTMLContent(zipFileName, baseName, 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(fileName, basePath string) (map[string]string, error)
|
||||
ReadMetaFromFile(filePath string, basePath string) (map[string]string, error)
|
||||
}
|
||||
|
||||
type FileRepository struct{}
|
||||
@@ -29,7 +32,15 @@ func NewFileRepository() *FileRepository {
|
||||
}
|
||||
|
||||
func (r *FileRepository) FileExists(fileName, basePath string) bool {
|
||||
filePath := filepath.Join(basePath, fileName)
|
||||
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
|
||||
}
|
||||
|
||||
_, err := os.Stat(filePath)
|
||||
return err == nil
|
||||
}
|
||||
@@ -39,18 +50,31 @@ func (r *FileRepository) CreateDir(dirPath string) error {
|
||||
}
|
||||
|
||||
func (r *FileRepository) DeleteFile(fileName, basePath string) error {
|
||||
filePath := filepath.Join(basePath, fileName)
|
||||
var filePath string
|
||||
if filepath.IsAbs(fileName) {
|
||||
filePath = fileName
|
||||
} else if basePath != "" {
|
||||
filePath = filepath.Join(basePath, fileName)
|
||||
} else {
|
||||
filePath = fileName
|
||||
}
|
||||
return os.Remove(filePath)
|
||||
}
|
||||
|
||||
func (r *FileRepository) MoveFile(srcFileName, destPath, basePath string) error {
|
||||
srcPath := filepath.Join(basePath, srcFileName)
|
||||
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 (r *FileRepository) UnzipHTMLContent(zipFileName, baseName, basePath string) error {
|
||||
zipFilePath := filepath.Join(basePath, zipFileName)
|
||||
destDir := filepath.Join(basePath, "html", baseName)
|
||||
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)
|
||||
|
||||
@@ -83,6 +107,7 @@ func (r *FileRepository) UnzipHTMLContent(zipFileName, baseName, basePath string
|
||||
|
||||
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
outFile.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -108,19 +133,37 @@ func (r *FileRepository) GetSoftwareDir(softwareName, basePath string) string {
|
||||
return filepath.Join(basePath, softwareName)
|
||||
}
|
||||
|
||||
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 {
|
||||
softwareDir := r.GetSoftwareDir(softwareName, basePath)
|
||||
return filepath.Join(softwareDir, fmt.Sprintf("%s-%s.tic", softwareName, version))
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.tic", softwareName))
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetSourcePath(softwareName, version, basePath string) string {
|
||||
softwareDir := r.GetSoftwareDir(softwareName, basePath)
|
||||
return filepath.Join(softwareDir, fmt.Sprintf("%s-%s.lua", softwareName, version))
|
||||
return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.lua", softwareName))
|
||||
}
|
||||
|
||||
func (r *FileRepository) ReadMetaFromFile(fileName, basePath string) (map[string]string, error) {
|
||||
filePath := filepath.Join(basePath, fileName)
|
||||
file, err := os.Open(filePath)
|
||||
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
|
||||
}
|
||||
|
||||
file, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user