Skip to content

Commit

Permalink
v0.1.4 - 修复中途终止时上传成功但分享链接未打印的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jan 3, 2025
1 parent 2ff558a commit d4cbd07
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
36 changes: 30 additions & 6 deletions goecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
)

var (
ecsVersion = "v0.1.3"
ecsVersion = "v0.1.4"
menuMode bool
onlyChinaTest bool
input, choice string
Expand Down Expand Up @@ -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() {
Expand All @@ -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("上传超时,程序退出")
Expand Down Expand Up @@ -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...")
Expand Down
17 changes: 9 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 转义序列
Expand All @@ -331,29 +331,30 @@ 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 {
// 获取文件的绝对路径
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 "", ""
}

0 comments on commit d4cbd07

Please sign in to comment.