Skip to content

Commit

Permalink
feat: clean temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
bitxeno committed Jul 1, 2024
1 parent a6000fe commit ee885b5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package manager
import (
"context"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"fmt"

"github.com/bitxeno/atvloadly/internal/app"
"github.com/bitxeno/atvloadly/internal/log"
Expand Down Expand Up @@ -58,6 +61,9 @@ func (t *InstallManager) TryStart(ctx context.Context, udid, account, password,
}

func (t *InstallManager) Start(ctx context.Context, udid, account, password, ipaPath string) error {
t.outputStdout.Reset()
t.outputStderr.Reset()

// set execute timeout 5 miniutes
timeout := 5 * time.Minute
ctx, cancel := context.WithTimeout(ctx, timeout)
Expand Down Expand Up @@ -95,6 +101,14 @@ func (t *InstallManager) Start(ctx context.Context, udid, account, password, ipa
return err
}

func (t *InstallManager) CleanTempFiles(ipaPath string) {
ipaName := filepath.Base(ipaPath)
fileNameWithoutExt := strings.TrimSuffix(ipaName, filepath.Ext(ipaName))
os.RemoveAll(filepath.Join(app.Config.Server.DataDir, "tmp", fileNameWithoutExt+".ipa"))
os.RemoveAll(filepath.Join(app.Config.Server.DataDir, "tmp", fileNameWithoutExt+".png"))
os.RemoveAll(filepath.Join(os.TempDir(), fileNameWithoutExt+".ipa"))
}

func (t *InstallManager) Close() {
if t.cancel != nil {
t.cancel()
Expand Down Expand Up @@ -124,6 +138,26 @@ func (t *InstallManager) OutputLog() string {
return t.outputStdout.String()
}

func (t *InstallManager) WriteLog(id uint) {
data := t.OutputLog()

// Hide log password string
// data = strings.Replace(data, v.Password, "******", -1)

saveDir := filepath.Join(app.Config.Server.DataDir, "log")
if err := os.MkdirAll(saveDir, os.ModePerm); err != nil {
log.Error("failed to create directory :" + saveDir)
return
}

path := filepath.Join(saveDir, fmt.Sprintf("task_%d.log", id))
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
log.Error("write log failed :" + path)
return
}
}


type outputWriter struct {
data []byte
em *event.Manager
Expand All @@ -146,3 +180,7 @@ func (w *outputWriter) Write(p []byte) (n int, err error) {
func (w *outputWriter) String() string {
return string(w.data)
}

func (w *outputWriter) Reset() {
w.data = []byte{}
}
File renamed without changes.
21 changes: 20 additions & 1 deletion internal/service/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package service
import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/bitxeno/atvloadly/internal/log"
"github.com/bitxeno/atvloadly/internal/manager"
Expand Down Expand Up @@ -59,11 +61,28 @@ func HandleInstallMessage(c *websocket.Conn) {
func runInstallMessage(mgr *manager.WebsocketManager, installMgr *manager.InstallManager, v model.InstalledApp) {
err := installMgr.Start(mgr.Context(), v.UDID, v.Account, v.Password, v.IpaPath)
if err != nil {
installMgr.CleanTempFiles(v.IpaPath)
msg := fmt.Sprintf("ERROR: %s", err.Error())
mgr.WriteMessage(msg)
return
}
log.Infof("install exit: %s", v.IpaPath)

if strings.Contains(installMgr.OutputLog(), "Installation Succeeded") {
now := time.Now()
v.RefreshedDate = &now
v.RefreshedResult = true
app, err := SaveApp(v)
if err != nil {
installMgr.CleanTempFiles(v.IpaPath)
msg := fmt.Sprintf("ERROR: save app to db failed. %s", err.Error())
mgr.WriteMessage(msg)
return
} else {
installMgr.WriteLog(app.ID)
}
}

installMgr.CleanTempFiles(v.IpaPath)
}

func HandlePairMessage(c *websocket.Conn) {
Expand Down
24 changes: 1 addition & 23 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package task
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -162,11 +160,7 @@ func (t *Task) runInternal(v model.InstalledApp) error {
installMgr := manager.NewInstallManager()
defer installMgr.Close()
err := installMgr.TryStart(context.Background(), v.UDID, v.Account, v.Password, v.IpaPath)
if err != nil {
return err
}

t.writeLog(v, installMgr.OutputLog())
installMgr.WriteLog(v.ID)
if err != nil {
log.Err(err).Msgf("Error executing installation script. %s", installMgr.ErrorLog())
return err
Expand All @@ -178,22 +172,6 @@ func (t *Task) runInternal(v model.InstalledApp) error {
}
}

func (t *Task) writeLog(v model.InstalledApp, data string) {
// Hide log password string
data = strings.Replace(data, v.Password, "******", -1)

saveDir := filepath.Join(app.Config.Server.DataDir, "log")
if err := os.MkdirAll(saveDir, os.ModePerm); err != nil {
log.Error("failed to create directory :" + saveDir)
return
}

path := filepath.Join(saveDir, fmt.Sprintf("task_%d.log", v.ID))
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
log.Error("write task log failed :" + path)
return
}
}

func ScheduleRefreshApps() error {
return instance.RunSchedule()
Expand Down
37 changes: 13 additions & 24 deletions web/static/src/page/install/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,18 @@ export default {
let ipa = data[0];
_this.ipa = ipa;
// send start install msg
_this.websocketsend(1, {'udid': _this.device.udid, 'account': _this.form.account, 'password': _this.form.password, 'ipa_path': ipa.path});
_this.websocketsend(1, {
ID: 0,
ipa_name: _this.ipa.name,
ipa_path: _this.ipa.path,
device: _this.device.mac_addr,
udid: _this.device.udid,
account: _this.form.account,
password: _this.form.password,
icon: _this.ipa.icon,
bundle_identifier: _this.ipa.bundle_identifier,
version: _this.ipa.version,
});
} catch (error) {
console.log(error);
_this.log.output += error;
Expand Down Expand Up @@ -286,35 +297,13 @@ export default {
if (line.indexOf("ERROR") !== -1) {
_this.loading = false;
toast.error(this.$t("install.toast.install_failed"));
// clean upload temp file
api.clean(_this.ipa)
return;
}
// Installation successful.
if (line.indexOf("Installation Succeeded") !== -1) {
_this.loading = false;
// Save installation data.
api
.saveApp({
ID: 0,
ipa_name: _this.ipa.name,
ipa_path: _this.ipa.path,
device: _this.device.mac_addr,
udid: _this.device.udid,
account: _this.form.account,
password: _this.form.password,
refreshed_result: true,
icon: _this.ipa.icon,
bundle_identifier: _this.ipa.bundle_identifier,
version: _this.ipa.version,
})
.then((res) => {
toast.success(this.$t("install.toast.install_success"));
});
toast.success(this.$t("install.toast.install_success"));
return;
}
},
Expand Down

0 comments on commit ee885b5

Please sign in to comment.