refactor(resource): improve directory cleanup in `Extract` function

* Added checks to remove existing directories before extracting files for hosts, ui, others, and script_plugin_templates.
* Enhanced logging in `StartUIApp` to include the app path in the error message when the UI app does not exist.
This commit is contained in:
qianlifeng 2025-07-30 00:16:49 +08:00
parent fe23eed7b6
commit 9f9cf84ea8
No known key found for this signature in database
2 changed files with 39 additions and 5 deletions

View File

@ -35,26 +35,60 @@ var embedThemes = []string{}
func Extract(ctx context.Context) error { func Extract(ctx context.Context) error {
start := util.GetSystemTimestamp() start := util.GetSystemTimestamp()
extractHostErr := extractFiles(ctx, HostFS, util.GetLocation().GetHostDirectory(), "hosts", false)
// hosts
hostDirectory := util.GetLocation().GetHostDirectory()
if util.IsDirExists(hostDirectory) {
rmErr := os.RemoveAll(hostDirectory)
if rmErr != nil {
return rmErr
}
}
extractHostErr := extractFiles(ctx, HostFS, hostDirectory, "hosts", false)
if extractHostErr != nil { if extractHostErr != nil {
return extractHostErr return extractHostErr
} }
flutterErr := extractFiles(ctx, UIFS, util.GetLocation().GetUIDirectory(), "ui/flutter", true) // ui
uiDiretory := util.GetLocation().GetUIDirectory()
if util.IsDirExists(uiDiretory) {
rmErr := os.RemoveAll(uiDiretory)
if rmErr != nil {
return rmErr
}
}
flutterErr := extractFiles(ctx, UIFS, uiDiretory, "ui/flutter", true)
if flutterErr != nil { if flutterErr != nil {
return flutterErr return flutterErr
} }
othersErr := extractFiles(ctx, OthersFS, util.GetLocation().GetOthersDirectory(), "others", false) // others
othersDirectory := util.GetLocation().GetOthersDirectory()
if util.IsDirExists(othersDirectory) {
rmErr := os.RemoveAll(othersDirectory)
if rmErr != nil {
return rmErr
}
}
othersErr := extractFiles(ctx, OthersFS, othersDirectory, "others", false)
if othersErr != nil { if othersErr != nil {
return othersErr return othersErr
} }
scriptPluginTemplatesErr := extractFiles(ctx, ScriptPluginTemplatesFS, util.GetLocation().GetScriptPluginTemplatesDirectory(), "script_plugin_templates", false) // script_plugin_templates
scriptPluginTemplatesDirectory := util.GetLocation().GetScriptPluginTemplatesDirectory()
if util.IsDirExists(scriptPluginTemplatesDirectory) {
rmErr := os.RemoveAll(scriptPluginTemplatesDirectory)
if rmErr != nil {
return rmErr
}
}
scriptPluginTemplatesErr := extractFiles(ctx, ScriptPluginTemplatesFS, scriptPluginTemplatesDirectory, "script_plugin_templates", false)
if scriptPluginTemplatesErr != nil { if scriptPluginTemplatesErr != nil {
return scriptPluginTemplatesErr return scriptPluginTemplatesErr
} }
// themes
themeErr := parseThemes(ctx) themeErr := parseThemes(ctx)
if themeErr != nil { if themeErr != nil {
return themeErr return themeErr

View File

@ -295,7 +295,7 @@ func (m *Manager) UpdateServerPort(port int) {
func (m *Manager) StartUIApp(ctx context.Context) error { func (m *Manager) StartUIApp(ctx context.Context) error {
var appPath = util.GetLocation().GetUIAppPath() var appPath = util.GetLocation().GetUIAppPath()
if fileInfo, statErr := os.Stat(appPath); os.IsNotExist(statErr) { if fileInfo, statErr := os.Stat(appPath); os.IsNotExist(statErr) {
logger.Info(ctx, "UI app not exist") logger.Info(ctx, "UI app not exist: "+appPath)
return errors.New("UI app not exist") return errors.New("UI app not exist")
} else { } else {
if !util.IsFileExecAny(fileInfo.Mode()) { if !util.IsFileExecAny(fileInfo.Mode()) {