v0.1.4 - 修复中途终止时上传成功但分享链接未打印的问题

This commit is contained in:
spiritlhl
2025-01-03 17:44:22 +08:00
parent 2ff558ae7b
commit d4cbd0772a
2 changed files with 39 additions and 14 deletions

View File

@@ -39,7 +39,7 @@ import (
)
var (
ecsVersion = "v0.1.3"
ecsVersion = "v0.1.4"
menuMode bool
onlyChinaTest bool
input, choice string
@@ -323,7 +323,6 @@ func main() {
minutes := int(duration.Minutes())
seconds := int(duration.Seconds()) % 60
currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
// 使用互斥锁保护output的写入
var mu sync.Mutex
mu.Lock()
output = utils.PrintAndCapture(func() {
@@ -333,14 +332,32 @@ func main() {
utils.PrintCenteredTitle("", width)
}, tempOutput, output)
mu.Unlock()
// 启动新的goroutine处理上传
// 创建一个通道来传递上传结果
resultChan := make(chan struct {
httpURL string
httpsURL string
}, 1) // 使用带缓冲的通道,避免可能的阻塞
// 启动上传
go func() {
utils.ProcessAndUpload(output, filePath, enabelUpload)
httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload)
resultChan <- struct {
httpURL string
httpsURL string
}{httpURL, httpsURL}
uploadDone <- true
}()
// 等待上传完成或超时
select {
case <-uploadDone:
case result := <-resultChan:
if result.httpURL != "" || result.httpsURL != "" {
if language == "en" {
fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL)
} else {
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", result.httpURL, result.httpsURL)
}
}
// 给打印操作一些时间完成
time.Sleep(100 * time.Millisecond)
os.Exit(0)
case <-time.After(30 * time.Second):
fmt.Println("上传超时,程序退出")
@@ -592,7 +609,14 @@ func main() {
default:
fmt.Println("Unsupported language")
}
utils.ProcessAndUpload(output, filePath, enabelUpload)
httpURL, httpsURL := utils.ProcessAndUpload(output, filePath, enabelUpload)
if httpURL != "" || httpsURL != "" {
if language == "en" {
fmt.Printf("Upload successfully!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL)
} else {
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", httpURL, httpsURL)
}
}
finish = true
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
fmt.Println("Press Enter to exit...")

View File

@@ -299,7 +299,7 @@ func UploadText(absPath string) (string, string, error) {
}
// ProcessAndUpload 创建结果文件并上传文件
func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
func ProcessAndUpload(output string, filePath string, enableUplaod bool) (string, string) {
// 使用 defer 来处理 panic
defer func() {
if r := recover(); r != nil {
@@ -312,14 +312,14 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
err = os.Remove(filePath)
if err != nil {
fmt.Println("无法删除文件:", err)
return
return "", ""
}
}
// 创建文件
file, err := os.Create(filePath)
if err != nil {
fmt.Println("无法创建文件:", err)
return
return "", ""
}
defer file.Close()
// 匹配 ANSI 转义序列
@@ -331,13 +331,13 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
_, err = writer.WriteString(cleanedOutput)
if err != nil {
fmt.Println("无法写入文件:", err)
return
return "", ""
}
// 确保写入缓冲区的数据都刷新到文件中
err = writer.Flush()
if err != nil {
fmt.Println("无法刷新文件缓冲:", err)
return
return "", ""
}
fmt.Printf("测试结果已写入 %s\n", filePath)
if enableUplaod {
@@ -345,15 +345,16 @@ func ProcessAndUpload(output string, filePath string, enableUplaod bool) {
absPath, err := filepath.Abs(filePath)
if err != nil {
fmt.Println("无法获取文件绝对路径:", err)
return
return "", ""
}
// 上传文件并生成短链接
http_url, https_url, err := UploadText(absPath)
if err != nil {
fmt.Println("上传失败,无法生成链接")
fmt.Println(err.Error())
return
return "", ""
}
fmt.Printf("上传成功!\nHttp URL: %s\nHttps URL: %s\n", http_url, https_url)
return http_url, https_url
}
return "", ""
}