refactor: extract models/routes and env-based server config
- Move API request/response structs into new lib/models package - Centralize Gin route registration in lib/routing to simplify wiring - Add lib/server config helper to allow WARPBOX_BOX_POLL_INTERVAL_MS override - Improves modularity and makes polling behavior configurable per environmentrefactor: extract models/routes and env-based server config - Move API request/response structs into new lib/models package - Centralize Gin route registration in lib/routing to simplify wiring - Add lib/server config helper to allow WARPBOX_BOX_POLL_INTERVAL_MS override - Improves modularity and makes polling behavior configurable per environment
This commit is contained in:
55
lib/server/paths.go
Normal file
55
lib/server/paths.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func boxPath(boxID string) string {
|
||||
return filepath.Join(uploadRoot, boxID)
|
||||
}
|
||||
|
||||
func manifestPath(boxID string) string {
|
||||
return filepath.Join(boxPath(boxID), boxManifestFile)
|
||||
}
|
||||
|
||||
func safeFilename(name string) (string, bool) {
|
||||
filename := filepath.Base(name)
|
||||
filename = strings.TrimSpace(filename)
|
||||
return filename, filename != "" && filename != "." && filename != string(filepath.Separator)
|
||||
}
|
||||
|
||||
func safeBoxFilePath(boxID string, filename string) (string, bool) {
|
||||
path := filepath.Join(boxPath(boxID), filename)
|
||||
return path, strings.HasPrefix(path, boxPath(boxID)+string(filepath.Separator))
|
||||
}
|
||||
|
||||
func uniqueFilename(directory string, filename string) string {
|
||||
if _, err := os.Stat(filepath.Join(directory, filename)); os.IsNotExist(err) {
|
||||
return filename
|
||||
}
|
||||
|
||||
extension := filepath.Ext(filename)
|
||||
base := strings.TrimSuffix(filename, extension)
|
||||
for count := 2; ; count++ {
|
||||
candidate := base + "-" + strconv.Itoa(count) + extension
|
||||
if _, err := os.Stat(filepath.Join(directory, candidate)); os.IsNotExist(err) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueManifestFilename(filename string, usedNames map[string]int) string {
|
||||
count := usedNames[filename]
|
||||
usedNames[filename] = count + 1
|
||||
|
||||
if count == 0 {
|
||||
return filename
|
||||
}
|
||||
|
||||
extension := filepath.Ext(filename)
|
||||
base := strings.TrimSuffix(filename, extension)
|
||||
return base + "-" + strconv.Itoa(count+1) + extension
|
||||
}
|
||||
Reference in New Issue
Block a user